380 likes | 515 Views
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
E N D
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 Programming 15.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 Programming 15.2 Running a Bash Script • Three ways to run a Bash script: • Firstly, you runchmod 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 Programming 15.3 Running a Bash Script Table 15.1 Some Important Writable Bash Environment Variables (continued on next page)
Introductory Bash Programming 15.3 Running a Bash Script (cont'd) Table 15.1 Some Important Writable Bash Environment Variables (continued from previous page)
Introductory Bash Programming 15.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
Introductory Bash Programming 15.3 Running a Bash Script (cont'd) • 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 Table 15.3 Some Useful Prompt Characters and their Descriptions
Introductory Bash Programming 15.3 Running a Bash Script (cont'd) • 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 Programming 15.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 Programming 15.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 $OS LINUX $declare OS $declare age $echo $age 42 $echo $Os LINUX
Introductory Bash Programming 15.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 Programming 15.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 $age 22 $ age=”Twenty” $ echo $age 0 • For generic variables, any type of value (integer or string) can be assigned. $ name=John $ echo $name John $ name=22 $ echo $name 22
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables Table 15.4 Variable Substitution Operators and Their Descriptions
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd) • Example: $ echo $name $ name=Mutlu $ echo $name Mutlu $ 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 $name Mutlu $ name= Mutlu Ankara bash: Ankara: command not found $ name='Mutlu Ankara' $ echo $name Mutlu Ankara $ touch Ankara{1,2,3} $ name= Ankara* $ echo $name Ankara1 Ankara2 Ankara3 $ echo “$name” Ankara* $ echo "All files starting with Ankara: $name " All files starting with Ankara: Ankara* $ echo \$name $name $ echo '$name' $name
Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd) • 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.4 Command Substitution • 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.5 Exporting Environment • 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.gz • Example: $ cat display_name echo $name exit 0 $ name=“Mutlu Ankara” $ bash display_name $ declare –x name=“Mutlu Ankara” $ bash display_name Mutlu Ankara $ echo $? 0
Introductory Bash Programming 15.3.5 Exporting Environment (Cont’d) • Example: $ cat export_name #! /bin/bash declare –x name=“Mutlu Ankara” ./display_change_name bash display_name exit 0 $ cat display_change_name #! /bin/bash echo $name name=“Blue Rose” echo $name exit 0 $ ./export_name Mutlu Ankara Blue Rose Mutlu Ankara $
Introductory Bash Programming 15.3.6 Resetting Variables • 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.7 Creating Read-Only User-Defined 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=Bolu bash: name: readonly variable bash: place: readonly variable $ readonly $ unset name bash: unset: name: cannot unset: readonly variable
Introductory Bash Programming 15.3.8 Reading From Standard Input • 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.4 Passing Arguments To Shell Scripts • 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 (Cont'd) • 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/bash • filename=output.txt • set $(ls) • echo $@ • echo • echo $1 $2 • echo $1 $2 > $filename • shift 1 • echo $1 $2 • echo $1 $2 >> $filename
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
Introductory Bash Programming 15.6 Program Control Flow Commands 15.6.1 The if-then-elif-else-fi Statement (cont'd) • 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.
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.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.8 Semantics of the case statement Example: Let's display the contents of the case-demo and run it.