1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

37
1 LINUX Shell Scripting Advanced Issues Yusuf Altunel

Transcript of 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

Page 1: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

1

LINUX Shell ScriptingAdvanced Issues

Yusuf Altunel

Page 2: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

2

Content

Commands Command Line Arguments Redirection of Input/Output Pipes and Filters Programming in Background Conditionals

if test if else fi

Loops for loop do loop

Page 3: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

3

The readread Statement Use to get input (data from user) from keyboard and

store (data) to variable.Syntax: readread variable1, variable2,...variableN variable1, variable2,...variableN

Example: Write a shell script to first ask user, name then waits to enter name from the user via keyboard. Then user enters name from keyboard (after giving name you

have to press ENTER key) entered name through keyboard is stored (assigned) to

variable fname.

Solution is in the next slide

Page 4: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

4

Example (readread Statement)

$ vi sayH###Script to read your name from key-board#Script to read your name from key-board##echoecho "Your first name please:" "Your first name please:"readread fname fnameechoecho "Hello $fname, Lets be friend!" "Hello $fname, Lets be friend!"

Run it as follows:$ chmodchmod 755 sayH 755 sayH$ ./sayH./sayHYour first name please: vivekHello vivek, Lets be friend!

Page 5: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

5

Wild Cards Wild card

/ShorthandMeaning Examples

*Matches any string or group of characters.

$ ls * will show all files

$ ls a*will show all files whose first name is starting with letter 'a'

$ ls *.c   will show all files having extension .c

$ ls ut*.cwill show all files having extension .c but file name must begin with 'ut'.

?Matches any single character.

$ ls ?   will show all files whose names are 1 character long 

$ ls fo?will show all files whose names are 3 character long and file name begin with fo

[...]Matches any one of the enclosed characters

$ ls [abc]*  will show all files beginning with letters a,b,c

Page 6: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

6

More commands on one command line Syntax:

command1;command2command1;command2To run two command in one command line.

Examples:

$ datedate;;whowho Will print today's date followed by users who are currently login.

Note that You can't use$ datedate whowho for same purpose, you must put semicolon in between the datedate and whowho command.

Page 7: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

7

Command Line Arguments 1. Telling the command/utility

which option to use.

2. Informing the utility/command which file or group of files to process

Let's take rmrm command, is used to remove file, which of the file? how to tail this to rmrm command

rmrm command does not ask the name of the file So what we do is to write command as follows:

$ rmrm {file-name}rmrm : is the command file-name :file to remove

Page 8: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

8

Arguments - Specification$ myshell foo barmyshell foo bar

                           

    Shell Script name i.e. myshellmyshell     First command line argument passed to myshellmyshell i.e. foofoo     Second command line argument passed to myshellmyshell i.e. barbarIn shell if we wish to refer this command line argument we refer above as follows

myshell it is $0myshell it is $0foo it is $1foo it is $1bar it is $2bar it is $2

•Here $# (built in shell variable ) will be 2 (Since foofoo and barbar only two Arguments), •Please note at a time such 9 arguments can be used from $1..$9, •You can also refer all of them by using $* (which expand to `$1,$2...$9`). •Note that $1..$9 i.e command line arguments to shell script is know as "positional parameters".

Page 9: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

9

Arguments - Example$ vi demovi demo

#!/bin/sh#!/bin/sh### Script that demos, command line args# Script that demos, command line args##echoecho "Total number of command line argument are $#" "Total number of command line argument are $#"echoecho "$0 is script name" "$0 is script name"echoecho "$1 is first argument" "$1 is first argument"echoecho "$2 is second argument" "$2 is second argument"echoecho "All of them are :- $* or $@" "All of them are :- $* or $@"

•Run it as follows•Set execute permission as follows:$ chmodchmod 755 demo 755 demo•Run it & test it as follows:$ ./demo Hello World./demo Hello World•If test successful, copy script to your own bin directory (Install script for private use)$ cpcp demo ~/bin demo ~/bin•Check whether it is working or not (?)$ demodemo$ demo Hello World demo Hello World

Page 10: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

10

Redirection of Input/Output

In Linux (and in other OSs also) it's possible to send output to file or to read input from a file

For e.g.$ lsls command gives output to screen;

to send output to file of ls command give commandlsls > filename > filenameIt means put output of lsls command to filename.

Page 11: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

11

redirection symbols: ‘>>’ There are three main redirection symbols: >,>>,<(1) >> Redirector Symbol

Syntax:Linux-command > filenameLinux-command > filename

To output Linux-commands result to file. Note that if the file already exist,

it will be overwritten else a new file will be created.

For e.g. To send output of lsls command give

$ lsls > myfiles > myfiles

if 'myfilesmyfiles' exist in your current directory it will be overwritten without any warning.

Page 12: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

12

redirection symbols: ‘>>>>’(2) >>>> Redirector Symbol

Syntax:Linux-command >> filenameLinux-command >> filename

To output Linux-commands result to the END of the file.

if file exist: it will be opened new information/data will be written to the END of the file, without losing previous information/data,

if file does not exist, a new file is created. For e.g. To send output of date command

to already exist file give command$ datedate >> myfiles >> myfiles

Page 13: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

13

redirection symbols: ‘<<’

(3) << Redirector SymbolSyntax:Linux-command < filenameLinux-command < filename

To provide input to Linux-command from the file instead of standart input (key-board).

For e.g. To take input for cat command give$ catcat < myfiles < myfiles

Page 14: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

14

Pipes A pipe is a way

to connect the output of one program to the input of another program

without any temporary file.

Definition "A pipe is nothing but a temporary storage place

where the output of one command is stored and then passed

as the input for second command. Pipes are used

to run more than two commands Multiple commands

from same command line."

Syntax:command1 | command2command1 | command2

Page 15: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

15

Pipe - ExamplesCommand using Pipes Meaning or Use of Pipes

$ lsls | | moremoreOutput of lsls command is given as input to the command moremore So output is printed one screen full page at a time.

$ whowho | | sortsort  Output of who command is given as input to sort command So it will print sorted list of users

$ whowho | | sortsort >  > user_listuser_list

Same as above except output of sort is send to (redirected) the file named user_list

$ whowho | | wcwc -l -l whowho command provides the input of wcwc commandSo it will count the users logged in.

$ lsls -l |  -l | wcwc  -l  -l lsls command provides the input of wcwc command So it will count files in current directory.

$ whowho | | grepgrep raju rajuOutput of whowho command is given as input to grepgrep command So it will print if particular user is logged in. Otherwise nothing is printed

Page 16: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

16

Filter

Accepting the input from the standard input

and producing the result on the standard output

is know as a filter. A filter

performs some kind of process on the input and provides output.

Page 17: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

17

Filter: Examples

Suppose you have a file called 'hotel.txt' with 100 lines data,

you would like to print the content only between the line numbers 20 and 30 and then store this result to the file 'hlist'

The appropriate command:$ tailtail +20 < hotel.txt | +20 < hotel.txt | headhead -n30 >hlist -n30 >hlist

Here headhead command is filter: takes its input from tailtail command

tailtail command starts selecting from line number 20 of given file i.e. hotel.txt

and passes this lines as input to the headhead, whose output is redirected to the 'hlist' file.

Page 18: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

18

Filter: Examples

Consider one more following example$ sortsort < sname | < sname | uniquniq > u_sname > u_sname

Here uniquniq is filter takes its input from sortsort command and redirects to "u_sname" file.

Page 19: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

19

Processing in Background Use ampersand (&)

at the end of command To start the execution in background

and enable the user to continue his/her processing during the execution of the command

without interrupting $ lsls / -R | / -R | wcwc -l -l This command will take lot of time

to search all files on your system.

So you can run such commands in Background or simultaneously by adding the ampersand (&):

$ lsls / -R | / -R | wcwc -l -l&&

Page 20: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

20

Commands Related With Processes

For this purpose Use this Command Examples

To see currently running process  psps $ $ psps

To stop any process by PID i.e. to kill process

killkill    {PID}    {PID} $ $ killkill  1012  1012

To stop processes by name i.e. to kill process

killallkillall   {Proc-name}   {Proc-name} $ $ killallkillall httpd httpd

To get information about all running process

psps -ag -ag $ $ psps -ag -ag

To stop all process except your shell killkill 0 0 $ $ killkill 0 0

For background processing (With &, use to put particular command and program in background)

linux-command  &linux-command  & $ $ lsls / -R | / -R | wcwc -l & -l &

To display the owner of the processes along with the processes  

psps aux aux $ $ psps aux aux

To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command

psps ax | ax | grepgrep  {Proc-name}  {Proc-name}

For e.g. you want to see whether Apache web server process is running or not then

give command $ ps ax | grep httpd

To see currently running processes and other information like memory and CPU usage with real time updates.

toptop$ $ toptopNote that to exit from Note that to exit from top command press q.top command press q.

To display a tree of processes pstreepstree $ $ pstreepstree

Page 21: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

21

if condition

if condition used for making decisions in shell script, If the condition is true then command1 is executed.

Syntax:if if condition condition then cthen command1ommand1 if if condition condition is true or is true or if if

exit status of condition is 0 (zero) ... ... fiexit status of condition is 0 (zero) ... ... fi condition is defined as:

"Condition is nothing but comparison between two values."

For compression you can use test or [ expr ] statements or even exist status

Page 22: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

22

ifif condition - Examples $ catcat > showfile > showfile

#!/bin/sh#!/bin/sh###Script to print file#Script to print file##if catif cat $1 $1thenthenechoecho -e "\n\nFile $1, found and successfully echoed" -e "\n\nFile $1, found and successfully echoed"fifi

Run above script as:$ chmodchmod 755 showfile 755 showfile$./showfile foo./showfile foo

Shell script name is: showfileshowfile ($0$0) The argument is foofoo ($1$1). Then shell compare it as follows:

if catif cat $1 $1 :is expanded to ifif catcat foo foo.

Page 23: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

23

Example: Detailed explanation

if catcat command finds foo file and if its successfully shown on screen, it means our catcat command

is successful and its exist status is 0 (indicates success),

So our if condition is also true the statement echoecho -e "\n\nFile $1, found and successfully echoed" -e "\n\nFile $1, found and successfully echoed" is proceed by shell.

if cat command is not successful then it returns non-zero value

indicates some sort of failure the statement echoecho -e -e "\n\nFile $1, found and successfully

echoed" is skipped by our shell.

Page 24: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

24

testtest command or [ expr ][ expr ] testtest command or [ expr ][ expr ]

is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.

Syntax: testtest expressionexpression or [ [ expressionexpression ] ]

Page 25: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

25

test command - Example determine whether given argument number is positive. $ catcat > ispostive > ispostive

#!/bin/sh#!/bin/sh### Script to see whether argument is positive# Script to see whether argument is positive##ifif testtest $1 -gt 0 $1 -gt 0thenthenechoecho "$1 number is positive" "$1 number is positive"fifi

Run it as follows$ chmodchmod 755 ispostive 755 ispostive

$ ispostive 5ispostive 55 number is positive

$ispostive -45ispostive -45Nothing is printed

Page 26: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

26

Mathematical OperatorsMathemati

cal Operator in  Shell Script 

Meaning

Normal Arithmetica

l/ Mathemati

cal Statements

But in Shell

     For test

statement with if

command

For [ expr ] statement with

if command

-eq is equal to 5 == 6if test 5 -eq 6

if [ 5 -eq 6 ]

-ne is not equal to 5 != 6if test 5 -ne 6

if [ 5 -ne 6 ]

-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]

-leis less than or equal to

5 <= 6if test 5 -le 6

if [ 5 -le 6 ]

-gt is greater than 5 > 6if test 5 -gt 6

if [ 5 -gt 6 ]

-geis greater than or equal to

5 >= 6if test 5 -ge 6

if [ 5 -ge 6 ]

Page 27: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

27

String Operators

Operator Meaning

string1 = string2

string1 is equal to string2

string1 != string2

string1 is NOT equal to string2

string1 string1 is NOT NULL or not defined 

-n string1 string1 is NOT NULL and does exist

-z string1 string1 is NULL and does exist

Page 28: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

28

File and Directory OperatorsTest Meaning

-s file    Non empty file

-f file   File exists or is a normal file and not a directory 

-d dir     Directory exists and not a file

-w file   file is a writeable file

-r file    file is a read-only file

-x file    file is executable

Page 29: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

29

Logical Operators

Operator            Meaning

! expression Logical NOT

expression1  -a  expression2 Logical AND

expression1  -o  expression2 Logical OR

Page 30: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

30

ifif...elseelse...fifi If given condition is true then command1 is executed otherwise command2 is executed.

Syntax:ifif condition thenthen

condition is zero (true - 0)execute all commands up to else statement

elseelseif condition is not true thenexecute all commands up to fi

fifi

Page 31: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

31

if...else…fi -Example$ vivi isnump_n#!/bin/sh## Script to see whether argument ispositive or negative#ifif [ $# -eq 0 ]thenthen

echoecho "$0 : You must give/supply one integers"exitexit 1

fifi ifif testtest $1 -gt 0thenthen

echoecho "$1 number is positive"elseelse echoecho "$1 number is negative“

fifi

Try it as follows:$ chmod 755 isnump_n

$ isnump_n 55 number is positive

$ isnump_n -45 -45 number is negative

$ isnump_n./ispos_n : You must give/supply one integers

$ isnump_n 00 number is negative

Page 32: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

32

Loops in Shell Scripts Bash supports:

for loop while loop

Note that in each and every loop,

(a) First, the variable used in loop condition must be initialized, then execution of the loop begins.

(b) A test (condition) is made

at the beginning of each iteration.

(c) The body of loop ends

with a statement modifies

the value of the test (condition) variable.

Page 33: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

33

for Loop

Syntax:forfor { variable name } in { list } dodo

execute one for each item in the list until the list is not finished (And repeat all statements between do and done)

donedone

Page 34: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

34

for Loop: Example

Example:$ catcat > testforforfor i in 1 2 3 4 5dodo

echoecho "Welcome $i times"

donedone

Run it above script as follows:$ chmodchmod +x testfor$ ./testfor

•The for loop first creates i variable •and assigned a number to i from the list of numbers 1 to 5,

•The shell executes echo statement for each assignment of i. •This process will continue until all the items in the list were not finished, •because of this it will repeat 5 echo statements.

Page 35: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

35

for loop - Example $ vivi chessboard

forfor (( i = 1; i <= 9; i++ )) ### Outer for loop ###dodo   forfor (( j = 1 ; j <= 9; j++ )) ### Inner for loop ###   dodo        tot=`expr $i + $j`        tmp=`expr $tot % 2`        ifif [ $tmp -eq 0 ]; then            echo -e -n "\033[47m "        elseelse            echo -e -n "\033[40m "        fifi  donedone echoecho -e -n "\033[40m" #### set back background colour to black echoecho "" #### print the new line ###donedone

Page 36: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

36

while Loop

Syntax:whilewhile [ condition ]dodo

command1command2......

donedone

Example:whilewhile [ $i -le 10 ]dodo echoecho "$n * $i = `expr $i \* $n`" i=`expr $i + 1`donedone

Page 37: 1 LINUX Shell Scripting Advanced Issues Yusuf Altunel.

37

End of Chapter

LINUX Shell Scripting

Advanced Issues