t*******l 发帖数: 421 | 1 要实现下列功能:进入目录bbs,显示所有subdir,从屏幕输入要考古的subdir,
进入该目录,浏览最后一片文章.大致是这样的.
cd bbs
ls
read -p " " answer
if ( -e $argv[1]) then
cd $argv[1]
pico lastpost
else
echo2 file $argv[1] doesn't exist
endif
学这个都两星期了,还是创不出来可以执行的小程序来.
请大虾指导指导. |
r********s 发帖数: 179 | 2 your "if...else" grammar seems to be C shell, unfortunately
"read" only works for bourne shell.
【在 t*******l 的大作中提到】 : 要实现下列功能:进入目录bbs,显示所有subdir,从屏幕输入要考古的subdir, : 进入该目录,浏览最后一片文章.大致是这样的. : cd bbs : ls : read -p " " answer : if ( -e $argv[1]) then : cd $argv[1] : pico lastpost : else : echo2 file $argv[1] doesn't exist
|
t*******l 发帖数: 421 | 3 ah?
I use C shell. then what should I do for the same purpose?
【在 r********s 的大作中提到】 : your "if...else" grammar seems to be C shell, unfortunately : "read" only works for bourne shell.
|
C**y 发帖数: 28 | 4 I think you need to read some csh intro on the internet.
I always forgot those stupid shell grammars, and can program a pretty good
one via the help of google in a short time.
【在 t*******l 的大作中提到】 : ah? : I use C shell. then what should I do for the same purpose?
|
r********s 发帖数: 179 | 5 [10:45pm] cat hoho.sh
#!/bin/sh
cd bbs
ls
echo -n "Which directory you wanna browse:\t "
read answer
if [ -d $answer ]
then
cd $answer
echo "The content of lastpost is:\n"
echo "******"
cat "/home/rhino/bbs/$answer/lastpost"
echo "******"
else
echo "$answer directory does not exitst!"
fi
[10:45pm] ls -R bbs
family love sysop
bbs/family:
lastpost
bbs/love:
lastpost
bbs/sysop:
lastpost
[10:46pm] ./hoho.sh
family love s
【在 t*******l 的大作中提到】 : ah? : I use C shell. then what should I do for the same purpose?
|
t*******l 发帖数: 421 | 6 thanks rhinoceros!
我又扩展了一下。如果/bbs/boards/family/里有吃,穿,住,用,行中的
若干项,是不是意味着要多几个if语句,以相同的格式?
我刚刚这样试了一下,不行:(
#!/bin/sh
cd bbs
ls
echo -n "Which directory you wanna browse:\t "
read answer
if [ -d $answer ]
then
cd $answer
echo "The content of lastpost is:\n"
echo "******"
cat "/home/rhino/bbs/$answer/lastpost"
echo "******"
else
if [ -d $answer2 ]
then
cd $answer2
ls
if [ -d $answer3 ]
then
cd $answer3
ls
【在 r********s 的大作中提到】 : [10:45pm] cat hoho.sh : #!/bin/sh : cd bbs : ls : echo -n "Which directory you wanna browse:\t " : read answer : if [ -d $answer ] : then : cd $answer : echo "The content of lastpost is:\n"
|
r********s 发帖数: 179 | 7
also remember you need a "fi" as the end of testing structure
【在 t*******l 的大作中提到】 : thanks rhinoceros! : 我又扩展了一下。如果/bbs/boards/family/里有吃,穿,住,用,行中的 : 若干项,是不是意味着要多几个if语句,以相同的格式? : 我刚刚这样试了一下,不行:( : #!/bin/sh : cd bbs : ls : echo -n "Which directory you wanna browse:\t " : read answer : if [ -d $answer ]
|
r********s 发帖数: 179 | 8 if condition1
then
command list if condition1 is true
[elif condition2
then command list if condition2 is true]
[else
command list if condition1 is false]
fi
FYI, the above shows the nested conditional structure.
【在 r********s 的大作中提到】 : : also remember you need a "fi" as the end of testing structure
|
f***y 发帖数: 98 | 9 yeah....
remove the line "read answer"
replace $answer, $answer2, $answer3 with $1, $2, $3...
【在 r********s 的大作中提到】 : if condition1 : then : command list if condition1 is true : [elif condition2 : then command list if condition2 is true] : [else : command list if condition1 is false] : fi : FYI, the above shows the nested conditional structure.
|