Thursday, September 22, 2011

Please need help with Unix/Linux homework?

1) Suppose you've created a shell script named /usr/bin/myscript. How can you prevent a user from executing this script? Should you remove the executable permission from the script file?



2) If the shell variable x has the value 5, and your reassign it with x=';expr $x + 10';, what is the new value of x? What would have been the value if single quotes were used instead? What is the most likely mistake in the above?



3) A script named test containing the df and du commands displays nothing (not even an error message) when executed. Why might that happen? State two ways of making this script behave properly.



4) Write a script that accepts one or more filenames as arguments, and displays the filenames in uppercase like this:

$ upcase file1 readme README

FILE1 README README

[Hint: Use tr and positional parameters.]



5) If the command set $(cat foo) generates the error unknown option, what could be the reason assuming that foo is a small readable text file?



6) Write a script which accepts one filename as an argument, and displays the last modification time if the file exists and a suitable error message if it doesn檛.

[Hints: It is acceptable to display the whole output of ls -l file. Remember the file argument is accessed by using a positional parameter. Use the test command in an if statement to see if a file exists.]

To extract just a file's modification time from the ls -l output, you can set LC_TIME environment variable to POSIX; then the modification time is in the 6th, 7th, and 8th fields of the output. You can extract that by resetting the positional parameters from the output of the ls -l, using set and command substitution. (There are easier ways but they require knowledge of awk or other advanced filter commands.)



7) Why won檛 the exit command terminate the script, when placed in a shell script like this:

( statements; exit ) How would you fix this, so the script exits after running the statements?



8) You have a small script called cd2man containing these lines:

#!/bin/sh -

x=$(find $HOME -name $1 -print)

cd $x The script has correct permissions and PATH is set correctly so the script can be found. When you run cd2man man1 you find that the current directory hasn檛 changed even though the directory man1 exists somewhere in your home directory tree. No error messages appeared either. Why did that happen, and what should you do to make cd2man change the current working directory?



9) You have to run a script foo and need to have both the output and error messages saved in the single file named foo.out. How should you run the script?



10) Write a shell script for rm called saferm which uses the interactive mode only if you use it with more than three filenames. [Hint: use an appropriate if statement that runs rm -i ';$@'; when the number of command line arguments is greater than three. ]Please need help with Unix/Linux homework?
Most of these you can answer yourself very quickly by just typing the examples into a terminal window on your *nix system.



One important point to note though is that you do not seem to have specified what shell you are supposed to be using. I'm guess it's bash or maybe just sh ?Please need help with Unix/Linux homework?
can you give me that a kinder grade level please

Report Abuse

No comments:

Post a Comment