1 介绍
$?命令:可以得到上次执行命令的值,如果返回时0,说明执行成功,如果是其它数说明命令执行失败
联合使用命令:我们一般这样使用
value=$(cat 1.txt | grep chenyu)
echo $value
2 代码测试
#/bin/bash
pwd="pwd"
ll="l+"
$pwd
if [ $? -eq 0 ];
then
echo "pwd executed success"
else
echo "pwd executed fail"
fi
$ll
if [ $? -eq 0 ];
then
echo "l+ executed success"
else
echo "l+ executed fail"
fi
cmd=$(ls | cat -n);
echo $cmd
value=$(cat cmd.sh);
echo $value
3 运行结果
/home/chenyu/Desktop/linux/dabian/shell
pwd executed success
./cmd.sh: line 15: l+: command not found
l+ executed fail
1 1.txt 2 back 3 cmd.sh 4 cmd.sh~ 5 color.sh 6 color.sh~ 7 funcion.sh 8 funcion.sh~ 9 IFS.sh 10 IFS.sh~ 11 shuzu.sh 12 shuzu.sh~ 13 test.sh 14 test.sh~
#/bin/bash pwd="pwd" ll="l+" $pwd if [ $? -eq 0 ]; then echo "pwd executed success" else echo "pwd executed fail" fi $ll if [ $? -eq 0 ]; then echo "l+ executed success" else echo "l+ executed fail" fi cmd=$(ls | cat -n); echo $cmd value=$(cat cmd.sh); echo $value
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。