Redian新闻
>
看看我的第一个小script为什么不工作
avatar
看看我的第一个小script为什么不工作# Unix - 噫吁兮,危乎高哉
t*l
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
学这个都两星期了,还是创不出来可以执行的小程序来.
请大虾指导指导.
avatar
r*s
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

avatar
t*l
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.

avatar
C*y
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?

avatar
r*s
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?

avatar
t*l
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"

avatar
r*s
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 ]

avatar
r*s
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

avatar
f*y
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。