閱讀947 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Linux Shell 基礎 -- 結構化命令

簡單if-then

#!/bin/bash
if pwd
then
echo "It worked"
fi

這個腳本在 if 行采用了 pwd 命令。如果命令成功結束, echo 語句就會顯示該文本字符串。在
命令行運行該腳本時,會得到如下結果
image
shell執行了 if 行中的 pwd 命令。由於退出狀態碼是 0 ,它就又執行了 then 部分的 echo 語句。
下麵是另外一個例子

if IamNotaCommand
then
echo "It worked"
fi
echo "We are outside the if statement"

image
if-then-else 語句

testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
echo "The bash files for user $testuser are:"
ls -a /home/$testuser/.b*
echo
else
echo "The user $testuser does not exist on this system."
echo
fi

image
嵌套 if

最後更新:2017-10-07 12:33:00

  上一篇:go  情感分析教程+心法!如何用 Apache MXNet 看懂電影影評
  下一篇:go  Minikube - Kubernetes本地實驗環境