【发布时间】:2013-09-07 15:19:06
【问题描述】:
echo "Select your option:"
echo "1. Change ip address"
echo "2. Add route"
echo "3. Reboot"
echo "4. Exit"
read A
case $A in
1)
echo "Add Ip address"
read IP
echo "Add Netmask"
read Netid
echo "Add name of interface"
read Interface
ifconfig ${Interface} ${IP}/${Netid}
if [ $? -ne 0 ];then
echo "Ip address not configured"
fi
;;
2)
echo "Add Destination"
read dst
echo "Add Netmask"
read Netid
echo "Add Gateway"
read gw
route add $dst mask $Netid gw $gw
if [ $? -ne 0 ];then
echo "Route not added"
fi
;;
3)
reboot
;;
4)
echo "Bye"
exit 0
;;
default)
echo "Wrong selection"
exit 1
esac
错误:
[b104@b104 Downloads]$ ./NetworkUtility.sh
./NetworkUtility.sh: line 1: $'\r': command not found
Select your option:
1. Change ip address
2. Add route
3. Reboot
4. Exit
1
': not a valid identifier 7: read: `A
./NetworkUtility.sh: line 8: $'\r': command not found
./NetworkUtility.sh: line 9: syntax error near unexpected token `newline'
'/NetworkUtility.sh: line 9: `case $A in
[b104@b104 Downloads]$
【问题讨论】:
-
您的脚本文件包含 DOS/Windows 样式的行尾 (
\r\n),这让您的 shell 感到困惑。尝试使用 unix 行尾 (\n) 保存它。 -
您可以执行
tr -d "\r" < NetworkUtility.sh > cleaned.sh,然后检查./cleaned.sh是否效果更好。 -
关于这个问题最不幸的是它有多长——当一个两行的脚本(有一个shebang和一个单行)时,绝对没有理由拥有超过20行的脚本空行)会产生完全相同的问题。
-
如果您在 Windows 上使用 IntelliJ 进行开发,但在 Linux 上执行脚本,由于默认的 IntelliJ 行尾配置,您可能会遇到此类问题。您可以通过配置 Unix 结尾来解决它,如下所述:jetbrains.com/help/idea/…
标签: shell