bash sh差异,脚本转换

时间:2014-07-07 09:50:39

标签: linux bash ubuntu sh

我有这个用bash编写的脚本

#!/bin/sh

# script makes grep directly on dmesg output
# looking  for  'USB disconnect'  phrase  if
# it  finds  it  then  machine  is  rebooted
# ver 1.2
clear

UNPLUG_MESSAGE="PLEASE UNPLUG THE USB STICK NOW"
echo $UNPLUG_MESSAGE && sleep 2

while true; do

USB_STATUS=`dmesg | tail -n 5 |  grep 'disconnect'`

  if [[ $USB_STATUS == *USB?disconnect* ]]
     then
        clear && echo "Rebooting... bye" 
    sleep 1 && sudo reboot

  elif [[ $USB_STATUS != *USB?disconnect* ]]
     then
        clear && echo "Please remove USB drive..."
        sleep 2
  fi
done

exit 0

将#!/ bin / bash更改为#!/ bin / sh后,脚本不再起作用了 - 我应该在此脚本中更改什么内容?

我得到的错误就像未知的操作数 USB?disconnect ,. / renboot.sh:16:./ renboot.sh:[[:not found,etc。

1 个答案:

答案 0 :(得分:4)

在ubuntu上shdash(您可以ls -l $(which sh)运行以查看此内容。)

dash中,没有[[双括号)运算符。您必须仅使用test内置函数或[单括号)。

您可以替换:

if [[ $USB_STATUS == *USB?disconnect* ]]
...
elif [[ $USB_STATUS != *USB?disconnect* ]]

人:

if [ $(echo $USB_STATUS | grep -c "USB disconnect") != 0 ]
...
elif [ $(echo $USB_STATUS | grep -c "USB disconnect") = 0 ]