Reference no: EM133140285
KIT501 ICT Systems Administration Fundamentals - University of Tasmania
Goal
The main purpose of this practical is to give you further familiarity with the UNIX shell (in particular, bash) and start to write your first scripts.
1. UNIX Shell Scripting/Programming Concept
Hands on exercises - starting with a simple script
A Login to your ictteach account. Change into kit501 directory.
Remove any existing files or subdirectories stored under kit501 (unless you really want to keep them).
B Use joe or nano to create a text file which must be named as info.sh, with the following file content:
# My first shell script # I'm a little excited ...
echo The date today is `date`
echo The current directory is `pwd` echo My home directory is
cd pwd
echo The current users are users
(Note: press the Enter/Return key at the end of the last line)
Exit the text editor by saving the file content.
You have now created your first UNIX shell script/program.
C Unlike in C or Java programming, you do NOT need to compile a shell program. Additionally, the extension (.sh) in the file name is optional, but having this extension helps you identify the type of this file (which is a shell script).
D You can try to execute this program by simply typing the shell script name:
$ ./info.sh
info.sh: permission denied
The reason for the above error message is that, by default, there is no x (execute) permission for newly created text files:
$ ls -l info.sh
-rw-r--r-- 1 sxu staff 78 Apr 11 16:33 info.sh
So you will need to assign x permission to it manually:
$ chmod u+x info.sh
$ ls -l info.sh
-rwxr--r-- 1 sxu staff 78 Apr 11 16:34 info.sh
Now you can run it. The following is a sample output:
$ ./info.sh
The date today is Mon Apr 11 16:39:48 EST 2022 The current directory is /u/student/jchen/KIT501 My home directory is
/u/student/jchen
The current users are cmcgee john tgray
Compare your output of the script against each command line contained within the script content. Can you see that each command line has been executed one after another?
E Can you guess the purpose of the first two lines in this shell script?
# My first shell script # I'm a little excited ...
These are comment lines which help users understand the purpose of this script. Comment lines are for humans to read. They are ignored by UNIX shells - everything after a # character is ignored on a line, so it's good practice to leave helpful comments explaining what sections of your script do (and it is mandatory when you write your scripts for the scripting assignment).
2. UNIX Shell Variables
Hands on exercises
A Create the following variables named x and y respectively, and then evaluate them. Note that, in each line, the first $ shown is the shell prompt.
$ x=foo
$ echo $x foo
$ y=.doc
$ echo $y
.doc
You can use the command unset to remove variables:
$ unset x
$ echo $x
$ unset y
$ echo $y
You can use shell variables to construct file names.
$ x=foo
$ y=.doc
$ z=$x$y
$ echo $z foo.doc
$ unset x
$ unset y
$ unset z
B Let's create more shell variables:
$ msg1='You have mail' (note the single quotes)
$ echo $msg1 You have mail
$ msg2=You\ have\ more\ mail
$ echo $msg2
You have more mail
Why is it necessary to use single quotes or \ in the above variable creation? The reason is that the whitespace on a UNIX shell is also a special character. Try the following:
$ msg=You have mail have: command not found
$ echo $msg
Here the shell understands that you try to assign You as value to a variable named msg, and then run the have command. Given that there is no such command named have, the command line has failed.
C Let's create the shell variable named msg again:
$ msg='You have mail'
$ echo $msg You have mail
$ echo "$msg" You have mail
$ echo '$msg'
$msg
Here you see the other difference between the use of single quotes and double quotes. Double quotes allow the use of $ to evaluate shell variables. Single quotes do not.
To summarise the difference between single quotes and double quotes on UNIX:
Enclosing a group of characters in single or double quotes can remove the special meaning of the characters. Though the shell ignores all special characters enclosed in single quotes, double quotes permit using $ to evaluate a variable and back-quote ` for command substitution.
The following exercises reinforces the above summary.
D $ echo The newline character is \n
$ echo 'The newline character is \n'
$ echo "The newline character is \n"
$ echo There are `who | wc -l` users on the system
$ echo "There are `who | wc -l` users on the system"
$ echo 'There are `who | wc -l` users on the system'
$ msg='We will depart at 10AM'
$ echo The message is $msg
$ echo The message is "$msg"
$ echo The message is '$msg'
3. Specifying a Shell, and Interactive Shell Scripts
Hands on exercises
A Add the following line to the beginning of your info.sh script:
#! /bin/sh
Save and run this script again. From now on, this script will also run on the Bourne shell, regardless of what shell you are currently using. This is useful because there are differences between UNIX shells. A script written to run on one shell may not run as expected on a different shell. Even though this line is a comment (it starts with a #), if it is the first line of a script, the shell interpreter will see the special sequence (#!) that specifies exactly what shell should be used to run the commands. In this case, the Bourne shell (sh) which can be found in the /bin directory.
B You can make shell scripts interactive, using the read command. The script will pause and wait for input (specifically from standard input) whenever it encounters a read command. The input entered will be stored in a variable (the variable's name must follow the read command).
Create the following shell script (see next page). Save and name it as emp1.sh:
#! /bin/sh
# This is an interactive script
echo -e "Enter the pattern to be searched: \c" read pname
echo -e "Enter the file to be used: \c" read flname
echo -e "Searching for $pname from $flname\n" grep "$pname" $flname
echo -e "\nSelected lines shown above"
Assign execute (x) permission to this file.
The script prompts the user to enter a keyword, and then searches through a record file to find all the entries which contain the keyword. The first echo command prompts the user to enter a keyword. The first read command stores the user input into the variable pname.
The second echo command prompts the user to enter a record filename. The second read command stores the user input into the variable flname. The grep command searches the specified file for lines that include the pattern entered and each line is then displayed.
? Note the use of the option -e and \c and \n for the echo command. The option -e with \c places the cursor at the end of the message line to accept user input (instead of the input being entered on the next line which looks bad). The \n is used to generate a new blank line which improves the layout of an output.
C To correctly run this script, you need a support file that the script can use for searching. Create a new text file which must be named as emp2.lst, with the following content:
2000, John Warren, NSW
2001, Adam Davis, NSW 3000, John Smith, ACT 3001, Jenny Gray, ACT 7000, Elton John, TAS 7001, Sam Jones, TAS
D Run the emp1.sh script as follows. Ensure that you understand each line in the output.
$ ./emp1.sh
Enter the pattern to be searched: John (example user input) Enter the file to be used: emp2.lst (example user input) Searching for John from emp2.lst
2000, John Warren, NSW 3000, John Smith, ACT 7000, Elton John, TAS
Selected lines shown above
E Please note that the script allows you to search for a pattern with multiple words (such as
John Smith).
$ ./emp1.sh
Enter the pattern to be searched: John Smith (eg user input) Enter the file to be used: emp2.lst (eg user input) Searching for John Smith from emp2.lst
3000, John Smith, ACT Selected lines shown above
The reason is in the following statement of this script:
grep "$pname" $flname
The use of double quotes here allows you to enter any string of characters as value for the pname variable. If you remove the double quotes in this statement, and run the script as above again, you would get unexpected output.
4. Positional Parameters (command line arguments)
A Write the following shell script and save it as pospar.sh #! /bin/sh
echo "name of the shell script: $0" echo "the number of arguments: $#" echo "the first argument is: $1" echo "the second argument is: $2" echo "the third argument is: $3"
echo "the complete set of arguments is: $*"
B Assign x permission to the script, and then run the script using each of the following
command lines. Do you understand the output of each command line?
$ ./pospar.sh
$ ./pospar.sh ab
$ ./pospar.sh ab cd
$ ./pospar.sh ab cd ef
$ ./pospar.sh ab cd ef gh
$ ./pospar.sh ab "cd ef" gh
$ ./pospar.sh "ab cd" "ef gh"
5. Exit Status of a Command
A Following the previous exercises you should still have the file info.sh stored under your current directory.
$ cat info.sh
$ echo $?
Ensure that you do not have a file named fool stored under the current directory. Do the following:
$ cat fool
$ echo $?
B Following the previous exercises you should still have the file emp2.lst stored under your current directory. Ensure that you understand the outputs of the following command lines:
$ grep John emp2.lst >/dev/null; echo $?
$ grep Jack emp2.lst >/dev/null; echo $?
What is the purpose of the >/dev/null in these command lines?
6. Logical Operators
We can use a command's exit status to control the flow of a shell program. This involves using the following logical operators:
&& think of this as 'and'
|| think of this as 'or'
cmd1 && cmd2 - cmd2 is executed only when cmd1 succeeds
cmd1 || cmd2 - cmd2 is executed only when cmd1 fails
Hands on exercises
A Run the following command lines and think about their outputs:
$ grep John emp2.lst && echo "Pattern found" 2000, John Warren, NSW
3000, John Smith, ACT 7000, Elton John, TAS Pattern found
$ grep John emp2.lst || echo "Pattern not found" 2000, John Warren, NSW
3000, John Smith, ACT 7000, Elton John, TAS
$ grep Jack emp2.lst && echo "Pattern found"
$ grep Jack emp2.lst || echo "Pattern not found" Pattern not found
Why is it that there is no output for the third command line here?
Attachment:- Systems Administration Fundamentals.rar