Introduction to vi
Linux Fundamentals
Paul Cobbaut
1
²
Part VII. scripting
Chapter 23. scripting introduction
Shells like bash and Korn have support for programming constructs that can be saved as
scripts. These scripts in turn then become more shell commands. Many Linux commands
are scripts. User profile scripts are run when a user logs on and init scripts are run when a
daemon is stopped or started.
This means that system administrators also need basic knowledge of scripting to understand
how their servers and their applications are started, updated, upgraded, patched, maintained,
configured and removed, and also to understand how a user environment is built.
The goal of this chapter is to give you enough information to be able to read and understand
scripts. Not to become a writer of complex scripts.
3
scripting introduction
23.1. prerequisites
You should have read and understood part III shell expansion and part IV pipes and
commands before starting this chapter.
23.2. hello world
Just like in every programming course, we start with a simple hello_world script. The
following script will output Hello World.
echo Hello World
After creating this simple script in vi or with echo, you'll have to chmod +x hello_world to
make it executable. And unless you add the scripts directory to your path, you'll have to type
the path to the script for the shell to be able to find it.
[paul@RHEL4a ~]$ echo echo Hello World > hello_world
[paul@RHEL4a ~]$ chmod +x hello_world
[paul@RHEL4a ~]$ ./hello_world
Hello World
[paul@RHEL4a ~]$
23.3. she-bang
Let's expand our example a little further by putting #!/bin/bash on the first line of the script.
The #! is called a she-bang (sometimes called sha-bang), where the she-bang is the first
two characters of the script.
#!/bin/bash echo
Hello World
You can never be sure which shell a user is running. A script that works flawlessly in bash
might not work in ksh, csh, or dash. To instruct a shell to run your script in a certain shell,
you can start your script with a she-bang followed by the shell it is supposed to run in. This
script will run in a bash shell.
#!/bin/bash
echo -n hello
echo A bash subshell `echo -n hello`
This script will run in a Korn shell (unless /bin/ksh is a hard link to /bin/bash). The /etc/
shells file contains a list of shells on your system.
#!/bin/ksh
echo -n hello
echo a Korn subshell `echo -n hello`
23.4. comment
Let's expand our example a little further by adding comment lines.
4
scripting introduction
#!/bin/bash
#
# Hello World Script #
echo Hello World
23.5. variables
Here is a simple example of a variable inside a script.
#!/bin/bash
#
# simple variable in script
var1=4
echo var1 = $var1
Scripts can contain variables, but since scripts are run in their own shell, the variables do
not survive the end of the script.
[paul@RHEL4a ~]$ echo $var1
[paul@RHEL4a ~]$ ./vars
var1 = 4
[paul@RHEL4a ~]$ echo $var1
[paul@RHEL4a ~]$
23.6. sourcing a script
Luckily, you can force a script to run in the same shell; this is called sourcing a script.
[paul@RHEL4a ~]$ source ./vars
var1 = 4
[paul@RHEL4a ~]$ echo $var1
4
[paul@RHEL4a ~]$
The above is identical to the below.
[paul@RHEL4a ~]$ . ./vars
var1 = 4
[paul@RHEL4a ~]$ echo $var1
4
[paul@RHEL4a ~]$
23.7. troubleshooting a script
Another way to run a script in a separate shell is by typing bash with the name of the script
as a parameter.
5
scripting introduction
paul@debian6~/test$ bash runme
42
Expanding this to bash -x allows you to see the commands that the shell is executing (after
shell expansion).
paul@debian6~/test$ bash -x runme
+ var4=42
+ echo 42 42
paul@debian6~/test$ cat runme
# the runme script
var4=42
echo $var4
paul@debian6~/test$
Notice the absence of the commented (#) line, and the replacement of the variable before
execution of echo.
23.8. prevent setuid root spoofing
Some user may try to perform setuid based script root spoofing. This is a rare but possible
attack. To improve script security and to avoid interpreter spoofing, you need to add -- after
the #!/bin/bash, which disables further option processing so the shell will not accept any
options.
#!/bin/bash -
or
#!/bin/bash --
Any arguments after the -- are treated as filenames and arguments. An argument of - is
equivalent to --.
6
Chapter 24. scripting loops
7
scripting loops
24.1. test [ ]
The test command can test whether something is true or false. Let's start by testing whether
10 is greater than 55.
[paul@RHEL4b ~]$ test 10 -gt 55 ; echo $?
1
[paul@RHEL4b ~]$
The test command returns 1 if the test fails. And as you see in the next screenshot, test
returns 0 when a test succeeds.
[paul@RHEL4b ~]$ test 56 -gt 55 ; echo $?
0
[paul@RHEL4b ~]$
If you prefer true and false, then write the test like this.
[paul@RHEL4b ~]$ test 56 -gt 55 && echo true || echo false
true
[paul@RHEL4b ~]$ test 6 -gt 55 && echo true || echo false
false
The test command can also be written as square brackets, the screenshot below is identical
to the one above.
[paul@RHEL4b ~]$ [ 56 -gt 55 ] && echo true || echo false
true
[paul@RHEL4b ~]$ [ 6 -gt 55 ] && echo true || echo false
false
Below are some example tests. Take a look at man test to see more options for tests.
[ -d foo ] Does the directory foo exist ?
[ -e bar ] Does the file bar exist ?
[ '/etc' = $PWD ] Is the string /etc equal to the variable $PWD ?
[ $1 != 'secret' ] Is the first parameter different from secret ?
[ 55 -lt $bar ] Is 55 less than the value of $bar ?
[ $foo -ge 1000 ] Is the value of $foo greater or equal to 1000 ?
[ "abc" < $bar ] Does abc sort before the value of $bar ?
[ -f foo ] Is foo a regular file ?
[ -r bar ] Is bar a readable file ?
[ foo -nt bar ] Is file foo newer than file bar ?
[ -o nounset ] Is the shell option nounset set ?
Tests can be combined with logical AND and OR.
paul@RHEL4b:~$ [ 66 -gt 55 -a 66 -lt 500 ] && echo true || echo false
true
paul@RHEL4b:~$ [ 66 -gt 55 -a 660 -lt 500 ] && echo true || echo false
false
paul@RHEL4b:~$ [ 66 -gt 55 -o 660 -lt 500 ] && echo true || echo false
true
8
scripting loops
24.2. if then else
The if then else construction is about choice. If a certain condition is met, then execute
something, else execute something else. The example below tests whether a file exists, and
if the file exists then a proper message is echoed.
#!/bin/bash
if [ -f isit.txt ]
then echo isit.txt exists!
else echo isit.txt not found!
fi
If we name the above script 'choice', then it executes like this.
[paul@RHEL4a scripts]$ ./choice
isit.txt not found!
[paul@RHEL4a scripts]$ touch isit.txt
[paul@RHEL4a scripts]$ ./choice
isit.txt exists!
[paul@RHEL4a scripts]$
24.3. if then elif
You can nest a new if inside an else with elif. This is a simple example.
24.4. for loop
The example below shows the syntax of a classical for loop in bash.
for i in 1 2 4
do
echo $i
done
9
scripting loops
using the bash {from..to} shorthand.
This for loop uses file globbing (from the shell expansion). Putting the instruction on the
command line has identical functionality.
24.5. while loop
Below a simple example of a while loop.
Endless loops can be made with while true or while : , where the colon is the equivalent of
no operation in the Korn and bash shells.
24.6. until loop
Below a simple example of an until loop.
24.7. practice: scripting tests and loops
1. Write a script that uses a for loop to count from 3 to 7.
2. Write a script that uses a for loop to count from 1 to 17000.
3. Write a script that uses a while loop to count from 3 to 7.
4. Write a script that uses an until loop to count down from 8 to 4.
5. Write a script that counts the number of files ending in .txt in the current directory.
10
scripting loops
6. Wrap an if statement around the script so it is also correct when there are zero files ending
in .txt.
11
Chapter 25. scripting parameters
12
scripting parameters
25.1. script parameters
A bash shell script can have parameters. The numbering you see in the script below
continues if you have more parameters. You also have special parameters containing the
number of parameters, a string of all of them, and also the process id, and the last return
code. The man page of bash has a full list.
#!/bin/bash
echo The first argument is $1
echo The second argument is $2
echo The third argument is $3
echo \$ $$ PID of the script
echo \# $# count arguments
echo \? $? last return code
echo \* $* all the arguments
Below is the output of the script above in action.
[paul@RHEL4a scripts]$ ./pars one two three
The first argument is one
The second argument is two
The third argument is three
$ 5610 PID of the script
# 3 count arguments
? 0 last return code
* one two three all the arguments
Once more the same script, but with only two parameters.
[paul@RHEL4a scripts]$ ./pars 1 2
The first argument is 1
The second argument is 2
The third argument is
$ 5612 PID of the script
# 2 count arguments
? 0 last return code
* 1 2 all the arguments
[paul@RHEL4a scripts]$
Here is another example, where we use $0. The $0 parameter contains the name of the script.
paul@debian6~$ cat myname
echo this script is called $0
paul@debian6~$ ./myname
this script is called ./myname
paul@debian6~$ mv myname test42
paul@debian6~$ ./test42
this script is called ./test42
13
scripting parameters
25.2. shift through parameters
25.3. runtime input
You can ask the user for input with the read command in a script.
#!/bin/bash
echo -n Enter a number:
read number
25.4. sourcing a config file
The source (as seen in the shell chapters) can be used to source a configuration file.
Below a sample configuration file for an application.
[paul@RHEL4a scripts]$ cat myApp.conf
# The config file of myApp
# Enter the path here
myAppPath=/var/myApp
14
scripting parameters
# Enter the number of quines here
quines=5
And here an application that uses this file.
[paul@RHEL4a scripts]$ cat myApp.bash
#!/bin/bash
#
# Welcome to the myApp application
# .
./myApp.conf
echo There are $quines quines
The running application can use the values inside the sourced configuration file.
[paul@RHEL4a scripts]$ ./myApp.bash
There are 5 quines
[paul@RHEL4a scripts]$
25.5. get script options with getopts
The getopts function allows you to parse options given to a command. The following script
allows for any combination of the options a, f and z.
This is sample output from the script above. First we use correct options, then we enter twice
an invalid option.
15
scripting parameters
kahlan@solexp11$ ./options.ksh
kahlan@solexp11$ ./options.ksh -af
received -a received -f
kahlan@solexp11$ ./options.ksh -zfg
received -z received -f
invalid option -g
kahlan@solexp11$ ./options.ksh -a -b -z
received -a
invalid option -b
received -z
25.7. practice: parameters and options
1. Write a script that receives four parameters, and outputs them in reverse order.
2. Write a script that receives two parameters (two filenames) and outputs whether those
files exist.
16
Chapter 26. more scripting
17
Index
26.1. eval
eval reads arguments as input to the shell (the resulting commands are executed). This
allows using the value of a variable as a variable.
kahlan@solexp11$ answer=42
kahlan@solexp11$ word=answer
kahlan@solexp11$ eval "y=\$$word" ; echo $y
42
26.2. (( ))
The (( )) allows for evaluation of numerical expressions.
paul@deb503:~/test42$ (( 42 > 33 )) && echo true || echo false
true
paul@deb503:~/test42$ (( 42 > 1201 )) && echo true || echo false
false
paul@deb503:~/test42$ var42=42
paul@deb503:~/test42$ (( 42 == var42 )) && echo true || echo false
true
paul@deb503:~/test42$ (( 42 == $var42 )) && echo true || echo false
true
paul@deb503:~/test42$ var42=33
paul@deb503:~/test42$ (( 42 == var42 )) && echo true || echo false
false
26.3. let
The let built-in shell function instructs the shell to perform an evaluation of arithmetic
expressions. It will return 0 unless the last arithmetic expression evaluates to 0.
[paul@RHEL4b ~]$ let x="3 + 4" ; echo $x
7
[paul@RHEL4b ~]$ let x="10 + 100/10" ; echo $x
20
[paul@RHEL4b ~]$ let x="10-2+100/10" ; echo $x
18
[paul@RHEL4b ~]$ let x="10*2+100/10" ; echo $x
30
The shell can also convert between different bases.
[paul@RHEL4b ~]$ let x="0xFF" ; echo $x
255
[paul@RHEL4b ~]$ let x="0xC0" ; echo $x
192
[paul@RHEL4b ~]$ let x="0xA8" ; echo $x
18
Index
168
[paul@RHEL4b ~]$ let x="8#70" ; echo $x
56
[paul@RHEL4b ~]$ let x="8#77" ; echo $x
63
[paul@RHEL4b ~]$ let x="16#c0" ; echo $x
192
There is a difference between assigning a variable directly, or using let to evaluate the
arithmetic expressions (even if it is just assigning a value).
kahlan@solexp11$ dec=15 ; oct=017 ; hex=0x0f
kahlan@solexp11$ echo $dec $oct $hex
15 017 0x0f
kahlan@solexp11$ let dec=15 ; let oct=017 ; let hex=0x0f
kahlan@solexp11$ echo $dec $oct $hex
15 15 15
19
Index
26.4. case
20
Index
26.5. shell functions
Shell functions can be used to group commands in a logical way.
This is sample output from this script with a function.
kahlan@solexp11$ ./funcs.ksh
We will now call a function
Hello World!
and hello to kahlan to!
The end
A shell function can also receive parameters.
kahlan@solexp11$ cat addfunc.ksh
#!/bin/ksh
function plus {
let result="$1 + $2"
echo $1 + $2 = $result
plus 3 10
plus 20 13
plus 20 22
This script produces the following output.
kahlan@solexp11$ ./addfunc.ksh
3 + 10 = 13
20 + 13 = 33
20 + 22 = 42
26.6. practice : more scripting
1. Write a script that asks for two numbers, and outputs the sum and product (as shown
here).
21
Index
Enter a number: 5
Enter another number: 2
Sum: 5 + 2 = 7
Product: 5 x 2 = 10
2. Improve the previous script to test that the numbers are between 1 and 100, exit with
anerror if necessary.
3. Improve the previous script to congratulate the user if the sum equals the product.
22
Index
Exercise 1: Hello World
Create a simple shell script that prints "Hello, World!" to the terminal when executed.
Exercise 2: Variables and Input
Write a script that takes user input for their name and age, then prints a greeting
message with their name and age.
Exercise 3: File Operations
Write a script that checks if a file exists. If it does, print a message saying it exists;
otherwise, create the file and print a message.
Exercise 4: Looping
Create a script that uses a loop to print numbers from 1 to 5.
Exercise 5: Conditional Statements
Write a script that asks the user to enter a number and then checks if it's even or
odd.
Exercise 6: Command Line Arguments
Create a script that takes two numbers as command line arguments and prints their
sum.
Exercise 7: Functions
Write a script that defines a function to calculate the square of a number. Then, call
the function with different numbers.
Exercise 8: String Manipulation
Create a script that takes a sentence as input and prints the number of words in it.
Exercise 9: Arrays
Write a script that declares an array of fruits and prints each fruit on a new line.
Exercise 10: Reading from a File
Create a script that reads a list of names from a file and prints them.
23
Index
Exercise 11: Case Statement
Write a script that uses a case statement to determine if a user's input is a weekday
or weekend day.
Exercise 12: Math Operations
Create a script that performs basic arithmetic operations (addition, subtraction,
multiplication, and division) on two numbers entered by the user.
Exercise 13: Logging
Write a script that logs the current date and time to a file.
Exercise 14: Advanced File Operations
Create a script that renames all files in a directory with a specific extension to have a
".bak" extension.
Exercise 15: Error Handling
Enhance one of your existing scripts to include error handling. For example, check if a
file exists before trying to read it.
24