Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15 Introductory Bash Programming

Similar presentations


Presentation on theme: "Chapter 15 Introductory Bash Programming"— Presentation transcript:

1 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

2 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.

3 Introductory Bash Programming 15.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 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

4 Introductory Bash Programming 15.3 Running a Bash Script
Table Some Important Writable Bash Environment Variables (continued on next page)‏

5 Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏
Table Some Important Writable Bash Environment Variables (continued from previous page)‏

6 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 Some Important Read-Only Bash Environment Variables

7 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. Controlling The Prompt Table Some Useful Prompt Characters and their Descriptions

8 Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏
Controlling The Prompt (Cont'd) Example: $ PS1='\w $ ' ~ $ $PS1='\d $ ' Mon May 12 $ $PS1='\h $ ' pardus $ $ \w \$ ' ~ $ $ PS1='bash-\v|\d|\w\$ ' bash-3.1|Mon May 12|~$

9 Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏
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.

10 Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏
Variable Declaration (cont'd) Examples: $ declare -i age=42 $ declare -rx OS=LINUX $echo $age 42 $echo $OS LINUX $declare OS $declare age $echo $Os

11 Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏
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

12 Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏
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” For generic variables, any type of value (integer or string) can be assigned $ name=John $ echo $name John $ name=22

13 Introductory Bash Programming. 15. 3
Introductory Bash Programming Reading and Writing Shell Variables Table Variable Substitution Operators and Their Descriptions

14 Introductory Bash Programming. 15. 3
Introductory Bash Programming Reading and Writing Shell Variables (cont'd) Example: $ echo $name $ name=Mutlu Mutlu $ echo $place $ echo ${name:-Sacit} ${place:-Ankara} Mutlu Ankara $ echo ${name:+Defined} Defined $ echo ${place:+Not Defined} $ echo ${place:=Istanbul} Istanbul Mutlu Istanbul

15 Introductory Bash Programming. 15. 3
Introductory Bash Programming 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' Mutlu Ankara $ touch Ankara{1,2,3} $ name= Ankara* Ankara1 Ankara2 Ankara3 $ echo “$name” Ankara* $ echo "All files starting with Ankara: $name " All files starting with Ankara: Ankara* $ echo \$name $name $ echo '$name'

16 Introductory Bash Programming. 15. 3
Introductory Bash Programming Reading and Writing Shell Variables (cont'd) Example: $ command=pwd $ $command /home/ctis $ command=hello bash: hello: command not found $ command=”ls -l” $ $command /etc

17 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)‏ 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

18 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 Example: $ cat display_name echo $name exit 0 $ name=“Mutlu Ankara” $ bash display_name $ declare –x name=“Mutlu Ankara” Mutlu Ankara $ echo $?

19 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 echo $name name=“Blue Rose” $ ./export_name Mutlu Ankara Blue Rose $

20 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”

21 Introductory Bash Programming. 15. 3
Introductory Bash Programming 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

22 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”

23 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 ... ..

24 Introductory Bash Programming. 15
Introductory Bash Programming 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 exit 0

25 Introductory Bash Programming. 15
Introductory Bash Programming 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 $ echo $1, $2 and $4

26 Introductory Bash Programming. 15
Introductory Bash Programming 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 >> $filename

27 Introductory Bash Programming. 15. 6 Program Control Flow Commands 15
Introductory Bash Programming Program Control Flow Commands The if-then-elif-else-fi Statement Figure 15.1  Semantics of the if-then-fi statement

28 Introductory Bash Programming. 15. 6 Program Control Flow Commands 15
Introductory Bash Programming Program Control Flow Commands 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.

29 Table 15.5 Some Useful Operators for the test Command (continued on next page)‏

30 Table 15.5 Some Useful Operators for the test Command (continued from previous page)‏

31 Table 15.5 Some Useful Operators for the test Command (continued from previous page)‏

32 Figure 15.2  Semantics of the if-then-else-fi statement

33 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.

34 the contents of the for-1 and for-2 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.

35 Figure 15.5 Semantics of the while statement
Example: Let's display the contents of the while-1 and while-demo run them.

36 Figure 15.6 Semantics of the until statement
Example: Let's display the contents of the until-1 and run it.

37 Figure 15.7  Semantics of the break and continue commands

38 Figure 15.8 Semantics of the case statement
Example: Let's display the contents of the case-demo and run it.


Download ppt "Chapter 15 Introductory Bash Programming"

Similar presentations


Ads by Google