UNIX菜菜鸟:一个非常简单的shell script
我写得不知道为什么总也通不过,不知道有哪位可以帮帮忙写出来让我看看,我自己错在什么地方了,多谢。虽然我知道这个很简单,但是我现在搞不掂,只好请大家帮帮忙拉。
1. Check if the file exists
2. Check if the input argument is a Directory, instead of a file.
3. Check if right number of argument is entered.
Here are a few sample runs of the shell script chex2.
这是这个scripts运行时的几个输入例子。
$ ls -l
-rwxrwxr-x 1 ngyat ngyat 293 Oct 3 22:22 chex2*
-rw-rw-r-- 1 ngyat ngyat 8 Oct 3 22:36 test123
drwxrwxr-x 2 ngyat ngyat 4096 Oct 3 22:20 try/
$ chex2 test123
test123 is now executable:
-rwxrw-r-- 1 ngyat ngyat 8 Oct 3 22:36 test123
$ chex2 try
try is a Directory.
$ chex2 file_no_exits
file_no_exits does not exist.
$ chex2 test123 extra_arg
usage: chex2 <infile>
$ chex2 test123 extra_arg extra_arg2
usage: chex2 <infile>
$ chex2
usage: chex2 <infile>
以下是老师的Hints,高手不看也罢(我是傻的看了都没用)。
Hints:
1. Argument Variables:
$n [where n = 1, 2, ... n] The argument value follows the command.
e.g. if we type: my_command a1 a2
then, $0 = my_command ,$1 = a1 and $2 = a2
$#argv report the number of words in the input list
e.g. if we type: my_command a1 a2
we will have $#argv equals 2
2. To test attribute of a file, you may want to use the following table:
Argument Test if it is a ...
-d file file is a directory
-f file file is an ordinary file
-r file file is reable
-w file file is writable
-x file file is executable
Note: you only need to use TWO of the above arguments, NOT all.
The following are two examples of using the above attributes:
a) to check if a file is readable,
if (-r $filename) then
...
endif
b) to check if a file is NOT writable,
if (! -w $filename) then
...
endif
3. Use exit command to get out of the if loop and the script
4. Here is one sample portion of logic of your shell script. [Hint: There should be three of this if part].
if no input file name then
print error message
exit ## this command will terminate the script and lets you get back to the shell prompt
endif