while [ condition ] while [ condition ] do done
$ cat wake #!/bin/sh resp="n" while [ "$resp" != "y" ] do echo "Wakeup [y/n]? " read resp done $
$ cat fac $ 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"
- $
$ cat lines $ cat lines while [ "$input" != done ] do - echo 'Enter a filename, or "done":'
- read input
- if [ "$input" != done ]
- then
- lines=`wc –l < $input`
- echo "$input has $lines lines"
- fi
done $
$ cat sayhi #! /bin/sh # $* = list of names count=$# while [ count -gt 0 ] do echo Hello $1 count=`expr $count - 1` shift done exit 0 $
Write a shell script called up, that will move you up in the directory structure 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
#! /bin/sh #! /bin/sh # $1 = number of levels to go up # (if no parameters, go up one level) # if [ $# -eq 0 ] then count=1 else count=$1 fi while [ $count -gt 0 ] do cd .. count=`expr $count - 1` done exit 0
The for statement is used to repeat commands for known or “fixed” values 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.
for variable in list for variable in list do done
$ cat colorscript $ cat colorscript #!/bin/sh do echo $color done echo "the end" $ colorscript red yellow blue the end $
$ userdirs jmsmith krjones $ userdirs jmsmith krjones Directory listing for: jmsmith cprogs/ dotask* xfile diskfile mbox Directory listing for: krjones mbox prog1.c prog2.c $
$ printall letter1 names $ printall letter1 names - Print letter1 [y/n]?
- y
- Print names [y/n]?
- n
case string in case string in choice) command(s) ;; choice) command(s) ;; esac
echo Enter command and filename echo Enter command and filename read cmd file case "$cmd" in - list)
- count)
- *)
- echo "command $cmd is not implemented"
- ;;
esac
$ cat yesno #! /bin/sh echo –n 'Yes or No (y/n)? ' read choice case $choice in "Y" | "y") echo You chose Yes;; "N" | "n") echo You chose No;; *) echo Invalid choice;; esac exit $
The following will be a script to: The following will be a script to: Give user a choice of what to do with the files listed as arguments: Copy to a subdirectory Concatonate 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.
$ cat files $ cat files #! /bin.ksh # script parameters = files to operate on cat << STOP M) Move Files C) Concatonate Files D) Delete Files Enter choice: STOP read choice (Continued on next slide)
case $choice in 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? if [ -f $fname ] then echo Error - $fname already exists else cat $* > $fname echo Files concated into $fname fi ;;
"d"|"D") rm $* "d"|"D") rm $* echo Files $* have been deleted ;; *) echo Invalid Choice -- No Can Do ;; esac exit 0 $
The shell searches a list of directories for an executable file with the same name as the command given. 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
Some commands are built into the shell kernel. 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
- $
Predefined Environmental Variables: Predefined 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)
System-wide login file System-wide login file /etc/profile Personal login file $HOME/.profile Personal environment file - Set by $ENV
- Usually $HOME/.kshrc
# Set PATH PATH=$PATH:. export PATH # Set environmental variable file ENV=$HOME/.envsetup # Set shell variables PS1='Command? ' # Display status information date echo "Currently logged in users:“ users
To include directory in prompt, put these lines in your .profile file: To include directory in prompt, put these lines in your .profile file: PS1='$PWD $ ' or PS1="`pwd`$ " export PS1
Dostları ilə paylaş: |