Linux Shell 基礎 -- 結構化命令
簡單if-then
#!/bin/bash
if pwd
then
echo "It worked"
fi
這個腳本在 if 行采用了 pwd 命令。如果命令成功結束, echo 語句就會顯示該文本字符串。在
命令行運行該腳本時,會得到如下結果
shell執行了 if 行中的 pwd 命令。由於退出狀態碼是 0 ,它就又執行了 then 部分的 echo 語句。
下麵是另外一個例子
if IamNotaCommand
then
echo "It worked"
fi
echo "We are outside the if statement"
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
最後更新:2017-10-07 12:33:00