More Shell Programming

19
More Shell Programmin g Software Tools

description

Software Tools. More Shell Programming. Keyword Shell Variables. The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where the shell looks for executables USER Your login name - PowerPoint PPT Presentation

Transcript of More Shell Programming

Page 1: More Shell Programming

More Shell

Programming

Software Tools

Page 2: More Shell Programming

Slide 2

Keyword Shell Variables

The shell sets keyword shell variables.

You can use (and change) them.HOME The path to your home directory

PATH Directories where the shell looks for executables

USER Your login name

SHELL The name of the shell you are running

PWD The current working directory

PRINTER Can be loaded with your default printer

Page 3: More Shell Programming

Slide 3

Keyword Example

$ cat env1#!/bin/shecho "Hi $USER!"echo "Your home is: $HOME"echo "Your path is: $PATH"echo "Your current directory is: $PWD"echo "Your shell is: $SHELL"echo "Your printer is: $PRINTER"$ env1Hi horner!Your home is: /homes/hornerYour path is:/usr/bin:.:.:/homes/horner/Unix/bin:...Your current directory is: /homes/horner/111Your shell is: /bin/tcshYour printer is:

Page 4: More Shell Programming

Slide 4

Readonly Shell Variables

$0 is the name the user typed to invoke the shell script:

$ cat print1#!/bin/shecho "This script is called $0"$ print1

This script is called print1$ ./print1This script is called ./print1$ ~/111/print1This script is called

/homes/horner/111/print1

Page 5: More Shell Programming

Slide 5

Command Line Arguments

The command line arguments that you call a script with are stored in variables $1, $2, ..., $9.

$ cat args1#!/bin/shecho "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9"$ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

The args are a1 a2 a3 a4 a5 a6 a7 a8 a9

With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

Page 6: More Shell Programming

Slide 6

Command Line Argument Example

How to write a command to swap two files?

$ cat swap#!/bin/shmv $1 /tmp/$1mv $2 $1mv /tmp/$1 $2

$ cat it1contents of file1$ cat it2contents of file2$ swap it1 it2$ cat it1contents of file2$ cat it2contents of file1$

Page 7: More Shell Programming

Slide 7

Command Line Arguments

$* lists all the command line args:$ cat args2#!/bin/shecho "The args are $*"$ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

$# contains the number of args:$ cat args3#!/bin/shecho "The number of args is $#"$ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

The number of args is 10

Page 8: More Shell Programming

Slide 8

shift

The shift command promotes each command line argument by one (e.g., the value in $2 moves to $1, $3 moves to $2, etc.)

$ cat shiftargs#!/bin/shecho "The args are 0 = $0, 1 = $1, 2 = $2"shiftecho "The args are 0 = $0, 1 = $1, 2 = $2"shiftecho "The args are 0 = $0, 1 = $1, 2 = $2"shift

$ shiftargs arg1 arg2 arg3The args are 0 = shiftarg, 1 = arg1, 2 = arg2The args are 0 = shiftarg, 1 = arg2, 2 = arg3The args are 0 = shiftarg, 1 = arg3, 2 =

The previous $1 becomes inaccessible

Page 9: More Shell Programming

Slide 9

shift Example

How to write a general version of the

swap command for two or more files?swap f1 f2 f3 ... fn_1 fn

f1 <--- f2f2 <--- f3f3 <--- f4...fn_1 <--- fnfn <--- f1

Page 10: More Shell Programming

Slide 10

shift Example$ cat swap1#!/bin/shorig1=$1mv $1 /tmp/$1while [ $2 ]do

mv $2 $1shift

donemv /tmp/$orig1 $1

$ cat it1 it2 it3contents of file1contents of file2contents of file3$ swap1 it1 it2 it3

$ cat it1 it2 it3contents of file2contents of file3contents of file1

Page 11: More Shell Programming

Slide 11

shift Example

$ swap1 it1 it2 it3$ cat it1 it2 it3

contents of file3contents of file1contents of file2$ swap1 it1 it2 it3

$ cat it1 it2 it3contents of file1contents of file2contents of file3$ swap1 it1 it2 $ cat it1 it2contents of file2contents of file1

Page 12: More Shell Programming

Slide 12

set

The set command sets the args:$ cat set1#!/bin/shset yat yih saamecho "One is $1, two is $2, three is $3"

$ set1One is yat, two is yih, three is saam

The set command is useful for moving the output of command substitution into the args:

$ dateThu Sept 22 17:06:27 HKT 2006$ cat day#!/bin/shset `date`echo "Today is $3 $2 $6"

$ dayToday is 22 Sept 2006

Page 13: More Shell Programming

Slide 13

$$

$$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive).

$ cat pid#!/bin/shecho $$$ pid1154$ pid1156$ pid1157

$ echo $$

892

$ ps

PID TTY TIME CMD

892 pts/0 0:01 csh

Page 14: More Shell Programming

Slide 14

$$ It can be used for temporary file names:

$ cat swap2#!/bin/shfile=/tmp/tmp$$mv $1 $filemv $2 $1mv $file $2$ cat it1 it2

contents of file1contents of file2$ swap2 it1 it2$ cat it1 it2contents of file2contents of file1$

Page 15: More Shell Programming

Slide 15

for

The for statement executes a loop once for each of a list of possibilities:

$ cat printall#!/bin/shfor file in *do

if [ -f $file ]then

echo "Print $file [y/n]? "read respif [ $resp = "y" ]then

lpr –Pcll2a $filefi

fidone

$ printallPrint it1 [y/n]?yPrint it2 [y/n]?n

Page 16: More Shell Programming

Slide 16

for

If the “in ___” part is omitted, it defaults to $*:

$ cat p#!/bin/shfor filedo

if [ -f $file ]then

lpr –Pcll2a $filefi

done$ p it1 it2 it3$

Page 17: More Shell Programming

Slide 17

for Example

This program will do a recursive listing of all directories. The first argument is the directory to start from. An optional second argument controls the number of leading “:” characters.

$ cat ldrec#!/bin/shcd $1if [ $2 ]then

space=$2else

space=0fispace1=`expr $space + 1`

Page 18: More Shell Programming

Slide 18

for Example for f in *do

if [ -d $f ] then i=1 while [ $i -le $space ] do echo –n ":" i=`expr $i + 1` done echo $f /homes/horner/111/ldrec $1/$f $space1 fi

done

The -n in echo will prevent a newline from printing.

Page 19: More Shell Programming

Slide 19

for Example $ ls -Fletter1 secret/ secret1/ secret2/ secret3/ secret4/ secretslink@

$ ldrec /homes/horner/111

secret

:verysecret

::veryverysecret

secret1/homes/horner/111/ldrec: /homes/horner/111/secret1: permission denied

secret2/homes/horner/111/ldrec: /homes/horner/111/secret2: permission denied

secret3

secret4

secretslink

:verysecret

::veryverysecret

$