|
|
# Shell Scripting Student Handout
|
|
|
|
|
|
## REVIEW OF COMMAND LINE
|
|
|
The special characters are:
|
|
|
metacharacter
|
|
|
A character that, when unquoted, separates words. One of the following:
|
|
|
| & ; ( ) < > space tab newline
|
|
|
control operator
|
|
|
A token that performs a control function. It is one of the following symbols:
|
|
|
|| & && ; ;; ;& ;;& ( ) | |& <newline>
|
|
|
\ – used to indicate a continuation of the command line on the next line. Used to make scripts and commands legible
|
|
|
& – indicates that this is the end of a line and the command is executed in the background
|
|
|
> – redirect output to whatever follows, a file descriptor
|
|
|
< – redirect input from whatever follows, a file descriptor
|
|
|
>> – redirect output and append to file that follows, not useful for commands
|
|
|
2> – redirect error output to whatever follows, can also use &>=
|
|
|
| – send output to input of the next command
|
|
|
|& – send output and error to input of next command
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 ELEMENTS OF SCRIPTS
|
|
|
2.1 First line
|
|
|
First line of a script file points to the program to use to run the script. For BASH scripts, use
|
|
|
#!/bin/bash
|
|
|
|
|
|
Simple program
|
|
|
#!/bin/bash
|
|
|
echo Hello World
|
|
|
2.2 Variables
|
|
|
Variables are defined by giving a name and equals sign. The variable scope is the script execution and does not impact any other program or script. If the variable is needed by another script you can use the command export to make the command available.
|
|
|
2.3 Decisions
|
|
|
|
|
|
3 RUNNING SCRIPTS
|
|
|
There are two methods of running scripts. You can execute a script by using bash in front of the filename of the script followed by the arguments.
|
|
|
bash filename [arguments]
|
|
|
|
|
|
The other method is to change the execute bit of the file using chmod
|
|
|
Chmod u+x filename
|
|
|
|
|
|
Which makes the filename executable by the user. If you want group or everyone to be able to execute the file then use g or o instead of u.
|
|
|
Then the file can be executed by entering the filename. Unless the file is in the PATH variable then you will need to specify the location of the file. If you are executing it in the same directory you are in you will need to preface the name with ./ to indicate the current directory.
|
|
|
./filename [arguments]
|
|
|
/<path>/filename [arguments]
|
|
|
filename [arguments]
|
|
|
|
|
|
4 EXAMPLES
|
|
|
|
|
|
5 RESOURCES
|
|
|
https://www.cyberciti.biz/open-source/learning-bash-scripting-for-beginners/
|
|
|
List of resources for beginning scripters
|
|
|
|