shell编程的一个问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
kandyche
帖子: 11
注册时间: 2005-11-25 11:01

shell编程的一个问题

#1

帖子 kandyche » 2007-06-12 15:53

#!/bin/sh
if cp test test1
then echo“cp done”
else echo “cp fail”
fi


cp 成功不是返回0 吗?
为什么这个if 后执行的是1?
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#2

帖子 eexpress » 2007-06-12 15:56

判断返回值。是$?吧
● 鸣学
kandyche
帖子: 11
注册时间: 2005-11-25 11:01

#3

帖子 kandyche » 2007-06-12 15:59

我知道判断返回值是成功是0
我意思是为什么这个if 后面的条件值为1(cp 成功后)
kandyche
帖子: 11
注册时间: 2005-11-25 11:01

#4

帖子 kandyche » 2007-06-12 15:59

我知道判断返回值是成功是0
我意思是为什么这个if 后面的条件值为1(cp 成功后)
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15

#5

帖子 iblicf » 2007-06-12 16:08

test 是保留字,不知道你说什么意思,

请你看看《提问的智慧》,不要在这里发什么家庭作业式的问题
kandyche
帖子: 11
注册时间: 2005-11-25 11:01

#6

帖子 kandyche » 2007-06-12 16:14

不知道就别回啊,我的意思是cp test文件到test1
我是不理解if (cp test test1)这个条件为什么会成立?????
因为正如2楼说cp成功应该返回0
thword
帖子: 119
注册时间: 2007-04-09 12:19

#7

帖子 thword » 2007-06-12 16:20

$echo test >test
$cp test test1
$echo $?
0
$rm test
$cp test test1
cp: 无法确认 ‘test’: No such file or directory
$echo $?
1
完全没有问题
$echo '#!/bin/sh
if cp test test1
then echo "cp done"
else echo "cp fail"
fi' >test.sh
$echo test >test
$sh test.sh
cp done
$rm test
$sh test.sh
cp: 无法确认 ‘test’: No such file or directory
cp fail
也没有问题

不知你想表达什么
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15

#8

帖子 iblicf » 2007-06-12 16:23

废话,如果有test 文件在当前目录,cp当然成功,,if 本身就是判断 0 为真
kandyche
帖子: 11
注册时间: 2005-11-25 11:01

#9

帖子 kandyche » 2007-06-12 16:46

我想表达的就是 if 判断的时候为什么0(cp 成功返回值) 为真??
kandyche
帖子: 11
注册时间: 2005-11-25 11:01

#10

帖子 kandyche » 2007-06-12 17:22

知道答案了,谢谢各位。
回复