|
Unix - Pages 128 - 155 Shell Scripts 1) This script will beep twice when invoked. internet% cat beep #!/bin/csh -f ################################################ # This script beeps twice at our terminal ################################################ echo -n '^G' sleep 1 echo -n '^G' 2) this script prints the lines of a file that exceds 80 characters. internet% cat longlines #!/bin/csh -f ################################################ # Print lines bigger than 80 characters # Usage: longlines < file ################################################ awk "length > 80" 3) This script showed the number of files and directories in the current directory internet% cat numfiles #!/bin/csh -f ################################################ # Count number of files and directories in current directory # Usage: numfiles ################################################ echo -n "files and directories: " ls | wc -l internet% numfiles files and directories: 31 4) This script showed the number of files and directories in the directory passed as parameter internet% cat numfiles #!/bin/csh -f ################################################ # Count number of files and directories in the directory # specified as parameter # Usage: numfiles directory ################################################ echo -n Number of files/directories in $argv[1]\. ls $argv[1] | wc -l internet% numfiles . Number of files/directories in .. 31 internet% numfiles ~ Number of files/directories in /home2/stud-30/spantaro. 14 internet% numfiles ~/cis316 Number of files/directories in /home2/stud-30/spantaro/cis316. 21 5) This script moved cursor to ~/cis316 directory and listed all the lines of the files ended in "sql" that
had the word create.
internet% cat searcher#!/bin/csh -f ########################################### # Search directory for pattern in all # files (argument 3 - last) # Usage: searcher directory pattern file(s) ########################################### cd $argv[1] # change directory to argument 1 grep -n "$argv[2]" $argv[3-$#argv] | more internet% searcher ~/cis316 create "*.sql" ctCustomer.sql:3:create table Customer ( ctOrderLines.sql:3:create table OrderLines ( ctOrders.sql:3:create table Orders ( ctProduct.sql:3:create table Product ( ctSequences.sql:6:create sequence ProductIDSeq increment by 1 start with 1; ctSequences.sql:7:create sequence ShippingMethodIDSeq increment by 1 start with 1; ctSequences.sql:8:create sequence ShippingAddressIDSeq increment by 1 start with 1; ctSequences.sql:9:create sequence CustomerIDSeq increment by 1 start with 1; ctSequences.sql:10:create sequence OrdersIDSeq increment by 1 start with 1; ctShippingAddress.sql:3:create table ShippingAddress ( ctShippingMethod.sql:3:create table ShippingMethod ( makeDB.sql:3:@createAll internet% internet% searcher ~/cis316 create ctCustomer.sql ctProduct.sql ctCustomer.sql:3:create table Customer ( ctProduct.sql:3:create table Product ( internet% 6) Checked and Listed lines in a C program that contained hex number. The value of $status is zero because the search was successful. internet% cat hexnum #!/bin/csh -f #################################################### # List lines in a C program that contain hex number # Usage: hexnum file #################################################### grep "0[xX][0-9a-fA-F]*" $argv[1] internet% hexnum Cfile fkjhgd 0x kjhkjdfhjkgd hsgfsjh adshgsfj 0xa internet% echo $status 0 internet% 7) Checked and Listed lines in a C program that contained hex number. The value of $status is two because the file passed as parameter does not exist. The 'test' command checks for the file and exit with 2 as $status. internet% cat hexnum #!/bin/csh -f #################################################### # List lines in a Cprogram that contain hex number # Usage: hexnum file #################################################### test -r $argv[1] || exit 2 grep "0[xX][0-9a-fA-F][0-9a-fA-F]*" $argv[1] internet% hexnum blablabla internet% echo $status 2 internet% 8) Checked and Listed lines in a C program that contained hex number. The value of $status is two because the file passed as parameter does not exist. The 'test' command checks for the file and exit with 2 as $status. before exiting it echoes a message to the user saying that file was not found. internet% cat hexnum #!/bin/csh -f #################################################### # List lines in a C program that contain hex number # Check for file name and exit in case file does # not exist. Set the status = 2 in case of error # Usage: hexnum file #################################################### test -r $argv[1] || (echo "can't read argV[1]."; exit 2) grep "0[xX][0-9a-fA-F][0-9a-fA-F]*" $argv[1] internet% hexnum somefile can't read argV[1]. grep: can't open somefile internet% echo $status 2 internet% 9) Checked and Listed lines in a C program that contained hex number. The value of $status is two because the file passed as parameter does not exist. The 'test' command checks for the file and exit with 2 as $status. before exiting it saves a message into a log file saying that the file was not found. internet% hexnum blabla >> hexlog grep: can't open blabla internet% echo $status 2 internet% cat hexlog can't read blabla. internet% 10) Checks to see if a user is logged in, and if not it invokes the mail program. internet% cat wmail #!/bin/csh -f
####################################################
# csh script to send mail or converse with a user
# Usage: wmail username
####################################################
set w = (`who | grep $argv[1]`)
if ($#w == 0) then
echo "$argv[1] is not logged in...send mail"
mail $argv[1]
endifinternet% wmail mbennani mbennani is not logged in...send mail Hi Mehdi, this is just to test my wmail shell script bye sio 11) Added feature to talk to the code above internet% cat wmail #!/bin/csh -f
####################################################
# csh script to send mail or converse with a user
# Usage: wmail username
####################################################
set w = (`who | grep $argv[1]`)
if ($#w == 0) then
echo "$argv[1] is not logged in...send mail"
mail $argv[1]
else
echo "$argv[1] logged in...let's talk"
write $argv[1]
endif internet% who mharriso pts/0 Jul 16 21:30 (ACA7DC6C.ipt.aol.com) spantaro pts/2 Jul 16 22:29 (12.47.49.130) internet% wmail mharriso mharriso logged in...let's talk hi Write failed (mharriso logged out?) internet% who spantaro pts/2 Jul 16 22:29 (12.47.49.130) internet% 12) When trying to check in how many terminals the user was logged in, I got
the following error.internet% cat wmail #!/bin/csh -f
####################################################
# csh script to send mail or converse with a user
# Usage: wmail username
####################################################
set w = (`who | grep $argv[1]`)
if ($#w == 0) then
echo "$argv[1] is not logged in...send mail"
mail $argv[1]
else if ($#w == 5) then
echo "$argv[1] logged in...let's talk"
write $argv[1]
else
echo "$argv[1] logged in on ports $w[2] and $w[7]"
endifinternet% who ktakahas pts/0 Jul 16 23:25 (63-93-100-131.oak.dial.netzero.com) spantaro pts/2 Jul 16 22:29 (12.47.49.130) internet% wmail ktakahas Subscript out of range internet% 13) Now the script searcher also checks to see if the user informed all the
parameters necessary to execute the code.internet% cat searcher #!/bin/csh -f
###########################################
# Search directory for pattern in all
# files (argument 3 - last)
# Usage: searcher directory pattern file(s)
###########################################
if ($#argv < 3) then # less than 3 args?
echo "Usage: searcher directory pattern file(s)"
exit 1
endif
cd $argv[1] # change directory to argument 1
grep -n "$argv[2]" $argv[3-$#argv] | moreinternet% searcher ~/cis316 Usage: searcher directory pattern file(s) internet% 14) Script longlines works with arguments or no arguments. In case of no arguments
it reads from the standard input.internet% cat longlines internet% cat longlines
#!/bin/csh -f
##################################################
# Print lines bigger than 80 characters
# Usage: longlines [file]
##################################################
if ($#argv == 0) then # no arguments
awk "length > 80" # read from standard output
else
echo $argv[1]\:
awk "length > 80" $argv[1] #use filename passed in $argv[1]
endifinternet% cat testlonglines | longlines jfhgjfhdkgjdhfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffuytiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoooooooooo ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo jhgkfdglkjfdljgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggg internet% longlines testlonglines testlonglines: jfhgjfhdkgjdhfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffuytiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoooooooooo ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo jhgkfdglkjfdljgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggg internet% 15) Script numfiles now counts number of files and directories in current
directory or first argument specified as parameterinternet% cat numfiles #!/bin/csh -f
##############################################################
# Count number of files and directories in current directory
# or first argument specified as parameter
# Usage: numfiles [directory]
##############################################################
if ($#argv == 0) then
set dir = "."
else
set dir = $argv[1]
endif
echo -n Number of files/directories in $dir is\:
ls $dir | wc -l
internet% numfiles
Number of files/directories in . is: 37internet% numfiles ~/cis316 Number of files/directories in /home2/stud-30/spantaro/cis316 is: 21 internet% 16) It was supposed to display hours, minutes, and seconds but I am not sure
it worked properly. tval[3] does not seems to be number of seconds at all.internet% cat timer #!/bin/csh -f ########################################### # Display hours, minutes, and seconds # Usage: timer ########################################### set tval = `uptime` # call the uptime program echo "The value of tval is: " $tval @ time = $tval[3] # get the number of seconds echo "The value of time is: " $time @ hrs = $time / 3600 # calculate number of hours echo -n "$hrs hours " @ time = $time % 3600 @ mins = $time / 60 # calculate number of minutes echo -n "$mins minutes " @ secs = $time % 60 # calculate number of seconds echo -n "$secs seconds" echo "" internet% timer The value of tval is: 12:12am up 10 day(s), 13:31, 3 users, load average: 0.02, 0.03, 0.04 The value of time is: 10 0 hours 0 minutes 10 seconds internet% 17) Display hours, minutes, and seconds dropping unnecessary 0s. internet% more timer
#!/bin/csh -f
###########################################
# Display hours, minutes, and seconds
# Usage: timer
###########################################
set tval = `uptime` # call the uptime program
echo "The value of tval is: " $tval
@ time = $tval[3] # get the number of seconds
echo "The value of time is: " $time
if ($time == 0) then
echo timer reset
exit
endif
@ hrs = $time / 3600 # calculate number of hours
if ($hrs != 0) then
echo -n "$hrs hours "
if ($hrs > 1) echo -n "s"
endif@ time = $time % 3600
@ mins = $time / 60 # calculate number of minutes
if ($mins != 0) then
echo -n "$mins minutes "
if ($mins > 1) echo -n "s"
endif
@ secs = $time % 60 # calculate number of seconds
if ($secs != 0) then
echo -n "$secs seconds"
if ($secs > 1) echo -n "s"
endif
echo "" # print a blank line
18) Script longlines reads from standard input if no argument is passed.
If an argument is passed it checks whether it is a plain file.internet% cat longlines
#!/bin/csh -f
##################################################
# Print lines bigger than 80 characters
# Usage: longlines [file]
##################################################
if ($#argv == 0) then # no arguments
awk "length > 80" # read from standard output
exit 0
endif
if (-f $argv[1]) then # is argument a plain file?
echo $argv[1]\: # yes; display name and call
awk "length > 80" $argv[1] # awk with the file name
else # argument not a plain file
echo $0\: No file $argv[1] # display error message and
exit 1 # exit with error status
endif
internet% internet% longlines japonese longlines: No file japonese internet% longlines xyz longlines: No file xyz internet% longlines < testlonglines jfhgjfhdkgjdhfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffuytiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoooooooooo ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo jhgkfdglkjfdljgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggg internet% longlines testlonglines testlonglines: jfhgjfhdkgjdhfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffuytiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoooooooooo ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo jhgkfdglkjfdljgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggg 19) Script numfiles now ensures that $dir is a directory before process it. internet% cat numfiles #!/bin/csh -f
##############################################################
# Count number of files and directories in current directory
# or first argument specified as parameter
# Usage: numfiles [directory]
##############################################################
if ($#argv == 0) then # no argument passed
set dir = "." # use current directory
else
set dir = $argv[1]
endif
if (! -d $dir) then # is $dir a directory?
echo $0\: $dir not a directory. # no - display error
exit 1 # exit with error status
endif
echo -n Number of files/directories in $dir is\:
ls $dir | wc -l # process directory $dir
internet% internet% numfiles Number of files/directories in . is: 43 internet% numfiles ~ Number of files/directories in /home2/stud-30/spantaro is: 15 internet% numfiles japonese numfiles: japonese not a directory. internet% 20) This script uses a foreach loop to send invitations to a party.
It pipes the text between quotes to command mail followed by a username.internet% cat mailInvitation #!/bin/csh -f
foreach username($*)
echo "Dear $username, \
You are cordially invited to my party. \
You can not miss it." | mail $username
end
internet% 21) Got the following error: internet% nfiles if: Expression syntax internet% cat nfiles
#!/bin/csh -f
###############################################################
# Script to number and format all files in argument list
# Usage : nfiles file(s)
# Author: Siomara pantarotto
# date : July, 24, 2001
##############################################################
if ($#argv ==0) then # arguments passed?
echo "Usage: $0 file(s)" # no - display error message
exit 1 # and exit
endif
foreach file ($argv[*]) # expand argument wordlist
if (! -z $file) then # file not empty?
cat $file | more -h $file # yes; number and format
else
echo "$file empty." # no - inform the user
endif
end
internet% |