Linux shell script is a text file with set execute permission (x). It may contain solely a sequence of shell commands or some additional control instructions making it more of a program structure.
Linux shell script is being interpreted by Linux shell, for example BASH shell or C shell.
What is a Linux shell script?
Use any text editor to edit the script file.
Change the created script file permission (default permissions = 644, should actually be 755).
Copy the script file into one of the directories listed on PATH variable, can check this using echo $PATH
Run the script file by typing in the command line.
Script_file_name or ./script_file_name
How to create a Linux shell script?
Keywords
Special Variables
System Variables
Linux shell scripts
$0
The script name.
$1...$9
The command line argument.
$*
List of all command line arguments.
$@
Table of all command arguments.
$?
Status of the last executed command.
$#
Number of all arguments the script was executed with.
Special Variables
SHELL
System shell
USER
User name
HOME
Home user directory
PS1
Shell command prompt
EDITOR
Default editor
PATH
Default directories list
MAIL
Default mail directory
System Variables
Integer variables:
x = 3
String variables:
str = "a string" or str = 'a string'
Types of variables
Variables do not have to be declared - first assignment of a value to a variable makes it declared.
x = 3
In order to refer to the variable a $ character has to be added at the beginning of the variable name.
Y = $X
The first line of the script is used to call bash shell for the remaining part of the script execution.
Calling the right shell at the beginning of the script guarantees that the remaining part of the script will execute correctly.
Variables
Integer variables may be a subject of different arithmetic operations:
+, addition
-, subtraction
*, multiplication
/, division
%, division (modulo)
Arithmetic operations can be performed using two commands 'let' and 'expr' or $[] expression.
Arithmetic operations
A table can be assigned as:
table = (value1 value2 ... valueN)
or
table[0] = 1
table[1] = 3 ...
table[N] = N
Tables do not have to be continuous.
To delete a selected table element the unset command is used:
unset table[2]
By calling unset table[*] or unset table[@] the table will be deleted as a whole.
Tables
For, while, until
Loop instructions
If, case
Condition instructions
test
test instruction
Control instructions
If condition
then
command
fi
If condition
then
command1
else
command2
fi
If condition
then
command1
elif condition
command2
else
command3
fi
case $variable if
value1) command1;
value2) command2;
) command;;
esac
for VARIABLE in 1 2 3 4 5 ... N
do
command1
command2
commandN
done
while [condition]
do
command1
command2
command3
done
The condition is treated as a continuation condition unless the condition is false.
While
test: logical test instruction
syntax: test condition
Test relations between integers.
Test relations between strings.
Test files information.
Test
Relations between integers:
lt, less than, [$# -lt 2]
gt, greater than, [$a -gt $b]
le, lesser equal, [$a -le $1]
ge, greater equal, [$a -ge 2]
eq, equal, [$a -eq $b]
ne, not equal, [$c -ne 0]
Test relations between integers
Relations between string:
==, equal, ["$a" == "q"]
!=, not equal, ["$a" != "x"]
-n, empty, [-n "$a"]
-z, not empty, [-z "$1"]
Test relations between strings
Files information:
-a, file exists.
-b, file exists and is a special block file.
-c, file exists and is a special character file.
-e, file exists.
-L, file exists and is a symbolic link.
-d, file exists and is a directory.
-r, read right set.
-w, write right set.
-x, execution right set.
-f, file exists and is an ordinary file.
-p, file exists and is a pipe.
-N, file exists and was modified since its last load.
Test files information
In a Windows system all text files with '.bat' extension are assumed to be script files and can be executed.
The batch files may contain definitions of variables, control instructions, so they can be used for shell programming.
Batch files
New variables can be instantiated like:
set name = value
The name must only be made up of alphabetic characters, numeric characters and underscores, it cannot begin with a numeric character.
Variables are referenced as:
%name%
Variables
Command line arguments are treated as special variables within the batch script.
%0 = the name of the batch file itself.
%1 = the first argument.
%2 = the second argument.
To reference after the ninth argument you must use the shift command to shift the arguments 1 variable to the left.
Command line arguments
IF [NOT] ERRORLEVEL number command
IF [NOT] string1 == string2 command
IF [NOT] EXIST filename command
If you want to include more than one command, enclose it within brackets:
@ echo off
if exist hello.bat(
echo Removing hello.bat...
del hello.bat
echo hello.bat removed!
)
Control Instructions - If
FOR %%variable IN (set) DO command [command-parameters]
@ echo off
if exist bigtxt.txt rename bigtxt.txt bigtxt
for %%f in (*.txt) do type %%f >> bigtxt
rename bigtxt bigtxt.txt
A more useful option of the FOR construct is to use the option /L which turns in to a numeric mode whereby the following syntax applies.
FOR /L %%variable IN (start,step,end) DO command [command-parameters].