1. Write a shell script to check whether the name passed as rst argument is the name of a le or directory. Ans: #!/bin/bash if [ -f $1 ] echo "$1 is a le" echo "$1 is not a le" 2. Write a shell script to print the "long list" of all the le names passed at command line. Ans: #!/bin/bash for i in $* ls -l $i 3. Write a shell script to enter a le/directory name at command line. Change its permissions to user - rwx group - rwothers r-- Ans: #!/bin/bash if [ -f $1 ] chmod 764 $1 ls -l $1 elif [ -d $1 ] chmod 764 $1 ls -ld $1 echo "Not a le nor a directory" 4. Write a shell script to create a le and a directory. The name given as rst argument should be used for creating the le and the second argument should be used as the name of directory. touch $1 mkdir $2 1 P a g e
5. Write shell script to create a le inside a directory. The le name should be passed as rst argument and the directory name/path should be passed as second argument. mkdir $2 cd $2 touch $1 6. Write a shell script which checks the total number of arguments passed. If the argument count is greater than 5 display the message "Too many arguments". if [ $# -gt 5 ] echo "Too many arguments" echo "Less arguments" 7. Write a shell script which prints the count of total number of les and directories in any directory (name should be passed at command line) f1=0 d1=0 cd $1 for i in * if [ -f $i ] f1=$(( $f1 + 1 )) elif [ -d $i ] d1=$(( $d1 + 1 )) echo "Number of les: $f1" echo "Number of directories: $d1" 2 P a g e
8. WASS to check if the le names passed at command line by the user exits or not. If yes copy the contents of rst le into another le. if [ -f $1 ] if [ -f $2 ] cat $1 >> $2 echo "Successfully copied" echo "$2 es not exit" echo "$1 es not exist" 9. WASS to ask the user for a name. If the name starts with "f/f" create a le. If the name starts with "d/d" create a directory. For any other entry show a relevant message. Ans: #/bin/bash echo "Enter a name" read name ch=${name:0:1} if [ $ch == "f" ] [ $ch == "F" ] touch $name echo "A le whose name is $name is created" elif [ $ch == "d" ] [ $ch == "D" ] mkdir $name echo "A directory whose is $name is created" echo "You have entered $name" 3 P a g e
10. WASS that reads from command line. tf the argument passed starts with "F/f" a le should be created. if argument starts with "D/d" it should create a directory. Ans: #/bin/bash ch=${$1:0:1} if [ $ch == "f" ] [ $ch == "F" ] touch $name echo "A le whose name is $name is created" elif [ $ch == "d" ] [ $ch == "D" ] mkdir $name echo "A directory whose is $name is created" echo "You have entered $name" 11. WASS that reads from command line. If the number of arguments passed are: 4 - print a long list 5 - simply display the names if [ $# -eq 4 ] for i in $* ls -l $i elif [ $# -eq 5 ] for i in $* echo $i 12. WASS to count the number of sub-directories in a directory. nd. -type d wc -l 4 P a g e
13. WASS to count the number of les having read permission on them in a directory. count=0 for i in * if [[ -f $i ]] && [[ -r $i ]] count=$(( $count + 1 )) echo "Number of les having read permission : $count" 14. WASS to read ve le names from the user. Check if the user has entered the le name "HELLO". Show all the names while searching and Stop the script as soon this le name is encountered Ans : #!/bin/bash for((i=0; i<5; i++)) read NAME[$i] echo "Entries are" for((i=0; i<5; i++)) if [ `nd. -type f -name ${NAME[$i]}` ] if [ ${NAME[$i]} = "HELLO" ] echo ${NAME[$i]} break echo ${NAME[$i]} echo "${NAME[$i]} is not a le" 5 P a g e
15. WASS that reads from command line. Check whether the rst 3 argument names are all of existing les or not. for i in $1 $2 $3 if [ `nd. -type f -name $i` ] echo "$i exists" echo "$i es not exists" 6 P a g e