UNIX菜菜鸟:一个非常简单的shell script

mercury1231 2002-10-10 10:50:52
我写得不知道为什么总也通不过,不知道有哪位可以帮帮忙写出来让我看看,我自己错在什么地方了,多谢。虽然我知道这个很简单,但是我现在搞不掂,只好请大家帮帮忙拉。


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




...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mercury1231 2002-10-11
  • 打赏
  • 举报
回复
多谢楼上这位兄台相助。不过可能兄台没有看清楚题目,第三个要求是说当命令后面的参数超过一个的时候,应该怎么样处理。
blh 2002-10-11
  • 打赏
  • 举报
回复
$#表示你的参数个数,包括你的shell程序名
$0表示你的程序名
$1 $2 ....表示输入的参数
$*和$@都表示全体参数清单,使用
例子,打印所有输入参数
#!/bin/bash
for p in $@
do
echo $p
done
mercury1231 2002-10-11
  • 打赏
  • 举报
回复
#!/bin/csh
#This is a program to make a file executable, with some pre-judgement

if($#argv >1) then
echo "usage: chex2 <infile>"
exit

else if($1 == '') then
echo "usage: chex2 <infile>"
exit

else if(! -d $1 && ! -f $1) then
echo $1" does not exist."
exit

else if(-d $1) then
echo $1" is a Directory."
exit
endif

chmod u+x $1
echo $1 is now executable:
ls -l $1


终于写得通过啦。多谢楼上这位兄弟帮忙。希望以后能得到你的指教。
blh 2002-10-10
  • 打赏
  • 举报
回复
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
mercury1231 2002-10-10
  • 打赏
  • 举报
回复
虽然很简单,但还是请哪位大虾帮帮忙看看。
mercury1231 2002-10-10
  • 打赏
  • 举报
回复
嗬嗬,倒真不是懒,我自己真的基本上都不太懂UNIX,而且这个东西不是我们课上交的,只是发了几页纸让我们自己看看。就要写这个东西了。

以下是我自己写的,但是好像有错误,我真的是不知道出了什么错。


#!/bin/csh
#This is a program to make a file executable, with some pre-judgement

if(! -d $1 && ! -f $1) then
echo $1 does not exist.
exit
endif

if(-d $1) then
echo $1 is a Directory.
exit
endif

if($1 == '') then
echo usage: chex2 <infile>
exit
endif

if($#args >= 2) then
echo usage: chex2 <infile>
exit
endif

chmod u+x $1
echo $1 is now executable:
ls -l $1

bugfree 2002-10-10
  • 打赏
  • 举报
回复
信息都全了,不是写不出来, 是懒吧。。。

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧