Sei sulla pagina 1di 16

#1-lsl #This file lists the current directory and echo's a string ls -la | more echo 'enter your

next command' ********************************************************************** # 2-copy # This program lists the number of arguments, and then names the files # then copies the first file to the next three files. echo 'There are ' $# ' named files' echo $* echo 'all except ' $1 ' are target files' cp $1 $2 cp $1 $3 cp $1 $4

**********************************************************************

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 3-menu #Lists a menu and takes input from the keyboard clear echo ' which course do you wish to attend ' echo ' ' echo -n '1 UNIPLEX wordprocessing echo '4 C programming for beginners ' ' '

echo -n '2 Advance C programming echo '5 Shell programming for unix ' ' '

echo -n '3 System Analysis echo echo ' ' '6 COBOL course

echo -n ' Type the number and press enter ' read course_number echo 'Sorry! course number '$course_number 'is booked out for this session' ********************************************************************** # 4-userinput

# Asks for input from keyboard and displays on screen.

clear echo 'Please type your first name' read fname echo 'Please type your last name' read lname echo 'Hello, ' $fname echo $lname ' is a very aristocratic last name ' echo 'Thanks for coming'
NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

********************************************************************** # 5-iftestfi #Input from keyboard and tests input, then uses 'if' statements clear echo 'Please type your first name' read fname echo 'Please type your last name' read lname echo 'Hello, ' $fname if (test $lname != 'Smith') then echo $lname ' is a very aristocratic last name ' fi echo -n 'type your age in years: ' read age if (test $age -gt 30) then echo 'you are a senior citizen.' fi if (test $age -lt 29) then echo 'you are a minor' fi ********************************************************************** # 6-caseesac

clear echo 'which Department do you wish to use' echo '' echo -n '1 - Department A. ' echo
NORMAL.DOT Rev G

'4 - Department B. '

echo -n '2 - Accounts Dept. '


F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

echo

'5 - Typing Dept. '

echo -n '3 - Personnel Dept.' echo echo ' ' echo -n ' press the number and press enter ' read dept case $dept in 1) echo 'you want Department A ';; #2) echo 'you want Department B ';; 2) cat shell_script|more;; 3) echo 'you want the Accounts Dept. ';; 4) echo 'you want the Typing Dept. ';; 5) echo 'you want the Personnel Dept. ';; 6) echo 'you want the Sales Dept. ';; *) echo $dept ' invalid ';; esac ********************************************************************** # 7-ifelifelse '6 - Sales Dept. '

clear echo 'enter your age' read age if (test $age -lt 21) then echo 'You are a minor' elif (test $age -gt 65) then echo 'You are a senior citizen' else echo 'you are a relatively standard age' fi
NORMAL.DOT Rev G

**********************************************************************
F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 8-print

#Print command - tests if an ordinary file, then directs to printer

if (test -f "$1" = 1) then cat "$1" > /dev/lp0 else echo "" echo "missing filename" echo "Usage : print filename" echo "" fi ********************************************************************** # 9-d

# Lists files to doc. and runs Word Count on file - counts # the number of files in a directory. cd $1 ls -F > /tmp/count1

wc -l /tmp/count1 cd /tmp rm count1 **********************************************************************

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 10-cdir # Tests if file is a directory, and changes dir and lists if (test -d "$1" = "1") then cd $1 ls -l else echo "usage : cdir dir-name" fi ********************************************************************** # 11-lexe # Tests if file a directory, lists dir to temporary file 'x' # and greps all executable files(with * following file name) if (test -d "$1" = "1") then cd $1 ls -F> /tmp/x grep "*$" /tmp/x rm /tmp/x else echo "You forgot the directory name" echo "" fi **********************************************************************

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 12-ldir # Lists all sub directories of named directory if test -d "$1" = 1 then cd $1 ls -F> /tmp/x grep "/$" /tmp/x rm /tmp/x else echo "You forgot the directory name" echo "" fi # 13-switch1

clear echo 'Run menu Y/N' read ans while (test $ans = 'Y') do clear echo '' echo '' echo 'which Department do you wish to use' echo '' echo -n '1 - Department A. ' echo '4 - Department B. '

echo -n '2 - Accounts Dept. ' echo


NORMAL.DOT Rev G

'5 - Typing Dept. '

echo -n '3 - Personnel Dept.'


F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

echo echo echo ' '

'6 - Sales Dept. 'x - exit'

'

echo -n ' press the number and enter ' read dept case $dept in 1) echo 'you want Department A ' echo 'Return to continue' read ret;; #2) echo 'you want Department B ';; 2) cat shell_script | more echo 'Return to continue' read ret;; 3) echo 'you want the Accounts Dept. ' echo 'Return to continue' read ret;; 4) echo 'you want the Typing Dept. ' echo 'Return to continue' read ret;; 5) echo 'you want the Personnel Dept. ' echo 'Return to continue' read ret;; 6) echo 'you want the Sales Dept. ' echo 'Return to continue' read ret;; x) break;; *) echo $dept ' invalid ';; esac
NORMAL.DOT Rev G

done
F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 14-switch2 clear echo 'Run menu Y/N' read ans while (test $ans = 'Y'|| test $ans = 'y') do clear echo '' echo '' echo 'which command do you wish to use' echo '' echo -n '1 - List all users logged on echo '4 - List all processes ' '

echo -n '2 - List files in current Directory ' echo '5 - List files in another directory' '

echo -n '3 - List sub-directories only echo echo echo ' ' echo -n ' press the number and enter ' read commd case $commd in 1) echo '' '6 - List disk useage ' 'x - exit'

echo 'LISTING ALL USERS LOGGED ON' echo '' who read ret;; 2) echo ''
NORMAL.DOT Rev G

echo 'LISTING ALL FILES '


F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

echo '' ls -la read ret;; 3) echo '' echo 'LISTING SUB DIRECTORIES' echo 'enter parent directory name' read nam echo '' ldir $nam read ret;; 4) echo '' echo 'LISTING ALL PROCESSES' echo '' ps -deaf | more read ret;; 5) echo '' echo 'LISTING FILES IN ANOTHER DIRECTORY' echo '' echo 'Enter directory name' read dir ls -la $dir | more read ret;; 6) echo '' echo 'LISTING DISK USEAGE' echo '' df -v read ret;; [Xx]) break;;
NORMAL.DOT Rev G

*) echo $commd ' invalid ';;


F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

esac done

********************************************************************** # 15-fordo # source takes the value of $1, the 'shift' operator # moves on to $2, then $3 etc. for each pass of the loop. source=$1 shift for target do cp $source $target done ********************************************************************** # 16-chperm

# A is a variable which takes the value of all $*(all files) # fo each pass of the for loop.

for A in $* do chmod 744 $A ls -l $A done **********************************************************************

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 17-until

clear echo 'Enter a value' read val

until (test $val) do echo 'You must input a valid number' echo 'Enter a value' read val done

echo 'The value entered was ' $val **********************************************************************

# 18-tel for $Word do grep $Word < tellist done -----------------------------------#tellist fred mick dec kevin
NORMAL.DOT Rev G

2850939 371331 2844045 371929

****************************************************************
F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

# 19-scrap

# This program will delete all executable files in the specified directory # which have the permissions (755), ie -rwxr-xr-x. # These are usually files which are created with the 'C' compiler.

echo echo echo " echo echo " echo " echo -n " read yn if (test $yn = 'y') then if test -d $1 then find $1 -perm -755 -exec rm {} \; else echo not a valid directory fi echo finished fi THIS PROGRAM WILL DELETE ALL EXECUTABLE FILES" IN THE SPECIFIED DIRECTORY " W A R N I N G"

--- ARE YOU SURE YOU WISH TO CONTINUE?--> "

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

################################################################ # 20-subtract

# This program uses the 'expr' Expression evaluator to convert # strings to integers to run arithmetic operators. # NOTE all 'read' inputs are accepted as string values

clear echo "SUBTRACT TWO NUMBERS" echo -n "Enter first number :- " read amnt1 echo echo -n "Enter second number :- " read amnt2

numb=`expr $amnt1 - $amnt2` # expr $amnt converts the value of amnt to an integer # this command must be enclosed within accent marks and spaces are required echo echo echo "The answer is " $numb

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

SPECIAL SHELL VARIABLES

$0 ---$# ---$$ ---$* ---$@ ---$- ---$? ----

The name of the command being executed. The number of positional characters. The process number of this shell. List of positional characters. Alternative list of positional characters. Current set of flags for the shell. Exit status of last command executed.

Note : The difference between $@ and $* is important but not obvious. There is no difference unless they are enclosed in double quotes.

$* --> $1 $2 $3 $4 ......$n. $@ --> $1 $2 $3 $4 ......$n. "$*" --> "$1 $2 $3 $4 .......$n" "$@" --> "$1" "$2" "$3" "$4" ......"$n"

TESTS ON FILE TYPES

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

Test -s ---- Checks that file exists and is not empty. -f ---- Checks that the file is an ordinary file. -d ---- Checks that the file is a Directory. -w ---- Checks that the file is writeable. -r ---- Checks that the file is readable. -x ---- Checks that the file is executable. Returns 1 if true Returns 0 if false. Tests on files can be combined for further checking. ie. test -f-w filename.

INTEGER EXPRESSIONS. Test -gt --- greater than -ge --- greater than or equal to. -eq --- equal to. -ne --- is not equal to. -le --- is less than or equal to. -lt --- is less than. !-lt --- is not less than. (the exclamation mark can be used with any of the above to negate the operator)

NORMAL.DOT Rev G

F:\GV\GV_B\LPL_UPLOAD\COURSEDB\B_A_COURSEWARE\COURSES FROM AOIFE\LZUBB108170_UNIX_FUNDAMENTALS\R1A\LEARNING_COMPONENTS\03802\BOURNE_SCRIPT_EXAMPLES.DOC

Potrebbero piacerti anche