gpt4 book ai didi

linux - bash sh 差异,脚本转换

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:58:25 25 4
gpt4 key购买 nike

我有这个用 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、./reboot.sh: 16: ./reboot.sh: [[: not found 等错误

最佳答案

在 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 ]

关于linux - bash sh 差异,脚本转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24607696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com