
我想做一個 if then 語句:如果變數 i 等於 0 到 9 的正規表示式並且如果 grep 沒有傳回任何結果,則執行某些操作。這是正確的語法嗎?
if [[ $i =~ [0-9] ]] && if ! grep $i /opt/tftpboot/*; then
或者是
if [[ $i =~ [0-9] ]] && ! [[ grep $i /opt/tftpboot/* ]]; then
答案1
它是:
if [[ $i =~ [0-9] ]] && ! grep $i /opt/tftpboot/*; then
您可能不需要 的輸出grep
,在這種情況下您可以執行以下操作:
if [[ $i =~ [0-9] ]] && ! grep -q $i /opt/tftpboot/*; then