User Tools

Site Tools


writing_a_shell_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
writing_a_shell_script [2016/08/03 16:44] walkeradminwriting_a_shell_script [2023/03/09 22:35] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Writing a Shell Script ====== ====== Writing a Shell Script ======
 +<color darkorange>2016</color>
 \\  \\ 
 \\  \\ 
-==== To successfully write a shell script, you have to do three things: ====+\\  
 +==== To successfully write a shell script (also known as a bash script or .bat (batch) file (Windows), you have to do three things: ====
 \\  \\ 
     1.Write a script     1.Write a script
Line 27: Line 29:
 The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows: The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows:
 \\  \\ 
-    [me@linuxbox me]$ chmod 755 hello_world+    chmod 755 helloworld (sudo if you are using Raspbian)
 \\  \\ 
 The "755" 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 "700" instead. The "755" 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 "700" instead.
 +\\  
 +\\  
 +At this point, your script will run. Try this: 
 +\\  
 +    ./hello_world 
 +\\  
 +You should see "Hello World!" displayed. If you do not, see what directory you really saved your script in, go there and try again. 
 +\\  
 +\\  
 +==== Paths ==== 
 +\\  
 +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't usually have to specify a complete path name to the program you want to run, the shell just seems to know.\\  
 +\\  
 +Well, you are right. The shell does know. Here'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 command not found error message.\\  
 +\\  
 +This list of directories is called your path. You can view the list of directories with the following command:\\  
 +\\  
 +    echo $PATH 
 +\\  
 +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 ("./") to the file.\\  
 +\\  
 +You can add directories to your path with the following command, where directory is the name of the directory you want to add: 
 +\\  
 +    export PATH=$PATH:directory 
 +\\  
 +A better way would be to edit your .bash_profile or .profile file (depending on your distribution) to include the above command. That way, it would be done automatically every time you log in.\\  
 +\\  
 +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 bin and is a subdirectory of your home directory. If you do not already have one, create it with the following command:\\  
 +    mkdir bin 
 +\\  
 +Move your script into your new bin directory and you're all set. Now you just have to type:\\  
 +\\  
 +    hello_world 
 +\\  
 +Now your script will run. On some distributions, most notably Ubuntu, you will need to open a new terminal session before your newly created bin directory will be recognised. 
 +\\  
 +\\  
 +This content came from: 
 +\\  
 +    http://linuxcommand.org/lc3_wss0010.php 
 +\\  
 +\\ 
  
writing_a_shell_script.1470239063.txt.gz · Last modified: 2023/03/09 22:35 (external edit)