|
Part 1: New Linux Techniques Create and execute a script file
|
bet | 2/10 | Sana | 15.03.2017 | Hajmi | 36,92 Kb. | | #53 |
Part 1: New Linux Techniques Create and execute a script file
Note: the instructions say to use vi as your editor. Substitute your version of vi (vim or vim-tiny) as needed.
A script file is basically a small, low level program. The commands in the script file are executed by the shell (bash in this lab). Scripts are handy to automate tasks. Some of the things that can be done by a script:
-
make decisions (using if statements)
-
loop
-
call other scripts to be executed
-
read and write files
-
execute system commands
Useful information:
-
Parameters passed to a script are denoted by $1, $2, $3, … where $1 is the 1st parameter, $2 is the 2nd, etc.
-
The number of parameters is $#
-
Conditionals are done by an if…elif…else…fi structure
-
The if and elif are followed by a command that evaluates to true or false
-
The elif and else are optional
-
elif can be repeated
-
Only one else may be used, if needed
-
The if is closed with the fi statement
-
To stop a script use exit n
-
n = 0 is a normal exit
-
n = 1 is an error exit
-
If a value needs to be checked the test command is used
-
Numbers use conditionals like –gt, -lt, and -eq (greater than, less than, equal)
-
Strings use = or != (equal and not equal)
-
There are other comparisons that can be done, check the internet
-
Variables are case sensitive, and are UPPER case by convention for environment variables
|
| |