Tash SCRKT
Tash SCRKT
Tash SCRKT
1 of 50
UNIX
BASH Programming
Topics:
Shells
Running a BASH Script
Shell Variables and Related Commands
Passing Arguments to Shell Scripts
Program Control Flow Commands (if)
Arithmetic Operators
2 of 50
Shell Scripts
The shell script is a file that contains series of commands
to be executed by the shell.
There are three ways to execute Bash shell script:
o By making the shell script file an executable file using
chmod command,
o By running the /bin/bash command with the script file as
its parameter (/bin/bash script_file), or
o Begin a shell script with #!/bin/bash.
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
3 of 50
Command: echo
Purpose
Flags
\b
\c
\f
\n
\t
echo[flags][file-list]
Print-to-screen function
Moves already printed text back one space.
Forces the following command to appear on the same
line as the current command.
Forces the following command to appear on the next
line at a specified horizontal location.
Forces the following command to appear on a new line.
Indents the following command output by one tab.
4 of 50
Example:
$cat whoson
#!/bin/bash
date
echo Users Currently Logged In
who
$chmod u+x whoson
$whoson
Fri Jun 17 10:59:40 PDT 1994
Users Currently Logged In
alex
tty1
Jun 17 08:26
Jenny
tty02
Jun 17 10:04
5 of 50
6 of 50
7 of 50
8 of 50
$0
$1 - $9
$*
$@
$#
$$
$?
$!
9 of 50
arg2=$2
arg3=$3
arg2=$2
arg3=$3
10 of 50
The set command. When you call set command with one
or more arguments, it sets the values of the command line
argument variables ($1 - $9) to its arguments.
The set command may be used to cause it to use the
standard output of another command as its arguments.
11 of 50
$date
Fri Jun 17 23:04:09 PDT 1996
$cat dateset
set `date`
echo $*
echo Argument 1: $1
echo Argument 2: $2
echo Argument 4: $4
echo $2 $3, $6
$dataset
Fri Jun 17 23:04:09 PDT 1996
Argument 1: Fri
Argument 2: Jun
Argument 4: 23:04:13
Jun 17, 1996
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
12 of 50
13 of 50
$cat id1
echo $0 PID = $$
$echo $$
14137
$id1
id1 PID = 15253
$echo $$
14137
Number 14137 is the PID of the login shell, and 15253 is the
PID of the subshell.
14 of 50
15 of 50
Example:
$ set One Two Three
$ echo $3 $2 $1
Three Two One
$ set `date`
$ echo $1 $2 $3
Wed Nov 29
16 of 50
Prompt characters
Domain name of the host
Time in 12 hours hh:mm:ss format
The date in weekday month date format
Host name up to the first dot
The shell name
Time in 24 hour hh:mm:ss format
User name of the current user
Version of the Bash shell
Current working directory
\H
\T
\d
\h
\s
\t
\u
\v
\w
Examples:
PS1=\w$
PS1=\ht
17 of 50
User-defined variables
Name of the variable may be any sequence of letters and
digits with letter as the first character.
When you assign a value to a variable, you must not
precede or follow the equal sign with a Space or Tab.
Bash does not require the type declaration but declare
and typeset commands can be used to declare variables
type.
All Bash variable is string by default, but can be declared
to be an integer.
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
18 of 50
$echo $person
$person
$echo \$person
$person
$echo $person
alex
$echo $person
alex
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
19 of 50
20 of 50
$echo $memo
alex.report alex.summary
$ls
alex.report
alex.summary
To remove the value of a variable, set it to null:
$person=
or use unset command:
$unset person
21 of 50
22 of 50
$cat test1
feline=lion
echo test1 1: $feline
subtest
echo test1 2: $feline
$cat subtest
echo subtest 1:$feline
feline=tiger
echo subtest 2:
$feline
$test1
test1 1: lion
subtest 1:
subtest 2: tiger
test1 2: lion
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
$cat test2
export feline
feline=lion
echo test2 1: $feline
subtest
echo test2 2: $feline
$test2
test2 1: lion
subtest 1: lion
subtest 2: tiger
test2 2: lion
23 of 50
24 of 50
Example:
$cat readx
echo Enter a command: \c
read command
$command
echo Thanks
$readx
Enter a command: who
alex
tty11
Jun 17 07:09
scott
tty7
Jun 17 08:23
Thanks
25 of 50
Example:
$cat
echo
read
echo
echo
readc
Enter something: \c
word1 word2
Word 1 is: $word1
Word 2 is: $word2
$readc
Enter something: Monty Pythons Flying
Circus
Word 1 is: Monty
Word 2 is: Pythons Flying Circus
26 of 50
27 of 50
Control-Flow Commands
The control-flow commands alter the order of execution of
commands within a shell script.
if ... then
if test condition
then
commands
fi
The test condition is relational expression that might have
one out of two values: true or false.
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
28 of 50
29 of 50
30 of 50
-d file
-f file
-L file
-r file
-s file
-w file
-x file
31 of 50
Example:
#!/bin/bash
echo Enter filename: \c
read filename
if [-r $filename -a -s $filename]
then
echo File $filename exists and is not empty.
echo You have read access permission to the file.
fi
32 of 50
33 of 50
Example:
$cat AreYouOK
#!/bin/bash
echo Are you OK? #User prompt
echo input Y for yes and N for no:\c
read answer
if test $answer = Y
then
echo Glad to hear that.
else
echo Hope youll get better soon.
fi
34 of 50
35 of 50
36 of 50
if [$word1=$word2 -a $word2=$word3]
then
echo Match: words 1, 2 & 3
elif [$word1 = $word2]
then
echo Match: words 1 & 2
elif [$word1 = $word3]
then
echo Match: words 1 & 3
elif [$word2 = word3]
then
echo Match: words 2 & 3
else
echo No match
fi
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
37 of 50
38 of 50
39 of 50
40 of 50
In
$((expression))
the value of expression is calculated and the result is
returned.
$x=2 y=3
$echo The value of z=x*y is $((x*y))
The value of z=x*y is 8
$let i=5
$let i=i+1
$echo $i
6
41 of 50
$x=3
$((x=x-2))
$echo $x
1
Command
expr args
provides arithmetic operations capability and evaluates
either numeric or nonnumeric character strings.
The expr command takes the arguments as
expressions, evaluates them, and displays the results on
the standard output device.
Sheridan College - Victor Ralevich
UNIX/LINUX BASH Programming 1
42 of 50
43 of 50
44 of 50
45 of 50
46 of 50
Example:
$ expr 10\<20
1
$expr 10\>20
0
Variables can be declared as integers with the
declare i
command. If you attempt to assign any string value, Bash
assigns 0 to the variable.
47 of 50
48 of 50
$x=16#b
$echo $x
11
$ declare i num
$ num=hello
$ echo $num
0
$num=5+5
$echo $num
10
49 of 50
$num=4 * 5
$echo $num
20
$num=4*5
$echo $num
20
$num=4 * 5
$echo $num
bash: *: command not found
50 of 50