bash 使用“读取变量”的“错误变量名称”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33320452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"bad variable name" using "read var"
提问by VapoRizer
I am getting confused about Linux shells. It may be that I oversee something obvious as a Linux noob.
我对 Linux shell 感到困惑。作为 Linux 菜鸟,我可能会监督一些显而易见的事情。
All I want is the following script to run:
我想要的是运行以下脚本:
#!/bin/bash
echo "Type some Text:"
read var
echo "You entered: "$var
Here is the situation:
这是情况:
- Installed Ubuntu Server 14.04 in a VirtualBox on windows
- Installed it with this packages
- A SAMBA mounted on /media/buff
- The script is on /media/buff/ShellScript/test.sh
- made executable by "sudo chmod a+x /media/buff/ShellScript/test.sh"
- The rest is default
- I am using PSPad on windows to edit the script file
- 在 Windows 上的 VirtualBox 中安装 Ubuntu Server 14.04
- 用这个包安装它
- 安装在 /media/buff 上的 SAMBA
- 该脚本位于 /media/buff/ShellScript/test.sh
- 由“sudo chmod a+x /media/buff/ShellScript/test.sh”执行
- 其余为默认
- 我在 Windows 上使用 PSPad 来编辑脚本文件
I readabout the dash but I'm not getting it. Here are some variations:
我读过有关破折号的信息,但我没有明白。以下是一些变化:
Using sh to launch
使用 sh 启动
user@Ubuntu:/media/buff/ShellScript$ sh test.sh
Type some Text:
:bad variable nameread var
You entered:
Using bash to launch:
使用 bash 启动:
user@Ubuntu:/media/buff/ShellScript$ bash test.sh
Type some Text:
': Ist kein gültiger Bezeichner.var (means no valid identifyier)
You entered:
Changed the Shebang in the script to "#!/bin/sh", Using sh to launch
将脚本中的shebang改成“#!/bin/sh”,使用sh启动
user@Ubuntu:/media/buff/ShellScript$ sh test.sh
Type some Text:
:bad variable nameread var
You entered:
I searched across the Internet for hours now and I assume, that the script itself is ok, but there are missing some environment settings. I used "sudo dpkg-reconfigure dash" to set dash as default shell (which I think is ubuntu default anyway)
我现在在互联网上搜索了几个小时,我认为脚本本身没问题,但是缺少一些环境设置。我使用“sudo dpkg-reconfigure dash”将dash设置为默认shell(我认为无论如何都是ubuntu默认的)
sadface panda :)
悲伤的Pandas:)
回答by Andras Deak
There are most probably carriage returns (CR, '\r'
) at the end of your lines in the shell script, so the variable is trying to be read into var\r
instead of var
. This is why your error message is meaningless. A similar error should look like this:
'\r'
在 shell 脚本的行尾很可能有回车符 (CR, ),因此变量正试图被读入var\r
而不是var
. 这就是为什么您的错误消息毫无意义。类似的错误应如下所示:
run.sh: 3: export: [... the variable name]: bad variable name
In your case bash throws an error because var\r
is illegal for a variable name due to the carriage return, so it prints
在您的情况下,bash 抛出错误,因为var\r
由于回车,变量名是非法的,因此它会打印
test.sh: 3: read: var\r: bad variable name
but the \r
jumps to the beginning of the line, overwriting the start of the error message.
但\r
跳转到行的开头,覆盖错误消息的开头。
Remove the carriage returns fom the ends of lines, possibly by using the dos2unix
utility.
删除行尾的回车,可能是使用该dos2unix
实用程序。
回答by Chandrani H
Here are a list of editorsthat support unix newline character.
以下是支持 unix 换行符的编辑器列表。
Brackets has an extensionfor new-line/end-of-line support and it is built-in in Notepad++. Go to the 'Edit' tab. Find 'EOL Conversions' and select Unix (LF). That should get it done.
Brackets 有一个对换行/换行支持的扩展,它内置在 Notepad++ 中。转到“编辑”选项卡。找到“EOL 转换”并选择 Unix (LF)。那应该可以搞定。