Chapter 15 Introductory Bash Programming To introduce the concept of shell programming To describe...
-
Upload
nigel-washington -
Category
Documents
-
view
227 -
download
1
Transcript of Chapter 15 Introductory Bash Programming To introduce the concept of shell programming To describe...
Chapter 15 Introductory Bash Programming
To introduce the concept of shell programming
To describe how shell programs are executed
To discuss how command line arguments are passed to shell programs
To explain the concept of command substitution
To describe some basic coding principles
To write and discuss some shell scripts
To cover the related commands
Introductory Bash Programming15.1 Introduction
Bash is more than a command interpreter.
It has own programming language.
A shell program is commonly known as shell script.
In order to permit non-sequential execution of the commands in a shell script, Bash also supports program flow control commands (statements) such as if, case, for, while and until.
Introductory Bash Programming15.2 Running a Bash Script
Three ways to run a Bash script: Firstly, you run chmod u+x script_file
If you use the bash shell, then you will run the following command:
./script_file If you use the Linux shell other than the bash shell, you
can run /bin/bash ./script_file
Secondly, you run /bin/bash script_file
Lastly, you run chmod u+x script_file and you can put the following line at the beginning of the script_file and then you run ./script_file
#!/bin/bash
Introductory Bash Programming15.3 Running a Bash Script
Table 15.1 Some Important Writable Bash Environment Variables (continued on next page)
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
Table 15.1 Some Important Writable Bash Environment Variables (continued from previous page)
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
Two types of shell variables are shell environments variables (writable or read-only) and user-defined variables.
Table 15.2 Some Important Read-Only Bash Environment Variables
One can use the set command to display shell environment variables and user defined variables with their values. The env command can be used to display the former ones with their values.
15.3.1 Controlling The Prompt
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
Table 15.3 Some Useful Prompt Characters and their Descriptions
15.3.1 Controlling The Prompt (Cont'd)
Example:
$ PS1='\w $ '~ $ $PS1='\d $ 'Mon May 12 $ $PS1='\h $ 'pardus $ $ PS1='\u@\h \w \$ 'ctis@pardus ~ $$ PS1='bash-\v|\d|\w\$ 'bash-3.1|Mon May 12|~$
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
15.3.2 Variable Declaration Linux users can use the declare and typeset commands to declare variables, initialize them, and set their attributes. Syntax:
declare [± options] [name [=value] ]typeset [± options] [name [=value] ]
Commonly used options / features: -a each 'name' is an array -f each 'name' is a function -i 'name' is an integer -r mark each 'name' read-only ( can not be turned off by
using +x) -x mark each 'name' exported.
Note that using + instead of – turns attributes off.
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
15.3.2 Variable Declaration (cont'd)
Examples: $ declare -i age=42$ declare -rx OS=LINUX
$echo $age 42 $echo $OSLINUX
$declare OS$declare age$echo $age42$echo $OsLINUX
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
15.3.2 Variable Declaration (cont'd)
Examples: In order to display all integer and read-only variables in
your environment: $ declare -ir
Similarly,
$ declare -x
$ declare -i
$ declare
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
15.3.2 Variable Declaration (cont'd) One can change the value of a variable by using the name=value syntax
For an integer variable, if a noninteger value is assigned, then this variable will get a value of zero. $ declare -i age=22
$ echo $age22$ age=”Twenty”$ echo $age0
For generic variables, any type of value (integer or string) can be assigned. $ name=John
$ echo $nameJohn $ name=22 $ echo $name22
Introductory Bash Programming15.3 Running a Bash Script (cont'd)
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables
Table 15.4 Variable Substitution Operators and Their Descriptions
Example:$ echo $name
$ name=Mutlu$ echo $nameMutlu $ echo $place
$ echo ${name:-Sacit} ${place:-Ankara}Mutlu Ankara$ echo ${name:+Defined} Defined$ echo ${place:+Not Defined} $ echo ${place:=Istanbul} Istanbul$ echo ${name:-Sacit} ${place:-Ankara}Mutlu Istanbul
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd)
The use of single and double quotes, *, and \ in an assignment statement.
Example:$ name=Mutlu $ echo $nameMutlu $ name= Mutlu Ankarabash: Ankara: command not found$ name='Mutlu Ankara'$ echo $nameMutlu Ankara$ touch Ankara{1,2,3}$ name= Ankara*$ echo $nameAnkara1 Ankara2 Ankara3$ echo “$name”Ankara*$ echo "All files starting with Ankara: $name "All files starting with Ankara: Ankara*
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd)
$ echo \$name$name$ echo '$name'$name
Example:
$ command=pwd$ $command
/home/ctis$ command=hello $ $command
bash: hello: command not found
$ command=”ls -l”$ $command................$ $command /etc................
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd)
Syntax: $(command)
Example:
$ command=pwd
$ echo “The value of command is: $command.”The value of command is: pwd
$ command=(pwd)
$ echo “The value of command is: $command.”The value of command is: /home/ctis/Desktop,
$ echo “The date and time is $(date).”The date and time is Wed May 14 13:00 EST 2008
Introductory Bash Programming 15.3.4 Command Substitution
Syntax: declare –x [name-list]typeset –x [name-list]export [name-list]
Note that you can download the most of scripts given in the following examples from
http://www.bilkent.edu.tr/~hmurat/ctis156/scripts.tar.gzExample:
$ cat display_nameecho $nameexit 0
$ name=“Mutlu Ankara”$ bash display_name$ declare –x name=“Mutlu Ankara”$ bash display_nameMutlu Ankara$ echo $?0
Introductory Bash Programming 15.3.5 Exporting Environment
Example:
$ cat export_name#! /bin/bashdeclare –x name=“Mutlu Ankara”./display_change_namebash display_nameexit 0
$ cat display_change_name#! /bin/bashecho $namename=“Blue Rose”echo $nameexit 0
$ ./export_nameMutlu AnkaraBlue RoseMutlu Ankara$
Introductory Bash Programming 15.3.5 Exporting Environment (Cont’d)
Syntax unset [name-list]
$ declare name=Mutlu place=Ankara
$ echo $name $place Mutlu Ankara
$ unset name
$ echo “$name”
$ echo “$place”Ankara------------------------------------------------------------------------------$ unset name place $ declare name=Mutlu ; echo “$name” Mutlu $ name= ; echo “$name”
Introductory Bash Programming 15.3.6 Resetting Variables
Syntax declare -r [name-list] typeset -r [name-list] readonly [name-list]
Example: $ declare –r name=Mutlu place=Ankara
$ echo $name $place Mutlu Ankara
$ name=Guler place=Bolubash: name: readonly variablebash: place: readonly variable
$ readonly$ unset name bash: unset: name: cannot unset: readonly variable
Introductory Bash Programming 15.3.7 Creating Read-Only User-Defined Variables
Syntax read [options] [name-list]
Example: $ cat read_demo #!/bin/bash echo “Enter input: “ read line echo “You entered: $line” echo “Enter another line: “ read word1 word2 word3 echo “The first word is: $word1” echo “The second word is: $word2” echo “The rest of the line is: $word3”
Introductory Bash Programming 15.3.8 Reading From Standard Input
Syntax read [options] [name-list]
Example: $ cat cmdargs_demos #!/bin/bash echo The command name is $0. echo “The num. of command line arg. passed as
parameters are $#.” echo “These values: $1 $2 $3 $4 $5 $6 $7 $8 $9.” echo “Another way: $@.” echo “Yet Another way: $*.” exit 0$ ./cmdargs_demo .....
Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts
Syntax shift [N]
Example: $ cat shift_demo #!/bin/bash echo The command program name is $0. echo The arguments are $@. echo The first three arg. are $1 $2 $3. shift echo The command program name is $0. echo The arguments are $@. echo The first three arg. are $1 $2 $3. exit 0
Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts (Cont'd)
Syntax set [options] [argument-list]
Example:
$ date Wed May 14 03:18:04 EEST 2008
$ set $(date) $ echo $@ Wed May 14 03:18:04 EEST 2008
$ echo $1, $2 and $4
Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts (Cont'd)
Example:
$ cat set-1#!/bin/bashfilename=output.txt
set $(ls)echo $@echo echo $1 $2 echo $1 $2 > $filename shift 1echo $1 $2echo $1 $2 >> $filename
Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts (Cont'd)
Introductory Bash Programming 15.6 Program Control Flow Commands 15.6.1 The if-then-elif-else-fi Statement
Figure 15.1 Semantics of the if-then-fi statement
The test command is used to evaluate the expression. Syntax
test [expression] [[expression]]
Example: Let's display the contents of the if-1 and run it.
Introductory Bash Programming 15.6 Program Control Flow Commands 15.6.1 The if-then-elif-else-fi Statement (cont'd)
Table 15.5 Some Useful Operators for the test Command (continued on next page)
Table 15.5 Some Useful Operators for the test Command (continued from previous page)
Table 15.5 Some Useful Operators for the test Command (continued from previous page)
Figure 15.2 Semantics of the if-then-else-fi statement
Figure 15.3 Semantics of the if-then-elif-else-fi statement
Example: Let's display the contents of the if-2 and if-3 and run them.
Figure 15.4 Semantics of the for statement
Example: Let's display the contents of the for-1 and for-2 and run them.
Figure 15.5 Semantics of the while statement
Example: Let's display the contents of the while-1 and while-demo run them.
Figure 15.6 Semantics of the until statement
Example: Let's display the contents of the until-1 and run it.
Figure 15.7 Semantics of the break and continue commands
Figure 15.8 Semantics of the case statement
Example: Let's display the contents of the case-demoand run it.