writing_a_shell_script
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
writing_a_shell_script [2016/08/03 16:44] – walkeradmin | writing_a_shell_script [2017/07/18 11:35] – walkeradmin | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Writing a Shell Script ====== | ====== Writing a Shell Script ====== | ||
+ | <color darkorange> | ||
\\ | \\ | ||
\\ | \\ | ||
- | ==== To successfully write a shell script, you have to do three things: ==== | + | \\ |
+ | ==== To successfully write a shell script | ||
\\ | \\ | ||
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: | ||
\\ | \\ | ||
- | | + | chmod 755 helloworld (sudo if you are using Raspbian) |
\\ | \\ | ||
The " | The " | ||
+ | \\ | ||
+ | \\ | ||
+ | At this point, your script will run. Try this: | ||
+ | \\ | ||
+ | ./ | ||
+ | \\ | ||
+ | You should see "Hello World!" | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== 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 (" | ||
+ | \\ | ||
+ | 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: | ||
+ | \\ | ||
+ | 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, | ||
+ | \\ | ||
+ | \\ | ||
+ | This content came from: | ||
+ | \\ | ||
+ | http:// | ||
+ | \\ | ||
+ | \\ | ||
writing_a_shell_script.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1