Exercises for shell script & perl (chapter10, due on 12/30 before the class) 1. Write a script to check whether a file exists and is readable. If the number of argument is not one, then print “Usage: chkfile file” and exit. Otherwise, print “File exists but is not readable”, “File is readable”, or “File doesn’t exist” correspondingly. 2. Write an argument check shell script, called argCheck.sh, so that we can check the arguments (using $#) to the command. The script expects one or two arguments. If the number of arguments is incorrect, it displays an error message 3. and terminates with exit status 1. If we have two arguments, we set the variables from and to. If we have only one argument, then it becomes the value of to. Compose a command search script, called cmdSearch.sh, using for and if constructs. The script locates a command, which is executable, on the command search path ($PATH) and displays its full pathname. If the command is not found, 4. it displays a message “$cmd not on $PATH”. Write a script, called append.sh, to append either the standard input or a file to the end of the other file. For example, $append.sh file will ask for input line by line, put in the file until ctrl+d, and $append.sh file1 file2 will appends file1 to the end of file2. 5. 6. Write a script, named comc.sh, to automatically select the last C program, link to three filenames: vic, comc, runc, and then it edits, compiles, and executes the selected file. That is, vi filename.c, filename (as an executable file), and cc -o filename filename.c. You need to check if the existence of a C program in the current directory. Once you have finished the coding, the only thing left is to create three links: ln comc.sh comc; ln comc.sh runc; and ln comc.sh vic. (hint: use expr to remove the suffix .c and use case for the choices) Write a one-liner perl instruction to (i) change the she-bang line to #!/usr/local/bin/perl in all .pl files in the current directory. (ii) convert all characters in a file, called foo, to uppercase, and write the changes back to the same file without using redirection. 7. 8. Write a perl program to convert a binary number specified as argument to decimal. (hint: use the reverse function,) Write a perl program that changes the login shell of users in /etc/passwd. The shell has to be changed to /bin/bash for all users with UID greater than 100 and whose current shell is /usr/bin/pdksh or /bin/ksh. The output has to be written to a separate file, named passwdChanged, in the current directory. 1
© Copyright 2026 Paperzz