{"id":195,"date":"2018-10-12T07:20:00","date_gmt":"2018-10-11T23:20:00","guid":{"rendered":"https:\/\/yeslq.com\/201810195.html"},"modified":"2019-05-07T10:43:22","modified_gmt":"2019-05-07T02:43:22","slug":"the-linux-command-line-writing-your","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=195","title":{"rendered":"The Linux Command Line&#8212;Writing Your First Script And Getting It To Work"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div>\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\"><br \/>\n<\/span><\/div>\n<div>\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\"><\/span><\/p>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">To successfully write a shell script, you have to do three things:<\/span><\/div>\n<ol style=\"background-color: white;\">\n<li><span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Write a script<\/span><\/li>\n<p><span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\"><\/p>\n<li>Give the shell permission to execute it<\/li>\n<li>Put it somewhere the shell can find it<\/li>\n<p><\/span><\/ol>\n<h2 style=\"background-color: white; font-weight: normal;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Writing A Script<\/span><\/h2>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">A shell script is a file that contains ASCII text. To create a shell script, you use a&nbsp;<i>text editor<\/i>. A text editor is a program, like a word processor, that reads and writes ASCII text files. There are many, many text editors available for your Linux system, both for the command line environment and the GUI environment. Here is <\/span><span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">a list of some common ones:<\/span><\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\" summary=\"Common text editor programs\">\n<tbody>\n<tr>\n<td valign=\"top\"><strong>Name<\/strong><\/td>\n<td valign=\"top\"><strong>Description<\/strong><\/td>\n<td valign=\"top\"><strong>Interface<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/vim1.html\" style=\"color: #002740;\">vi, vim<\/a><\/td>\n<td valign=\"top\">The granddaddy of Unix text editors,&nbsp;vi, is infamous for its difficult, non-intuitive command structure. On the bright side,&nbsp;vi&nbsp;is powerful, lightweight, and fast. Learning&nbsp;vi&nbsp;is a Unix rite of passage, since it is universally available on Unix-like systems. On most Linux distributions, an enhanced version of the traditional&nbsp;vi&nbsp;editor called&nbsp;vim&nbsp;is used.<\/td>\n<td valign=\"top\">command line<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Emacs<\/td>\n<td valign=\"top\">The true giant in the world of text editors is Emacs by&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Richard_Stallman\" style=\"color: #002740;\">Richard Stallman<\/a>. Emacs contains (or can be made to contain) every feature ever conceived for a text editor. It should be noted that&nbsp;vi&nbsp;and Emacs fans fight bitter religious wars over which is better.<\/td>\n<td valign=\"top\">command line<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/nano1.html\" style=\"color: #002740;\">nano<\/a><\/td>\n<td valign=\"top\">nano&nbsp;is a free clone of the text editor supplied with the&nbsp;pine&nbsp;email program.&nbsp;nano&nbsp;is very easy to use but is very short on features. I recommend&nbsp;nano&nbsp;for first-time users who need a command line editor.<\/td>\n<td valign=\"top\">command line<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/gedit1.html\" style=\"color: #002740;\">gedit<\/a><\/td>\n<td valign=\"top\">gedit&nbsp;is the editor supplied with the Gnome desktop environment.<\/td>\n<td valign=\"top\">graphical<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">kwrite<\/td>\n<td valign=\"top\">kwrite&nbsp;is the &#8220;advanced editor&#8221; supplied with KDE. It has syntax highlighting, a helpful feature for programmers and script writers.<\/td>\n<td valign=\"top\">graphical<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div style=\"background-color: white;\">\n<br \/>\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Now, fire up your text editor and type in your first script as follows:<\/span><\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; padding: 0.5em;\">\n<pre><span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">#!\/bin\/bash\n# My first script\n\necho \"Hello World!\"\n\n<\/span><\/pre>\n<\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">The clever among you will have figured out how to copy and paste the text into your text editor \ud83d\ude09<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">If you have ever opened a book on programming, you would immediately recognize this as the traditional &#8220;Hello World&#8221; program. Save your file with some descriptive name. How abouthello_world?<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">The first line of the script is important. This is a special clue, called a&nbsp;<i>shebang<\/i>, given to the shell indicating what program is used to interpret the script. In this case, it is&nbsp;\/bin\/bash. Other scripting languages such as&nbsp;Perl, awk, tcl, Tk,&nbsp;and&nbsp;python&nbsp;also use this mechanism.<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">The second line is a&nbsp;<i>comment<\/i>. Everything that appears after a &#8220;#&#8221; symbol is ignored by&nbsp;bash. As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/echo1.html\" style=\"color: #002740;\">echo<\/a>command. This command simply prints its arguments on the display.<\/span><\/div>\n<h2 style=\"background-color: white; font-weight: normal;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Setting Permissions<\/span><\/h2>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">The next thing we have to do is give the shell permission to execute your script. This is done with the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/chmod1.html\" style=\"color: #002740;\">chmod<\/a>&nbsp;command as follows:<\/span><\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; padding: 0.5em;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">[me@linuxbox me]$&nbsp;chmod 755 hello_world<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">The &#8220;755&#8221; will give you read, write, and execute permission. Everybody else will get only read and execute permission. If you want your script to be private (i.e., only you can read and execute), use &#8220;700&#8221; instead.<\/span><\/div>\n<h2 style=\"background-color: white; font-weight: normal;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Putting It In Your Path<\/span><\/h2>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">At this point, your script will run. Try this:<\/span><\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; padding: 0.5em;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">[me@linuxbox me]$&nbsp;.\/hello_world<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">You should see &#8220;Hello World!&#8221; displayed. If you do not, see what directory you really saved your script in, go there and try again.<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Before we go any further, I have to stop and talk a while about paths. When you type in the name of a command, the system does not search the entire computer to find where the program is located. That would take a long time. You have noticed that you don&#8217;t usually have to specify a complete path name to the program you want to run, the shell just seems to know.<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Well, you are right. The shell does know. Here&#8217;s how: the shell maintains a list of directories where executable files (programs) are kept, and only searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous&nbsp;command not founderror message.<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">This list of directories is called your&nbsp;<i>path<\/i>. You can view the list of directories with the following command:<\/span><\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; padding: 0.5em;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">[me@linuxbox me]$&nbsp;echo $PATH<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">This will return a colon separated list of directories that will be searched if a specific path name is not given when a command is attempted. In our first attempt to execute your new script, we specified a pathname (&#8220;.\/&#8221;) to the file.<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">You can add directories to your path with the following command, where&nbsp;<i>directory<\/i>&nbsp;is the name of the directory you want to add:<\/span><\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; padding: 0.5em;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">[me@linuxbox me]$&nbsp;export PATH=$PATH:<i>directory<\/i><\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">A better way would be to edit your&nbsp;.bash_profile&nbsp;or&nbsp;.profile&nbsp;file (depending on your distribution) to include the above command. That way, it would be done automatically every time you log in.<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Most Linux distributions encourage a practice in which each user has a specific directory for the programs he\/she personally uses. This directory is called&nbsp;bin&nbsp;and is a subdirectory of your home directory. If you do not already have one, create it with the following command:<\/span><\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; padding: 0.5em;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">[me@linuxbox me]$&nbsp;mkdir bin<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">Move your script into your new&nbsp;bin&nbsp;directory and you&#8217;re all set. Now you just have to type:<\/span><\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; padding: 0.5em;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">[me@linuxbox me]$&nbsp;hello_world<\/span><\/div>\n<div style=\"background-color: white;\">\n<span style=\"font-family: &quot;verdana&quot; , sans-serif; font-size: xx-small;\">and your script will run. On some distributions, most notably Ubuntu, you will need to open a new terminal session before your newly created&nbsp;bin&nbsp;directory will be recognised.<\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts To succes &#8230; <a title=\"The Linux Command Line&#8212;Writing Your First Script And Getting It To Work\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=195\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Writing Your First Script And Getting It To Work\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[51,52,53],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}