Name: Tej. D. Shah Subject:CC-304 Linux Uni. Practical programme College :L.J. College Of Computer Application Questions: Q.1 Check the output of the following commands:date, ls, who, cal, ps, wc, cat, uname, pwd, mkdir, rmdir, cd, cp, rm, mv, diff, chmod, grep, sed, head, tail,cut, paste, sort, nd, man Q.2 Write a script to nd the complete path for any le. Q.3 Write a shell script to execute following commands 1. Sort le abc.txt and save this sorted le in xyz.txt 2. Give an example of : To execute commands together without affecting result of each other. 3. How to print this is a three line 1. Text message 4. Which command display version of the UNIX? 5. How would u get online help of cat command? Q.4 Write a shell script to execute following commands 1. How would u display the hidden les? 2. How delete directory with les? 3. How would user can do interactive copying? 4. How would user can do interactive deletion of les? 5. Explain two functionality of mv command with example? Q.5 Write a shell script to execute following commands 1. Create a le called text and store name,age and address in it. 2. Display the contents of the le text on the screen. 3. Delete the directories mydir and newdir at one shot. 4. Sort a numeric le? 5. Change the permissions for the le newtext to 666. Q.6 Write shell script that accept lename and displays last modication time if le exists, otherwise display appropriate message. Q.7 Write a shell script to display the login names that begin with s. Q.8 Write a shell script to remove the zero sized le from the current directory
Q.9 Write a shell script to display the name of all the executable le from the current directory. Q.10 Write a shell script that will display welcome message according to time Q.11 Write a shell script to nd number of ordinary les and directory les. Q.12 Write a shell script that takes a lename from the command line and checks whether the le is an ordinary le or not. 1 If it is an ordinary le it should display the contents of the le. 2.If it is not an ordinary le script should display the message: File does not exist or is not ordinary, cannot display. Q.13 Write a shell script that takes a lename from the user and checks whether it is a directory le or not. 1 If it is a directory, the script should display the contents of the directory. 2.If it is not a directory le script should display the message: File is not a directory le Q.14 Write a shell script that takes a lename as an argument and checks if the le exists and is executable. 1If the le is executable the shell script should display the message: File exists 2 If the le does not exists and is not executable the script should display the message: File does not exist or is not executable. Q.15 Write a shell script that displays all subdirectories in current working directory. Q.16 Write a shell script that calculates the number of ordinary and directory les in your current working directory. Q17 Write a shell script that accepts 2 lenames and checks if both exists; if both exist append the content of the second le into the rst le. Q18 Write a shell script that takes the name of two les as arguments and performs the following: i. Displays the message : Displaying the contents of le :( rst argument) and displays the contents page wise. ii. Copies the contents of the rst argument to second argument. iii. Finally displays the message : File copied successfully. Q19 Write a shell script to display the following menu and acts accordingly: i. Calendar of the current month and year. ii. Display Good Morning/Good Afternoon/Good Evening according to the current login time. iii. User name, Users home directory. iv. Terminal name, Terminal type. v. Machine name. vi. No. of users who are currently logged in; List of users who are currently logged in.
Q.20 Write a shell script that displays the following menu and acts accordingly 1. Concatenates two strings 2. Renames a le 3. Deletes a le. 4. Copy the le to specic location Q.21 Write a shell script to change the sufx of all your *.txt les to.dat. Q.22 Write a shell script to accept a directory-name and display its contents. If input is not given HOME directory's contents should be listed. (Make use of command line argument) Q.23 Write a shell script to get all les of home directory and rename them if their names start with c. Newname = oldname111 Q.24 Write a shell script that takes two lename as arguments. It should check whether the contents of two les are same or not, if they are same second le should be deleted. Q.25 Write a shell script that accepts two directory names from the command line and copies all the les of one directory to another. The script should do the following _ If the source directory does not exist, flash a error message _ If destination directory does not exist create it _ Once both exist copy all the les from source directory to destination directory. Q.26 Write a shell script that displays the following menu _ List home directory _ Date _ Print working directory _ Users logged in Read the proper choice. Execute corresponding command. Check for invalid choice. Q.27 Write a shell script that displays all hidden les in current directory. Q.28 Write a shell script that Combine two les in the third le horizontally and vertically. Q.29 Write a shell script to delete all the spaces from a given le. Q.30 Write a shell script to nd a given date fall on a weekday or a weekend. Q.31 Write a shell script to search for a given word in all the les given as the arguments on the command line. Q.32 Write a shell script that display last modied le in the current directory. Q.33 Write a script to display the permissions of the particular le.
Q.34 Write a shell script to display the calendar in the following manner: i. Display the calendar of months m1 and m2 by CAL m1, m2 command le. ii. Display the calendar of the months from m1 to m2 by CAL m1-m2 command le. Q.35 Write a shell script to display the following menu for a particular le : i. Display all the words of a le in ascending order. ii. Display a le in descending order. iii. Toggle all the characters in the le. iv. Display type of the le. Q.36 Write a shell script to check whether the named user is currently logged in or not. Q.37 Write a shell script to display the following menu for a particular le: i.display all the words of a le in ascending order. ii.display a le in descending order. iii.display a le in reerse order. iv.toggle all the characters in the le v.display type of the le. Q.38 Write a shell script to nd total no. Of users and nds out how many of them are currently logged in. Q.39 Write a shell script that displays the directory information in the following format- Filename Size Date Protection Owner Q.40 Write a shell script to display ve largest les from the current directory Q.41 Write a shell script that toggles contents of the le Q.42 Write a shell script that report whether your friend has currently logged in or not. If he has logged in the shell script should send a message to his terminal suggesting a dinner tonight. If you do have write permission to his terminal or if he hasn t logged in such a message should be mailed to him about your dinner proposal. Q.43 Write a shell script for the performing the write and mail. Q.44 Write a shell script to accept any character using command line and list all the les starting with that character in the current directory. Q.45 Create a le called student containing roll-no, name and marks. a. Display the contents of the le sorted by marks in descending order b. Display the names of students in alphabetical order ignoring the case. c. Display students according to their roll nos. d. Sort le according to the second eld and save it to le names. e. Display the list of students who scored between 70 and 80. Note: Shell Script based on Files and lters should only to be asked in exam
Q.1 Check the output of the following commands:date, ls, who, cal, ps, wc, cat, uname, pwd, mkdir, rmdir, cd, cp, rm, mv, diff, chmod, grep, sed, head, tail,cut, paste, sort, nd, man Output:- abc@xyz-virtualbox:~$ date Thu Sep 4 11:38:40 IST 2014 abc@xyz-virtualbox:~$ ls Desktop Downloads Music Public Videos Documents examples.desktop Pictures Templates abc@xyz-virtualbox:~$ who xyz :0 2014-09-04 11:30 (:0) xyz pts/1 2014-09-04 11:38 (:0) abc@xyz-virtualbox:~$ cal September 2014 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 abc@xyz-virtualbox:~$ ps PID TTY TIME CMD 2598 pts/1 00:00:00 bash 2886 pts/1 00:00:00 ps abc@xyz-virtualbox:~$ ls wc 9 9 84 abc@xyz-virtualbox:~$ cat le1 Hello abc@xyz-virtualbox:~$ uname Linux abc@xyz-virtualbox:~$ pwd /home/xyz abc@xyz-virtualbox:~$ mkdir abc
abc@xyz-virtualbox:~$ rmdir abc abc@xyz-virtualbox:~$ cd Desktop abc@xyz-virtualbox:~$ cp le1 le2 abc@xyz-virtualbox:~$ mv le2 le3 abc@xyz-virtualbox:~$ grep * grep: Documents: Is a directory grep: Downloads: Is a directory examples.desktop:[desktop Entry] grep: Music: Is a directory grep: Pictures: Is a directory grep: Public: Is a directory grep: Templates: Is a directory grep: Videos: Is a directory abc@xyz-virtualbox:~$ head -5 le1 Hello hi sad sadnslad sadsad abc@xyz-virtualbox:~$ tail -2 le1 x zc abc@xyz-virtualbox:~$ cat le1 cut -d":" -f1 RollNO 1 2 3 abc@xyz-virtualbox:~$ sort le1 1 : xyz 2 : abc 3 : pqr RollNO :Name
abc@xyz-virtualbox:~$ paste le1 -d":" le3 RollNO:Name:Hello 1 : xyz: 2 : abc: 3 : pqr: Q.2 Write a script to nd the complete path for any le. echo "Enter name of le to nd the full path" read x readlink -f $x Q.3 Write a shell script to execute following commands 1. Sort le abc.txt and save this sorted le in xyz.txt 2. Give an example of : To execute commands together without affecting result of each other. 3. How to print this is a three line 1. Text message 4. Which command display version of the UNIX? 5. How would u get online help of cat command? select= until [ "$select" = "6" ]; do echo PROGRAM MENU echo 1 Sort le abc.txt and save this sorted le in xyz.txt echo 2 Give an example of:to execute commands together without affecting result of each other echo 3 How to print "this is a three -line Text message" echo 4 Which command display version of the UNIX? echo 5 How would u get online help of cat command echo 6 Exit echo Enter choice case $a in 1) echo File abc before sorting cat abc.txt echo File xyz after sorting sort abc.txt cat > xyz.txt
cat xyz.txt read x 2) echo Hi;cat le1 read y 3) printf "this is \na three -line\ntext message\n" read z read j 4) uname --version read e 5) echo man cat read f 6) exit ;; *) echo Please enter valid option read h esac done Q.4 Write a shell script to execute following commands 1. How would u display the hidden les? 2. How delete directory with les? 3. How would user can do interactive copying? 4. How would user can do interactive deletion of les? 5. Explain two functionality of mv command with example? select= until [ "$select" = "6" ]; do echo PROGRAM MENU echo 1 How would u display the hidden les? echo 2 How to delete directory with les? echo 3 How would user can do interactive copying? echo 4 How would user can do interactive deletion of les? echo 5 Expalin two functionalities of "mv" command with example? echo 6 Exit echo Enter choice
case $a in 1) ls -a read x 2) rm -r abc echo Directory with les removed read y 3) cp -i le4 le6 echo File1 copied to File2 read j 4) rm -i le4 le6 echo Both les removed read e 5) mv le5 le6 echo Renamed le5 as le6 read f mv Desktop Pictures read g 6) exit ;; *) echo Please enter valid option read h esac done Q.5 Write a shell script to execute following commands 1. Create a le called text and store name,age and address in it. 2. Display the contents of the le text on the screen. 3. Delete the directories mydir and newdir at one shot. 4. Sort a numeric le? 5. Change the permissions for the le newtext to 666. select= until [ "$select" = "6" ]; do echo PROGRAM MENU
echo 1 Create a le called text and store name,age and address in it. echo 2 Display the contents of the le text on the screen echo 3 Delete the directories mydir and newdir at one shot echo 4 Sort a numeric le? echo 5 Change the permissions for the le newtext to 666 echo 6 Exit echo Enter choice case $a in 1) echo Enter name,age and Address read name ge read Address (echo $name;echo $age;echo $Address) cat >> text.txt read g 2) cat text.txt read h 3) mkdir mydir;mkdir newdir echo Directories Created ls echo Directories removed rmdir mydir;rmdir newdir ls read j 4) sort -n no.txt read e 5) echo "This is new le" cat >> newtext chmod 666 newtext read g 6) exit ;; *) echo Please enter valid option read h esac done
Q.6 Write shell script that accept lename and displays last modication time if le exists, otherwise display appropriate message. echo PL enter lename if test -e $a ls -lt $a else echo File does not exist Q.7 Write a shell script to display the login names that begin with s. who grep s* Q.8 Write a shell script to remove the zero sized le from the current directory nd -size 0c -delete Q.9 Write a shell script to display the name of all the executable le from the current directory. nd -type f -executable -print Q.10 Write a shell script that will display welcome message according to time a="$(date +'%H')" if test $a -lt 12 echo Good Morning if test $a -gt 12 -a $a -lt 16 echo Good Afternoon if test $a -gt 16 -a $a -lt 20 echo Good Evening
if test $a -gt 20 echo Good Night Q.11 Write a shell script to nd number of ordinary les and directory les. echo Number of Ordinary les nd -type f wc -l echo Number of Directory les nd -type d wc -l Q.12 Write a shell script that takes a lename from the command line and checks whether the le is an ordinary le or not. 1 If it is an ordinary le it should display the contents of the le. 2.If it is not an ordinary le script should display the message: File does not exist or is not ordinary, cannot display. if test -f $1 echo $1 is Ordinary le echo $1 cat $1 else echo File is Directory Q.13 Write a shell script that takes a lename from the user and checks whether it is a directory le or not. 1 If it is a directory, the script should display the contents of the directory. 2.If it is not a directory le script should display the message: File is not a directory le echo Pl enter lename if test -d $a echo $a is Directory echo $a Contents
ls -l $a else echo File is not a Directory Q.14 Write a shell script that takes a lename as an argument and checks if the le exists and is executable. 1If the le is executable the shell script should display the message: File exists 2 If the le does not exists and is not executable the script should display the message: File does not exist or is not executable. if test -e $1 echo $1 exist else echo $1 does not exists if test -x $1 echo $1 is executable else echo $1 is not executable Q.15 Write a shell script that displays all subdirectories in current working directory. Output:- nd -type d Q.16 Write a shell script that calculates the number of ordinary and directory les in your current working directory. echo Number of Ordinary les nd -type f wc -l echo Number of Directory les nd -type d wc -l Q17 Write a shell script that accepts 2 lenames and checks if both exists; if both exist append the content of the second le into the rst le. echo PL enter rst lename
echo PL enter second lename read b if test -e $a -a -e $b cat $b >> $a Q18 Write a shell script that takes the name of two les as arguments and performs the following: i. Displays the message : Displaying the contents of le :( rst argument) and displays the contents page wise. ii. Copies the contents of the rst argument to second argument. iii. Finally displays the message : File copied successfully. echo Displaying the contents of rst argument page wise cat $1 more echo Copy the contents of rst argument to second argument read b cp $1 $2 echo File Copied Succesfully Q19 Write a shell script to display the following menu and acts accordingly: i. Calendar of the current month and year. ii. Display Good Morning/Good Afternoon/Good Evening according to the current login time. iii. User name, Users home directory. iv. Terminal name, Terminal type. v. Machine name. vi. No. of users who are currently logged in; List of users who are currently logged in. select= until [ "$select" = "7" ]; do echo PROGRAM MENU echo 1 Calendar of the current month and year echo 2 Display Good Morning/Good Afternoon/Good Evening according to the current login time. echo 3 User name,users home directory echo 4 Terminal name,terminal type echo 5 Machine name
echo 6 No of users who are currently logged in;list of users who are currenly logged in. echo 7 Exit echo Enter choice case $a in 1) cal read z 2) a="$(date +'%H')" if test $a -lt 12 echo Good Morning if test $a -gt 12 -a $a -lt 16 echo Good Afternoon if test $a -gt 16 -a $a -lt 20 echo Good Evening if test $a -gt 20 echo Good Night read x 3) echo Username;logname echo User Home directory;$home read c 4) echo "Terminalname" $SHELl echo Terminaltype tty read v 5) echo Machine name uname read b 6) echo List of users who are currently logged in who -q tail -1
read n 7) exit;; *) echo Please enter valid option read h esac done Q.20 Write a shell script that displays the following menu and acts accordingly 1. Concatenates two strings 2. Renames a le 3. Deletes a le. 4. Copy the le to specic location select= until [ "$select" = "5" ]; do echo PROGRAM MENU echo 1 Concatenates two strings echo 2 Renames a le echo 3 Deletes a le echo 4 Copy a le to specic location echo 5 Exit echo Enter choice case $a in 1) echo Enter string 1 echo Enter string 2 read b c=$a" "$b echo Concatenated Strin is $c read z 2) echo Renames a le mv le6 le7 echo File 6 renamed File7 successfully read g 3) echo Deletes a le rm le2 echo File2 removed successfully read j
4) echo Copy the le to specic location cp text.txt /home/xyz/downloads/xyz echo File text.txt succesfully Copied to xyz read v 5) exit;; *) echo Please enter valid option read h esac done Q.21 Write a shell script to change the sufx of all your *.txt les to.dat. echo Change Sufx of all your *.txt les to.dat for f in *.dat; do leftname=`basename $f dat` mv $f ${leftname}txt done Q.22 Write a shell script to accept a directory-name and display its contents. If input is not given HOME directory's contents should be listed. (Make use of command line argument) if test -d $1 ls $1 else ls $HOME Q.23 Write a shell script to get all les of home directory and rename them if their names start with c. Newname = oldname111 for f in c*; do mv $f r
done Q.24 Write a shell script that takes two lename as arguments. It should check whether the contents of two les are same or not, if they are same second le should be deleted. if test -e $1 -a -e $2 same=`diff $1 $2` if test -z $same rm $2 echo File 2 removed because contents of both the les were same else echo Both les are not same else echo File does not exist Q.25 Write a shell script that accepts two directory names from the command line and copies all the les of one directory to another. The script should do the following _ If the source directory does not exist, flash a error message _ If destination directory does not exist create it _ Once both exist copy all the les from source directory to destination directory. if test -d $1 echo Source Directory exists if test -d $2 cd $1 cp *.* $2\ else
mkdir $2 cd $1 cp *.* $2\ else echo Source Directory does not exist Q.26 Write a shell script that displays the following menu _ List home directory _ Date _ Print working directory _ Users logged in Read the proper choice. Execute corresponding command. Check for invalid choice. select= until [ "$select" = "5" ]; do echo PROGRAM MENU echo 1 List home directory echo 2 Date echo 3 Print working directory echo 4 Users logged in echo 5 Exit echo Enter choice case $a in 1) ls ~ read z 2) date read g 3) pwd read j 4) who read v 5) exit;; *) echo Please enter valid option read h
esac done Q.27 Write a shell script that displays all hidden les in current directory. ls -a Q.28 Write a shell script that Combine two les in the third le horizontally and vertically. select= until [ "$select" = "3" ]; do echo PROGRAM MENU echo 1 Vertically echo 2 Horizontally echo 3 Exit echo Enter choice case $a in 1) cat $1 > le9 cat $2 >> le9 read z 2) paste $1 $2 >> test2 read g 3) exit;; *) echo Please enter valid option read h esac done Q.29 Write a shell script to delete all the spaces from a given le. cat $1 tr -d " \t"
Q.31 Write a shell script to search for a given word in all the les given as the arguments on the command line. echo Enter the search word for i in $* do if test $a = $i echo Word $a Found on the command line argument done Q.32 Write a shell script that display last modied le in the current directory. ls -lt head -2 tail -1 cut -c 32-50 Q.33 Write a script to display the permissions of the particular le. ls -l $1 more cut -c 1-11,42-46 Q.36 Write a shell script to check whether the named user is currently logged in or not. echo Pl enter user name to check if user is currently logged-in b=`who cut -c 1-5 head -1` if test $a = $b echo User is logged-in else echo User is not logged-in Q.38 Write a shell script to nd total no. Of users and nds out how many of them are currently logged in. tot=`cat /etc/passwd wc -l` cur=`who wc -l`
echo Total users $tot echo Currently logged in users $cur Q.40 Write a shell script to display ve largest les from the current directory du -a sort -n -r head -5 ls -lsh head -6 tail -5 Q.42 Write a shell script that report whether your friend has currently logged in or not. If he has logged in the shell script should send a message to his terminal suggesting a dinner tonight. If you do have write permission to his terminal or if he hasn t logged in such a message should be mailed to him about your dinner proposal. echo Pl enter user name to check if user is currently logged-in b=`who cut -c 1-5 head -1` if test $a!= $b echo User is not logged-in else echo User is not logged-in talk $a"dinner tonight at my place" Q.44 Write a shell script to accept any character using command line and list all the les starting with that character in the current directory. ls -lt $1* Note: 30,34,35,37,39,41,43,45 Shell scripts are not there!