Kuvempu University: Laboratory Assignments Subject: UNIX & Shell Programming Subject Code: BSIT - 43
Kuvempu University: Laboratory Assignments Subject: UNIX & Shell Programming Subject Code: BSIT - 43
Laboratory Assignments Subject: UNIX & Shell Programming Subject Code: BSIT - 43
1. Use the echo command to display the line UNIX is fun to learn Ans : echo UNIX is fun to learn Redirect the displayed line to a file. Append the outputs of the commands to show the users logged into your system, your login account and the current date in the dd-mm-yyyy format to the file.
c) remove write privileges from everyone except the owner -rwxrr-d) allow the owner and group members to execute myfile and give only the owner permission to read or write to it. -rwxrwxr-4. For a directory myfolder in your working directory, do the following: a) allow everyone to list files in myfolder. No other privileges are to be changed. drrr b) allow the owner to and group members to list, remove or add files. All privileges are to be removed from everyone else. drwxrw-r b) give write privileges to everyone except the owner drwxrw-r-d) allow the owner and group members to execute myfolder. Only the owner gets read or write permission. drwxrwxrrw5. Put a long-running process in the background and check the accuracy of the sleep command.
Any process can be placed in the background by using the '&' at the end of the command, or by suspending the process and issuing the 'bg' command. There really is no good way to 'measure' the accuracy of the 'sleep' command because it is not designed to be accurate; it depends on the state of the system and other things going on at the same time. It uses an approximate value to suspend the process but is not designed to be extremely accurate. The value used in the sleep command is the number of seconds to suspend the process, which is subject to other processes running, possibly at a higher level than the current one. Since the process is probably not running at a real time level you wouldn't be able to check the acccuracy of the suspended time with any reliability.
6. The ls command with the R option lists the files in the specified directory and also subdirectories, if any. Use this with a suitable pip-and-filter arrangement to determine whether a file exists in the account logged in. subhashis@ubuntu:~$ cat > kp apple zebra boy cat
girl ls -R |sort kp | tee Sort apple boy cat girl zebra 7. Write an interactive shell using I/O redirection to accept input from file1, put stdout in file2 and stderr in file3.
echo read echo read -n "Enter soruce file name : " src -n "Enter target file name : " targ
if [ ! -f $src ] then echo "File $src does not exists" exit 1 elif [ -f $targ ] then echo "File $targ exist, cannot overwrite" exit 2 fi # copy file cp $src $targ # store exit status of above cp command. It is use to # determine if shell command operations is successful or not status=$? if [ $status -eq 0 ] then echo 'File copied successfully' else echo 'Problem copuing file' fi
8. Write a shell program to extract and sort all the users in the file system. Use the /etc/passwd file as input. Ans: cut -d ":" -f 1 /etc/passwd| sort 9. Write a shell program to translate the /etc/passwd file into uppercase and translate the : delimiter into tab characters. Ans : tr "[a-z]"":" "[A-Z]""\t" < /etc/passwd 10. Write a shell program using if-the-else to test whether a variable name is a directory or a file.
11. Write a shell program in which an infinite loop prompts a user for file names to be removed and remove the files. Use trap to exit when finished.
while true do echo -n Filename ': ' read aFile rm $aFile done 12. Write a background command to edit all the files in the directory replacing the word
while with until. tr 'while' 'until' < myfile &
13. Write a shell program to test for arguments arg1, arg2 and arg3. If they are not present, prompt the user for them.
1[/tmp]$ cat try.ksh 14. #! /bin/ksh 15. 16. 17. if [[ -n "$1" ]] ; then 18. echo '$1'="[$1]" 19. else 20. echo '$1' is empty 21. fi 22. if [[ -n "$2" ]] ; then 23. echo '$2'="[$2]" 24. else 25. echo '$2' is empty 26. fi 27. if [[ -n "$2" ]] ; then 28. echo '$3'="[$3]" 29. else 30. echo '$3' is empty 31. fi 32. [/tmp]$ ./try.ksh 33. $1 is empty 34. $2 is empty 35. $3 is empty 36. [/tmp]$ ./try.ksh 12 " " " 37. $1=[12] 38. $2=[ ] 39. $3=[ ] 40. [/tmp]$
"
14. Write a shell program that adds the integers 1 to 100 and displays the result with a suitable message.
#!/bin/bash x=1 t=0 while [ $x -le 100 ] do t=`expr $t + $x` x=`expr $x + 1` done echo $t
15. Using awk, sum the file sizes in your working directory and print the results.
$ ls -l | awk '/^-/ {total+=$5} END{print "Total of all file sizes "total}'
16. Using a case statement, write a shell program to read a command (eg., who, cal, ls, ps) from the user and execute it.
echo "Enter a number between 1 for who 2 for cal 3 for ls 4 for ps " read NUM case $NUM in 1) who; 2) cal 3) ls; 4) ps; *) echo "INVALID NUMBER!" ;; esac