Description
Unix/Linux Commands and Basic Shell Programming
Some of the basic commands are:
- ls: lists all files and directories (try with options: -a, -al)
- cat: displays file content (try cat file1 file2 > file3)
- mv: moves a file to a new location (try mv file1 file2)
- rm: deletes a file
- cp: copy file
- cmp: compares two files
- man: gives help information on a command
- history: gives a list of past commands
- clear: clear the terminal
- mkdir: creates a new directory
- cd: changes directory
- rmdir: deletes a directory
- chmod: changes the access mode of the specified files to the specified mode
- chown: changes the owner of the specified files to the specified userid
- echo: writes arguments to the standard output (try echo ‘Hello World’ > myfile)
- df: shows disk usage
- apt -get: install and update packages
- mail -s ‘subject’ -c ‘cc-address’ -b ‘bcc-address’ ‘to-address’ < filename: sends email with attachment
- chown/ chmod: change ownership/ permission of file or directory
- date: show the current date and time
- ps: displays active processes
- kill: kills process
- sh: bourne shell – command interpreter (good to learn about shell programming)
- grep: searches for pattern in files
- Ctrl+c: halts current command
- Ctrl+z: stops current command and resumes with foreground
- Ctrl+d (exit): logout of current session
- man: displays the manual page of the specified command
- echo: prints on the screen
For more commands, please refer to Linux command quick reference[3].
Shell programming (aka scripting)
A shell program (aka script) is a text file (typically has .sh extension, but not required) that contains standard Unix and shell commands. It allows you to execute a series of commands in a shell program simply by running the shell program rather than typing all commands. Shell programs are interpreted not compiled. They are used to automate system administration tasks. In a shell program, you can use
- comments,
- variables,
- conditional commands,
- repeated actions of commands, and
Bourne Shell (bsh or sh) is used for this lab. Other Shells are C-Shell – csh, Korn Shell, Born Again Shell – BASH, Thomas C-Shell – tcsh.
- Comments: Use the # character to signify comments. Anything after a # character until the end of the line is considered a comment and is ignored by the shell.
- Variables: Use letters, numbers, and the underscore to define variable names is a shell program. The assigned values to variables are stored internally as strings. To use a variable, precede the name with $. The shell provides the following pre-defined shell variables that may be used to pass parameters to a shell script.
$0 the name of the shell program
$1 thru$9 the first thru to ninth parameters
$# the number of parameters
$* all parameters passed represented as a single word with individual parameters separated
$@ all the parameters passed with each parameter as a separate word
$? hold the exit status of the previous command
$$ the process id of the current process
PATH the value of the PATH environment variable
HOME the full path name of your home directory
USER your user name
PWD the current directory path
- Conditional commands: Use if keyword for a conditional statement in either of the following arrangements:
if expression
then
command-list
fi
if expression
then
command-list
elsif expression
command-list2
fi
if expression
then
command-list1
else
command-list2
fi
case keyword can also be used to execute one of several lists of statements depending on the value of a variable.
Expressions (Boolean):
- Relational operators:
-eq, -ne, -gt, -ge, -lt, -le
- File operators:
-f file True if file exists and is not a directory
-d file True if file exists and is a directory
-s file True if file exists and has a size > 0
- String operators:
-z string True if the length of string is zero
-n string True if the length of string is nonzero
s1 = s2 True if s1 and s2 are the same
s1 != s2 True if s1 and s2 are different
s1 True if s1 is not the null string
- Compound comparison:
-a And
-o Or
! Not
The other type of conditional command supported by the shell is the case command. The case command allows the user to compare a single value against multiple values and when a match is found execute the associated command list.
- Repeated actions of commands: The Bourne shell provides three repeated action commands: for, while, and until as follows:
for variable in word1 word2 word3 … wordn
do
command-list
done
while command
do
command-list
done
until command
do
command-list
done
- Functions: shell functions are generally defined in a file as:
name () { add (){
commands; e.g. echo $[$1 + $2]
} }
Important notes:
- Shell programming is not good in numerical computation, but use can still use some mathematical expressions by using the keyword expr, e.g. i=‘expr $i+1’ or you may close expressions in brackets, e.g. i=$[i+1]
- Use #!/bin/sh as the first line of a shell program to define the path of the command interpreter.
- Use chmod +x <program_name> to make a shell program executable, then run either using sh <program_name>, or ./<program_name>
- Shell metacharacters are:
‘…’ takes without interpreting contents
“…” takes after processing $, `…` and \
\ escape, for example \c takes character c
`…` runs enclosed command and replace with output
Sample shell program
Demonstrate each of the following steps to the TA to get a grade on this part of the lab assignment
- Write the following shell program using vi, emacs, or an editor of your choice
#Sample shell programs for Lab assignment
#!/bin/sh
echo Executing $0
echo $(/bin/ls | wc -l) files
wc -l $(/bin/ls)
echo “HOME=”$HOME
echo “USER=”$USER
echo “PATH=”$PATH
echo “PWD=”$PWD
echo “\$\$”=$$
user=`whoami`
numusers=`who | wc -l`
echo “Hi $user! There are $numusers users logged on.”
if [ $user = “salagtash” ]
then
echo “Now you can proceed!”
else
echo “Check who logged in!”
exit 1
fi
response=”Yes”
while [ $response != “No” ]
do
echo “Enter height of rectangle: ”
read height
echo “Enter width of rectangle: ”
read width
area=`expr $height \* $width`
echo “The area of the rectangle is $area”
echo “Would you like to repeat for another rectangle [Yes/No]?”
read response
done
- Run the shell program by typing sh <YourProgram.sh>. When it runs without errors or warnings, write down your observations in detail and make a copy of the source file.
- Rewrite the program in Step 1. so that you compute also the area of a circle, then demonstrate steps 1 – 3
When your program runs without errors or warnings, make a copy of the source file



