当前位置: 技术问答>linux和unix
UNIX菜菜鸟:一个非常简单的shell script
来源: 互联网 发布时间:2014-12-25
本文导语: 我写得不知道为什么总也通不过,不知道有哪位可以帮帮忙写出来让我看看,我自己错在什么地方了,多谢。虽然我知道这个很简单,但是我现在搞不掂,只好请大家帮帮忙拉。 1. Check if the file exists 2. Check if...
我写得不知道为什么总也通不过,不知道有哪位可以帮帮忙写出来让我看看,我自己错在什么地方了,多谢。虽然我知道这个很简单,但是我现在搞不掂,只好请大家帮帮忙拉。
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
$ chex2 test123 extra_arg extra_arg2
usage: chex2
$ chex2
usage: chex2
以下是老师的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
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
$ chex2 test123 extra_arg extra_arg2
usage: chex2
$ chex2
usage: chex2
以下是老师的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
|
$#表示你的参数个数,包括你的shell程序名
$0表示你的程序名
$1 $2 ....表示输入的参数
$*和$@都表示全体参数清单,使用
例子,打印所有输入参数
#!/bin/bash
for p in $@
do
echo $p
done
$0表示你的程序名
$1 $2 ....表示输入的参数
$*和$@都表示全体参数清单,使用
例子,打印所有输入参数
#!/bin/bash
for p in $@
do
echo $p
done
|
1。
#!/bin/bash
if [ -f $1 ];
then
echo "$1 is exist!"
else
echo "$1 is not exist!"
fi
2。
#!/bin/bash
if [ -d $1 ];
then
echo "$1 is directory!"
else
echo "$1 is not directory!"
fi
3。
#!/bin/bash
if [ "$1" == "1234" ];
then
echo "The inputing number is right!"
else
echo "The inputing number is error!"
fi
#!/bin/bash
if [ -f $1 ];
then
echo "$1 is exist!"
else
echo "$1 is not exist!"
fi
2。
#!/bin/bash
if [ -d $1 ];
then
echo "$1 is directory!"
else
echo "$1 is not directory!"
fi
3。
#!/bin/bash
if [ "$1" == "1234" ];
then
echo "The inputing number is right!"
else
echo "The inputing number is error!"
fi