linux tools shell script.. computer science

profileSingpran
ShellProgramming151.ppt

* of 70

UNIX Unbounded 5th Edition
Amir Afzal


Chapter 12
Shell Programming

Copyright ©2008 by Pearson Education, Inc.
Upper Saddle River, New Jersey 07458
All rights reserved.

* of 70


Chapter 12
Exploring the Shell

  • This chapter explains capabilities of the shell as an interpretive high-level language
  • It describes shell programming constructs and particulars
  • Variables
  • Flow control
  • Running
  • Debugging

*

* of 70

UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION

  • A command language – interpreted (not compiled)
  • Shell program files are called shell procedures, shell scripts, or simply scripts
  • A file that contains a series of commands for the shell to execute

* of 70

Writing a Simple Script

* of 70

Executing a Script

  • Making Files Executable: The chmod Command

* of 70

Executing a Script

  • Making Files Executable: The chmod Command

* of 70

Executing a Script

  • Making Files Executable: The chmod Command

* of 70

Executing a Script

  • Making Files Executable: The chmod Command

chmod u=rwx,g=rx,o=r myfile

chmod 754 myfile # 754 = 1 1 1 1 0 1 1 0 0

r w x r - x r - -

* of 70

WRITING MORE SHELL SCRIPTS

  • Built in commands

* of 70

WRITING MORE SHELL SCRIPTS

Amir Afzal
UNIX Unbounded, 5th Edition

Copyright ©2008 by Pearson Education, Inc

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc

* of 70

WRITING MORE SHELL SCRIPTS

Amir Afzal
UNIX Unbounded, 5th Edition

Copyright ©2008 by Pearson Education, Inc

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc

* of 70

WRITING MORE SHELL SCRIPTS

  • Using Special Characters

When using the echo command on the prompt sign:

make sure to use –e option for the escape characters to work as specified

$echo –e “H\nE\nL\nL\nO”

When using the echo command inside script files, and you are using the bash command or chmod +x command to run this cript

make sure to use –e option for the escape characters to work as specified

echo –e “H\nE\nL\nL\nO”

* of 70

WRITING MORE SHELL SCRIPTS

  • Using Special Characters

When using the echo command on the prompt sign:

make sure to use –e option for the escape characters to work as specified

$echo –e “H\nE\nL\nL\nO”

When using the echo command inside script files, and you are using the bash command or chmod +x command to run this cript

make sure to use –e option for the escape characters to work as specified

echo –e “H\nE\nL\nL\nO”

* of 70

WRITING MORE SHELL SCRIPTS

  • Using Special Characters

When using the echo command on the prompt sign:

make sure to use –e option for the escape characters to work as specified

$echo –e “H\nE\nL\nL\nO”

* of 70

WRITING MORE SHELL SCRIPTS

  • Logging Off in Style

* of 70

WRITING MORE SHELL SCRIPTS

  • Executing Commands: The dot Command
  • Lets you execute a program in the current shell and prevents the shell from creating the child process

* of 70

WRITING MORE SHELL SCRIPTS

  • Path Modification
  • Command Substitution
  • Place the output of a command in the argument string

* of 70

WRITING MORE SHELL SCRIPTS

  • Reading Inputs: The read Command

* of 70

  • Reading Inputs: The read Command

Amir Afzal
UNIX Unbounded, 5th Edition

Copyright ©2008 by Pearson Education, Inc

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Comments
  • The shell recognizes # as the comment symbol; therefore, characters after the # are ignored.
  • Variables
  • Write the name of the variable, followed by the equal sign and the value you want to store in the variable
  • No spaces either side of the = sign

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Displaying Variables
  • the echo command can be used to display the contents of variables
  • Command Substitution
  • You can store the output of a command in a variable

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Command Line Parameters

* of 70

  • Command Line Parameters

Amir Afzal
UNIX Unbounded, 5th Edition

Copyright ©2008 by Pearson Education, Inc

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 by Pearson Education, Inc

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Assigning Values
  • Possible to assign values to the positional variables is to use the set command

Accents

Not single quotes

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Scenario – write a script file that does the following
  • Stores the specified file in a directory called keep in your HOME directory
  • Invoke the vi editor to edit the specified file

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Result

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Terminating Programs: The exit Command
  • Use to immediately terminate execution of your shell program
  • Conditions and Tests
  • The if-then construct

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • The if-then-else Construct

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • The if-then-else Construct

no #

Notice the spaces before and after

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • The if-then-elif Construct

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • The if-then-elif Construct

hour= `date +%H’

Replace these

two lines (8 and 9)

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • The if-then-elif Construct

if [ “$answer” = Y ]

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Testing Different Categories
  • Numeric Values
  • use the test command to test (compare) two integer numbers algebraically

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Scenario – write a script that:
  • Accepts three numbers as input (command line arguments) and …
  • Displays the largest of the three
  • Output should look like this:

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

if [ “$num1” –gt “$num2” -a “$num1” –gt “$num3” ]

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • String Values
  • Compare (test) strings with the test command

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • String Values
  • Compare (test) strings with the test command

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Files – use the test command to test file characteristics
  • Size, type, permissions

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Files – use the test command to test file characteristics
  • Size, type, permissions

if [ -r “$FILE” ]

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Files – use the test command to test file characteristics

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Parameter Substitution
  • Lets you test the value of a parameter and change its value according to a specified option

If variable has value

return this value

Else

return the string

If variable has value

return the string

Else

nothing

If variable has value

return this value

Else

variable = string

If variable has value

return this value

Else

print string

* of 70

echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"

echo ${var:="Variable is not set"}
echo "2 - Value of var is ${var}"

unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var“

var="Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"

echo ${var:?"Print this message"}
echo "5 - Value of var is ${var}"

Variable is not set

1 - Value of var is

Variable is not set

2 - Value of var is Variable is not set

3 - Value of var is

This is default value

4 - Value of var is Prefix

Prefix

5 - Value of var is Prefix

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Parameter Substitution

Notice the exit from the script

If name is still empty

* of 70

EXPLORING THE SHELL: PROGRAMMING BASICS

  • Parameter Substitution

* of 70

ARITHMETIC OPERATIONS

  • The expr Command – Arithmetic Operators
  • Note: spaces between the elements of an expression are necessary, integer only

* of 70

ARITHMETIC OPERATIONS

  • The expr Command – Arithmetic Operators
  • The * (multiplication) and % (remainder) characters have special meaning to the shell, they must be preceded by a backslash [\]

* of 70

ARITHMETIC OPERATIONS

  • The expr Command – Arithmetic Operators
  • The grave accent marks [` and `] surrounding the command are necessary and cause the output of the command (expr) to be substituted.

$ x=10

$ y=20

$ z=`expr $x + $y`

* of 70

$ c=`expr $a + $b`

$ echo “the value of addition=$c”

$ d=`expr $a - $b`

$ echo “the value of subtraction=$d”

$ e=`expr $a \* $b`

$ echo “the value of multiplication=$e”

$ f=`expr $a / $b`

$ echo “the value of division=$f”

$ g=`expr $a \% $b`

$ echo “the value of modulus=$g”

Show the output of each code segment?

* of 70

ARITHMETIC OPERATIONS

  • Relational Operators
  • Relational operators that work on both numeric and nonnumeric arguments.
  • If both arguments are numeric, the comparison is numeric
  • If one or both arguments are nonnumeric, the comparison is nonnumeric and uses the ASCII values.
  • because > (greater than) and < (less than) characters have special meaning to the shell, they must be preceded by a backslash [\]

n1=10

n2=20

if [ “$n1” \> “$n2” ]

then

echo “$n1 is greater than $n2”

else

echo ”$n1 is not greater than $n2”

fi

* of 70

ARITHMETIC OPERATIONS

  • Arithmetic Operations: The let Command
  • Simpler alternative to the expr command and includes all the basic arithmetic operations

* of 70

THE LOOP CONSTRUCTS

  • The For Loop: The for-in-done Construct
  • Used to execute a set of commands a specified number of times

* of 70

THE LOOP CONSTRUCTS

  • The For Loop: The for-in-done Construct

* of 70

for var in "$@"

do

echo "$var"

done

for i in {1..5}

do

echo "Welcome $i times"

done

for num in `seq 1 2 10`

do

echo "$num"

done

* of 70

for (( c=1; c<=5; c++ ))

do

echo "Welcome $c times"

done

exit 0

Spaces are not important here

* of 70

THE LOOP CONSTRUCTS

  • The For Loop: The for-in-done Construct

* of 70

THE LOOP CONSTRUCTS

  • The While Loop: The while-do-done Construct
  • The while loop continues as long as the loop condition is true.

* of 70

THE LOOP CONSTRUCTS

  • The While Loop: The while-do-done Construct

* of 70

THE LOOP CONSTRUCTS

  • The While Loop: The while-do-done Construct
  • Command line version of the carryon program

* of 70

THE LOOP CONSTRUCTS

  • The While Loop: The while-do-done Construct

* of 70

THE LOOP CONSTRUCTS

  • The While Loop: The while-do-done Construct

* of 70

THE LOOP CONSTRUCTS

  • The While Loop: The while-do-done Construct

* of 70

THE LOOP CONSTRUCTS

  • The Until Loop: The until-do-done Construct
  • Similar to the while loop, except …
  • It continues executing the body of the loop as long as the condition of the loop is false
  • Body of the until loop might never get executed if the loop condition is true

* of 70

THE LOOP CONSTRUCTS

  • Scenario: check whether a specified user is on the system or, if not, to be informed as soon as the user logs in

* of 70

DEBUGGING SHELL PROGRAMS

  • The Shell Command
  • Use the sh, ksh, or bash command with one of its options to make the debugging of your script files easier
  • Options

* of 70

DEBUGGING SHELL PROGRAMS

  • The Shell Command

* of 70

DEBUGGING SHELL PROGRAMS

  • The Shell Command

* of 70

DEBUGGING SHELL PROGRAMS

  • The Shell Command
  • Run the BOX program with the -v option, and specify some command line arguments.

* of 70

DEBUGGING SHELL PROGRAMS

  • The Shell Command