![](/img/trans.png)
[英]Detect a commented line in a linux bash script from another linux script using grep
[英]Detect OS from Bash script and notify user
使用bash,我想找到操作系統並通知用戶。 我試過了:
OS='uname -s'
echo "$OS"
if [ "$OS" == 'Linux' ]; then
echo "Linux"
else
echo "Not Linux"
fi
我得到了
uname -s
Not Linux
在終端上,這是錯誤的。 如何正確設置字符串到uname返回的內容?
謝謝
而不是單引號,你可能意味着使用反引號:
OS=`uname -s`
但你真的想要
OS=$(uname -s)
此外,您可以考慮使用案例,而不是最終將成為if / else系列的if語句:
case $( uname -s ) in
Linux) echo Linux;;
*) echo other;;
esac
這將按要求返回操作系統 - 請注意, uname
不一定適用於所有操作系統,因此它不是此答案的一部分。
case "$OSTYPE" in
linux*) echo "linux" ;;
darwin*) echo "mac" ;;
msys*) echo "windows" ;;
solaris*) echo "solaris" ;;
bsd*) echo "bsd" ;;
*) echo "unknown" ;;
esac
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.