shell control statements and more cs465 - unix. while statement while [ condition ] do command(s)...

26
Shell Control Statements and More CS465 - UNIX

Upload: calvin-bishop

Post on 18-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Shell Control Statementsand More

CS465 - UNIX

Page 2: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

while statement

while [ condition ]do

command(s)

done

• Same condition syntax as if statement

• Begin and end of command block defined by keywords do...done

• Loops while condition is TRUE

Page 3: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

while Loop Example #1$ cat wake#!/bin/shresp="n"while [ "$resp" != "y" ]doecho "Wakeup [y/n]? "read resp

done$$ wakeWakeup [y/n]? nWakeup [y/n]?YWakeup [yes/no]?y$

Page 4: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

while Loop Example #2$ cat fac

#!/bin/sh

echo "Enter number: "

read num

fac=1

loop=1

while [ $loop -le $num ]

do

fac=`expr $fac \* $loop`

loop=`expr $loop + 1`

done

echo "The factorial of $num is $fac"$$ facEnter number:5The factorial of 5 is 120$

Page 5: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

while example #3$ cat lineswhile [ "$input" != done ]do

echo 'Enter a filename, or "done":'read inputif [ "$input" != done ]then

lines=`wc –l < $input`echo "$input has $lines lines"

fidone$$ linesEnter a filename, or "done":shoesshoes has 12 linesEnter a filename, or "done":done$

Page 6: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

$ cat sayhi#! /bin/sh# $* = list of namescount=$#while [ count -gt 0 ]do echo Hello $1

count=`expr $count - 1`shift

doneexit 0$

while example #4

sayhi Sue Joe BobHello SueHello JoeHello Bob$

Page 7: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Student Exercise• Write a shell script called up, that will move you

up in the directory structure– If no arguments, move up ONE directory– If one argument, it should be a number, telling how

many directories to move up

• Usage Example:$ pwd/usr/home/faculty/small000$ . up 2$ pwd/usr/home$ . up$ pwd/usr

Page 8: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Exercise Sample Solution#! /bin/sh# $1 = number of levels to go up# (if no parameters, go up one level)#if [ $# -eq 0 ]then count=1else count=$1fiwhile [ $count -gt 0 ]do cd .. count=`expr $count - 1`doneexit 0

Page 9: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

for Statement

• The for statement is used to repeat commands for known or “fixed” values

• Unlike C programming, the for loop usually repeats tasks for “arguments” that are either issued from the script or a stated directory after for statement.

Page 10: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

for statement

for variable in listdo

command(s)

done

• variable is a variable name; don't use $

• list is a space-separated list of strings. Each element of the list will be assigned to variable one at a time, and the command block will be executed.

• Within the command block, use $variable to use the value that has been assigned.

Page 11: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

for example #1$ cat colorscript

#!/bin/sh

for color in red yellow blue

do

echo $color

done

echo "the end"

$ colorscript

red

yellow

blue

the end

$

Page 12: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

for example #2

$ userdirs jmsmith krjones

Directory listing for: jmsmith

cprogs/ dotask* xfile

diskfile mbox

Directory listing for: krjones

mbox prog1.c prog2.c

$

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

$ cat userdirs#!/bin/shfor user in $*do echo Directory for: $user

ls -F /home/${user}done$

Page 13: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

$ printall letter1 namesPrint letter1 [y/n]?yPrint names [y/n]?n

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

if [ -f $file ]then

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

lpr $filefi

fidone

$

Page 14: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

case statement

case string inchoice) command(s)

;;choice)command(s)

;;esac

• Very similar to C switch statement: executes the commands after a choice that matches string.

• Double-semicolon ends a block of commands, like the C break statement. If you skip the ;; you will keep going into the next block .

• * as a choice will match any string, and can be used to set a default

• esac ends the statement.

Page 15: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

case Example #1echo Enter command and filenameread cmd filecase "$cmd" in

list)ls -l "$file";;

count)wc -l "$file";;

*)echo "command $cmd is not implemented";;

esac

Page 16: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

$ cat yesno#! /bin/shecho –n 'Yes or No (y/n)? 'read choicecase $choice in"Y" | "y") echo You chose Yes;;"N" | "n") echo You chose No;;*) echo Invalid choice;;

esacexit$

case Example #2

yesnoYes or No (Y/N)? NYou chose No$

Page 17: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

The following will be a script to:Give user a choice of what to do with the files listed as arguments:

Copy to a subdirectoryConcatonate

or Delete

Carry out the action chosen, prompting for the subdirectory or file to concatonate into, as needed.Display a message confirming action was done.

case Example #3

Page 18: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

$ cat files#! /bin.ksh# script parameters = files to operate oncat << STOP M) Move Files C) Concatonate Files D) Delete FilesEnter choice:STOPread choice

(Continued on next slide)

case Example #3

Page 19: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

case $choice in "m"|"M") echo Move files to which subdir? read subname if [ ! -d $subname ]

then mkdir $subname

fi mv $* $subname

echo Files moved to subdir $subname ;;

"c"|"C") echo File to place concatonation in? read fname if [ -f $fname ] then

echo Error - $fname already exists else

cat $* > $fnameecho Files concated into $fname

fi ;;

case Example #3 (continued)

Page 20: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

"d"|"D") rm $*echo Files $* have been deleted ;;

*) echo Invalid Choice -- No Can Do ;;

esacexit 0$

case Example #3 (continued)

files file1 file2 M) Move Files C) Concatonate Files D) Delete FilesEnter choice:CFile to place concatonation in?comboFiles concated into combo$

Page 21: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

How the Shell Finds a Command

• The shell searches a list of directories for an executable file with the same name as the command given.

• The list of directories is stored in the PATH variable $ PATH=/bin:$PATH (sh/ksh)

• If there is a match in more than one directory, the shell uses the first one it finds.

• To run a command not in one of these directories, give a pathname (relative or absolute) instead.

$ ~/progs/dosomething

Page 22: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Built-In Commands• Some commands are built into the shell kernel.

– The echo command, for example, is often builtin, for efficiency.

• You can find out where the shell is getting a particular command using the “which” command in any shell:

$ which echo

echo: shell built-in command.

$ which cat

/usr/bin/cat

$

Page 23: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

More Environment VariablesPredefined Environmental Variables:

PPID shell’s parent’s process id

IFS list of command line word delimiters (default is list is space, tab & newline)

PS1 your shell prompt (default is $)

PS2 your input prompt (default is >)

SHENV directory where your .profile is located (default is $HOME)

Page 24: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Files Run at Login

• System-wide login file

/etc/profile

• Personal login file$HOME/.profile

• Personal environment file– Set by $ENV

Usually $HOME/.kshrc

Page 25: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Sample Bourne .profile

# Set PATHPATH=$PATH:.export PATH# Set environmental variable fileENV=$HOME/.envsetup# Set shell variablesPS1='Command? '# Display status informationdateecho "Currently logged in users:“users

Page 26: Shell Control Statements and More CS465 - UNIX. while statement while [ condition ] do command(s) done Same condition syntax as if statement Begin and

Directory in prompt

To include directory in prompt, put these lines in your .profile file:

PS1='$PWD $ '

or

PS1="`pwd`$ "

export PS1