linux shell之$?和得到联合使用命令的结果
生活随笔
收集整理的這篇文章主要介紹了
linux shell之$?和得到联合使用命令的结果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 介紹
$?命令:可以得到上次執行命令的值,如果返回時0,說明執行成功,如果是其它數說明命令執行失敗
聯合使用命令:我們一般這樣使用
value=$(cat 1.txt | grep chenyu) echo $value?
?
?
?
?
?
?
2 代碼測試
#/bin/bashpwd="pwd" ll="l+"$pwdif [ $? -eq 0 ]; thenecho "pwd executed success" elseecho "pwd executed fail" fi$llif [ $? -eq 0 ]; thenecho "l+ executed success" elseecho "l+ executed fail" ficmd=$(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?
總結
以上是生活随笔為你收集整理的linux shell之$?和得到联合使用命令的结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux shell之把文本里面的;替
- 下一篇: linux shell之IFS分割字符串