Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.

Similar presentations


Presentation on theme: "1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0."— Presentation transcript:

1 1 Shell Programming – Extra Slides

2 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0 while read aline do count=`expr $count + 1` done < $filename echo "$filename has $count lines“ From the command line: $wc –l data.txt 11 data.txt $countLines1 data.txt Data.txt has 0 lines

3 3 Counting the number of lines in a file #!/bin/sh #countLines2 filename=$1#Should check if arguments are given count=0 Cat $filename | while read aline do count=`expr $count + 1` done echo "$filename has $count lines“ From the command line: $wc –l data.txt 11 data.txt $countLines2 data.txt Data.txt has 0 lines WHY? Subshell execution takes place if input is piped into a for, while, until, if or case command or if the output is piped out.

4 4 Counting the number of lines in a file Solution #!/bin/sh #countLines3 filename=$1#Should check if arguments are given count=0 exec < $filename #any commands that will read from stand in # will read from $filename while read aline do count=`expr $count + 1` done echo "$filename has $count lines“ From the command line: $countLines3 data.txt Data.txt has 11 lines exec < /dev/tty--- Reassign the standard input back to terminal

5 5 trap l The pressing of DELETE key at the terminal, when a program is in execution, sends a signal to the executing program. l Using the trap command, the program can specify the action to be taken on receiving the signal. l Usage: trap commands signals, where commands are the actions to be taken on receiving the signals. l Some Commonly used signal numbers l 0Exit from Shell l 1 Hangup l 2Interrupt (eg: Delete key) l 15Software termination (sent by kill, for example)

6 6 Example Example 1: #!/bin/sh i=1 JUNK=junkfile trap ‘rm $JUNK$$;exit’ 2 while [ $i -le 100 ] Do # remove the file when interrupt is received echo $i >> $JUNK$$ i=`expr $i + 1` done

7 7 Examples Example 2: trap 'echo Caught SIGINT - exiting; exit' 2 X=0 while :#loop forever do echo "X=$X" X=`expr ${X} + 1` sleep 1 Done Example 3 #!/bin/sh trap 'echo `pwd` $$ >>./errdir' 2 3 15 while (true) do echo 'Hi' done

8 8 Example #!/bin/sh #What does this program do? trap 'increment' 2 increment() { echo "Caught SIGINT..." X=`expr ${X} + 500` if [ "${X}" -gt "2000" ] then echo "Okay, I'll quit..." exit 1 fi } ### main script X=0 while : do echo "X=$X" X=`expr ${X} + 1` sleep 1 done

9 9 Input and Output Redirection l We know that Unix shell allows us to redirect the input and output of programs using redirection (> and <) and piping(|). l When the Unix kernel starts any process, for example, grep, ls and so on, it sets up several places, called open files, for that process t read from and write to. Each of these files is given a number to identify with, called a file descriptor. l A file descriptor (also known as a file handle) is a non-negative digit that points at a file. l The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. Any of these may be redirected to a file or to each other. l In other words, it is quite possible to send the output from stdin and stderr to the same file. This is quite useful when a user would rather check a script's results after it has completed processing. l By default, the file that is opened for stdin, stdout and sterr is /dev/tty (your terminal). l But, when the shell starts a process, you can tell the shell what file to connect to any of those file descriptors. l For example in the following command, grep aPattern somefile > output, the file descriptor 1 is connected to the file, output.

10 10 More On I/O l What if you want to send the standard out to screen and capture the standard error in a pipe or a file? l It is easy to redirect any file descriptor to any file. Eg: command 2>errorFile command 2> file – redirects the standard error from any command cd JUNK 2>>out #the directory JUNK does not exist cat out sh: JUNK: not found.

11 11 More On I/O l What if you want to send the standard out to screen and capture the standard error in a pipe or a file? l It is easy to redirect any file descriptor to any file. Eg: command 2>errorFile command 2> file – redirects the standard error from any command cd JUNK 2>>out #the directory JUNK does not exist cat out sh: JUNK: not found. l Let us take a look at a few cases: l Sending both standard output and errors to the pipe or backquotes. command 2>&1 |… or var=`command 2>&1` This means that send standard error (with file descriptor 2) to the same place standard output is going(down the pipe or backquotes)

12 12 More On I/O l Sending stderr go down a pipe and stdout to the screen. command 2>&1 1>&2|…Will Not Work We should use file descriptors 3 to 9,as holding places, to accomplish this. command 3>&2 2>&1 1>&3 |… or var=` command 3>&2 2>&1 1>&3`

13 13 More On I/O Example: For the following grep command, assume that afile contains one matching line for “unix” and bfile does not exist. var=`grep “unix” afile bfile` echo $var#What is the output? var=`grep “unix” afile bfile 3>&2` echo $var#What is the output? var=`grep “unix” afile bfile 3>&2 2>&1` echo $var#What is the output? var=`grep “unix” afile bfile 3>&2 2>&1 1>&3` echo $var#What is the output?


Download ppt "1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0."

Similar presentations


Ads by Google