linux shell之$?和得到联合使用命令的结果
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
赞 (0)