1

我开始学习 Linux shell 脚本,当我写这个脚本时,我遇到了一个错误,

./my_script: 4: read: Illegal option -n
./my_script: 5: ./my_script: [[: not found

我发现它是因为 #!/bin/sh行,我仍然可以在没有该行的情况下运行脚本,但它不会执行/n之类的代码

#!/bin/sh
# Shell installer for gurb customizer. by Naveen Gamage.

OS=$(lsb_release -si)
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VER=$(lsb_release -sr)

grabninstall() {
    sudo add-apt-repository ppa:danielrichter2007/grub-customizer
    sudo apt-get update
    sudo apt-get install grub-customizer    
}

echo "Installer for GRUB CUSTOMIZER\n"
echo "GURB CUSTOMIZER"
echo "A tool for editing and configuring boot menu (GRUB2/BURG).\n"

read -p "Do you want to install Grub Customizer for $OS ${VER} [$ARCH] ? (Y/n) " -n 1

if [[ $REPLY =~ ^[Yy]$ ]]
then
    echo "The installer is downloading and installing GRUB Customizer!";
    echo "This action may require your password.\n";
    grabninstall
else
    echo "user quit"
    echo "Installation was unsuccessful."
fi

我在 Ubuntu 12.10 上这样做。

哪个 sh给出了这个输出

/bin/sh

知道我在哪里做错了吗?

4

1 回答 1

5

The problem is that you are using /bin/sh to run the script and on your system /bin/sh -> dash. This means that dash is executing your script. The dash shell does not support [[, but bash does. So you should change the first line in your script (called the Shebang) from #!/bin/sh to #!/bin/bash.

Alternatively, don't use [[ in your script. Only use features support by dash.

Also see this Ubuntu page on what constructs are not supported in dash.

于 2013-05-13T08:01:24.357 回答