shell - alpine linux sh 脚本不可运行

我制作了一个简单的脚本来更改 Alpine 虚拟机的主机名和 IP 我尝试安装 bash 并运行它,也尝试使用 sh 我不知道还能做什么或尝试。有人可以帮我吗?我会留下脚本和输出。

脚本

#!/bin/bash
 
#hostname change
read -p "New hostname " newhostname
echo "$newhostname" > /etc/hostname
sed -i s/alpine-blank/$newhostname/g /etc/hosts
hostname -F /etc/hostname
hostname
 
echo ________________________________________
 
#Change IP to staic
read -p "New static IP " nsip
sed -i s/#//g /etc/network/interfaces
sed -i s/ip/$nsip/g /etc/network/interfaces
sed -i s/hm/$newhostname/g /etc/network/interfaces
sed -i 's/iface eth0 inet dchp/#iface eth0 inet dchp/g' /etc/network/interfaces
service networking restart
ip a
 
echo ________________________________________
 
#New motd
echo "IP: $nsip
 
Hostname: $newhostname
 
Alpine Linux 3" > /etc/motd
 
echo ________________________________________
 
#updates
apk update
apk upgrade
 
echo ________________________________________
 
#Reboot Y/N
while true; do
    read -p "confirm reboot. [Y/N]: " yn
    case $yn in
        [Yy]* ) reboot; break;;
        [Nn]* ) exit;;
    esac
done

使用 bash 运行的脚本输出

alpine-blank:~# ls
setup.sh
alpine-blank:~# bash
alpine-blank:~# echo $0
bash
alpine-blank:~# ./setup.sh
bash: ./setup.sh: /bin/bash^M: bad interpreter: No such file or directory
alpine-blank:~#

使用 sh 和 shebang 定义 /bin/sh 的脚本

alpine-blank:~# ls
setup.sh
alpine-blank:~# sed -n 1p setup.sh
#!/bin/sh
alpine-blank:~# sh
alpine-blank:~# echo $0
sh
alpine-blank:~# ./setup.sh
sh: ./setup.sh: not found
alpine-blank:~# sh ./setup.sh
: not found line 2:
': bad variable nameme
: No such file or directory
: not found line 8: hostname
: not found line 9:
________________________________________
: not found line 11:
': bad variable name
: No such file or directorys
: No such file or directorys
: No such file or directorys
: No such file or directorys
'
BusyBox v1.34.1 (2022-04-04 10:19:27 UTC) multi-call binary.
 
Usage: ip [OPTIONS] address|route|link|tunnel|neigh|rule [ARGS]
 
OPTIONS := -f[amily] inet|inet6|link | -o[neline]
 
ip addr add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]
ip route list|flush|add|del|change|append|replace|test ROUTE
ip link set IFACE [up|down] [arp on|off] [multicast on|off]
        [promisc on|off] [mtu NUM] [name NAME] [qlen NUM] [address MAC]
        [master IFACE | nomaster]
ip tunnel add|change|del|show [NAME]
        [mode ipip|gre|sit] [remote ADDR] [local ADDR] [ttl TTL]
ip neigh show|flush [to PREFIX] [dev DEV] [nud STATE]
ip rule [list] | add|del SELECTOR ACTION
: not found line 20:
________________________________________
: not found line 22:
: not found line 29:
________________________________________
: not found line 31:
' is not an apk command. See 'apk --help'.
' is not an apk command. See 'apk --help'.
: not found line 35:
________________________________________
: not found line 37:
./setup.sh: line 41: syntax error: unexpected word (expecting "in")
alpine-blank:~#

回答1

从错误消息中可以看出,

/bin/bash^M: bad interpreter: No such file or directory

你在 bash 之后有一个 Control-M(即十六进制 0D)。该消息只是告诉您在目录 /bin 中没有同名的文件。解决方案是删除脚本的这个(以及行尾可能的其他 ^M)。这可以通过

dos2unix setup.sh

对于未来,我建议您将文本编辑器配置为不在行尾插入这些字符。