Lab 9-10 Shell Script Requirement
** Module 9 - Script in Korn Shell ** #!/bin/ksh # Name: ABC, Lab9 # Module 9 # 1. Accept exactly 1 command line argument, or print a Usage message and exit with error code 1 if (( $# != 1 )) then echo "Incorrect argument - Usage: ./lab9 testDirectory" exit 1 fi dirName=$1 absdirName=`readlink -f "$dirName"` #echo "Given directory path as an argument" $dirName #echo "Absolute path of entered directory" $absdirName # readlink provides absolute path ############################## # Function 1: 2. Call a function that checks for a valid directory. A valid directory is a user’s home directory or any of its subdirectories, so here are all the conditions to check: o it must be a directory o it must be a user’s home directory or any of the subdirectories below - When either of these conditions is not met, print a separate error message and exit with error code 1 validDirectory( ) { if [ ! -e "$dirName" ] then echo "Directory does not exist" exit 1 fi if [ ! -d "$dirName" ] then echo "Invalid Directory" exit 1 fi typeset validDir1="/home/student/"`whoami` # typeset -- local variable --> local keyword in bash typeset validDir2="/home/staff/"`whoami` #echo "Dir1: " $validDir1 " Dir2: " $validDir2 #For this lab, a user’s home directory is in either /home/student/ or /home/staff/. The user may not always enter an absolute path (such as /home/etc), and instead enters a relative path (such as module9 or ../..). You must be able to catch the case when the user is at the home directory, but entered .. , which means the given directory is above the home directory and is an error. On the other hand, if the user is at the home directory and enters . (for the current directory), then it is a valid directory. if [ `echo "$absdirName" | grep -c "$validDir1"` -gt 0 ] # make grep output quite (no message) and silent (no error display) then echo -n "" #echo "Valid directory - Student" elif [ `echo "$absdirName" | grep -c "$validDir2"` -gt 0 ] then echo -n "" #echo "Valid directory - Staff" else echo "can only search at your home directory" exit 1 fi } ############################## # Function 2: Print Result printResult( ) { echo "Found " $bashFilesIndex " bash script files in " $dirName trap '' INT # ignore control-c typeset tmpcounter=0 for bashFile in ${bashFiles[@]} do (( tmpcounter=tmpcounter+1 )) echo $bashFile if (( $tmpcounter==5 )) then sleep 3 tmpcounter=0 fi done trap - INT # signal handling is reset to default --> syntax for ksh shell is different than bash shell } ############################## # Main Function # validDirectory # Function 1 Call typeset bashFilesIndex=0 allFiles=`find "$dirName" -type f` #echo ${allFiles[@]} for file in ${allFiles[@]} do line1=`head -1 "$file"` #echo "File: " $file "Line1: " $line1 if [[ ! -z $line1 ]] then if [[ $line1 == "#!/bin/bash" ]] then #echo "Match: " $line1 bashFiles[$bashFilesIndex]=$file (( bashFilesIndex=$bashFilesIndex+1 )) fi fi done #echo ${bashFiles[@]} printResult # Function 2 call to display result ############################## ** Module 10 - Script in C Shell ** #!/bin/tcsh # Name: ABC, Lab10 # Module 10 # 1. Accept exactly 1 command line argument, or print a Usage message and exit with error code 1 if (( $# != 1 )) then echo "Incorrect argument - Usage: ./lab10 testDirectory" exit 1 endif set dirName = "$1" set absdirName = `readlink -f "$dirName"` #echo "Given directory path as an argument" $dirName #echo "Absolute path of entered directory" $absdirName # readlink provides absolute path ############################## # Main Function # # validDirectory # Function 1 Call ############################## # Copied Function 1 into main for tcsh and modified: 2. Call a function that checks for a valid directory. A valid directory is a user’s home directory or any of its subdirectories, so here are all the conditions to check: o it must be a directory o it must be a user’s home directory or any of the subdirectories below - When either of these conditions is not met, print a separate error message and exit with error code 1 if ( ! -e "$dirName" ) then echo "Directory does not exist" exit 1 endif if ( ! -d "$dirName" ) then echo "Invalid Directory" exit 1 endif set validDir1="/home/student/"`whoami` # typeset -- local variable --> local keyword in bash set validDir2="/home/staff/"`whoami` #echo "Dir1: " $validDir1 " Dir2: " $validDir2 #For this lab, a user’s home directory is in either /home/student/ or /home/staff/. The user may not always enter an absolute path (such as /home/etc), and instead enters a relative path (such as module9 or ../..). You must be able to catch the case when the user is at the home directory, but entered .. , which means the given directory is above the home directory and is an error. On the other hand, if the user is at the home directory and enters . (for the current directory), then it is a valid directory. if { ( echo "$absdirName" | grep -c "$validDir1" > /dev/null ) } then # make grep output quite (no message) and silent (no error display) echo -n "" #echo "Valid directory - Student" else if { ( echo "$absdirName" | grep -c "$validDir2" > /dev/null ) } then echo -n "" #echo "Valid directory - Staff" else echo "can only search at your home directory" exit 1 endif ############################## @ bashFilesIndex = 0 set allFiles = `find "$dirName" -type f` set bashFiles = #echo ${allFiles[*]} # instead of @ changed to * (list) for tcsh foreach tmpfile ($allFiles) # changed from for to foreach .. end instead of no do...done set line1 = `head -1 "$tmpfile"` #echo "File: " $tmpfile "Line1: " $line1 # echo $?line1 # check varible is set or not if ( ${%line1} != 0 ) then # if ( ! -z "$line1" ) then if ( "$line1" == "#\!/bin/bash" ) then #echo "Match: " $line1 @ bashFilesIndex = $bashFilesIndex + 1 #set bashFiles[$bashFilesIndex]=$tmpfile set bashFiles = ( $bashFiles $tmpfile ) endif endif end #echo ${bashFiles[*]} # instead of @ changed to * (list) for tcsh ############################## # Copied Function 2: Print Result here for tcsh script echo "Found " $bashFilesIndex " bash script files in " $dirName onintr - #trap '' INT # ignore control-c @ tmpcounter = 0 foreach bashFile ($bashFiles) #for bashFile in ${bashFiles[@]} @ tmpcounter = $tmpcounter + 1 echo $bashFile if ( $tmpcounter == 5 ) then sleep 3 @ tmpcounter = 0 endif end onintr #trap - INT # signal handling is reset to default --> syntax for ksh shell is different than bash shell ############################## |