bash 外壳;读取命令;在 Windows 7 上使用 Cygwin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2604293/
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
Bash Shell; read command; using Cygwin on Windows 7
提问by CrazyGrunt
Okay so I am having this problem. I write up a script to be run in cygwin on Windows. I've tried a bunch of basic example scripts in case it was my scripts problem.
好的,所以我遇到了这个问题。我编写了一个要在 Windows 上的 cygwin 中运行的脚本。我已经尝试了一堆基本的示例脚本,以防万一是我的脚本问题。
So I tried this:
所以我试过这个:
#!/bin/bash
echo -e "Enter your name and press [ENTER]: \c"
read var_name
echo "Your name is: $var_name"
Then I will run it and I enter a name for var_name.
然后我将运行它并为var_name 输入一个名称。
I get this:
我明白了:
$ ./project1.sh
Enter your name and press [ENTER]: Jake
': not a valid identifierad: `var_name
Your name is:
So as far as I understand it I am having a problem with read. I am trying to work on a project for my class, but I can't seem to figure out why it won't read it. I followed the book with no triumph then resorted to these examples on the web that do not seem to work for me either. Does anyone have any idea if it is my setup or if I am missing something, thanks.
因此,据我所知,我在阅读时遇到了问题。我正在尝试为我的班级做一个项目,但我似乎无法弄清楚为什么它不会阅读它。我没有成功地跟随这本书,然后在网上求助于这些似乎对我也不起作用的例子。有谁知道这是我的设置还是我遗漏了什么,谢谢。
回答by paxdiablo
You should do an od -xcbon that script. My guesstimate is that it's almost certainly going to have the wrong line ending character in it.
你应该od -xcb在那个脚本上做一个。我的猜测是它几乎肯定会有错误的行结束符。
The reason I suggest this is because the line:
我建议这样做的原因是因为该行:
': not a valid identifierad: `var_name
looks suspiciously like the two lines:
看起来很像这两行:
.........................ad: `var_name
': not a valid identifier
merged together (where the .characters indicate something that's been overwritten).
合并在一起(其中.字符表示已被覆盖的内容)。
That would be the case if your variable in that line was var_namecarriage-returnrather than the more normal var_name.
如果该行中的变量var_namecarriage-return不是更正常的var_name.
The fact that it's Cygwin also points to that conclusion since there's often trouble when you edit your scripts with a Windowseditor which uses CR/LF where Cygwin expects just LF.
它是 Cygwin 的事实也指向了这个结论,因为当您使用使用 CR/LF的Windows编辑器编辑脚本时经常会出现问题,而 Cygwin 只需要 LF。
I suspect you'll find that doing an od -xcbon your actual script shows that you have those Windows line endings on one or more of your script lines.
我怀疑你会发现od -xcb在你的实际脚本上做一个表明你在一个或多个脚本行上有那些 Windows 行结尾。
In fact, I just tested this under Ubuntu by putting a CTRL-Mat the end of just the readline and the output was (slightly modified to show the CTRL-M):
事实上,我只是在 Ubuntu 下测试了这一点,将 aCTRL-M放在行的末尾,read输出是(稍微修改以显示CTRL-M):
pax@pax-desktop:~$ od -xcb qq.sh ; ./qq.sh
0000000 2123 622f 6e69 622f 7361 0a68 6365 6f68
# ! / b i n / b a s h \n e c h o
043 041 057 142 151 156 057 142 141 163 150 012 145 143 150 157
0000020 2d20 2065 4522 746e 7265 7920 756f 2072
- e " E n t e r y o u r
040 055 145 040 042 105 156 164 145 162 040 171 157 165 162 040
0000040 616e 656d 6120 646e 7020 6572 7373 5b20
n a m e a n d p r e s s [
156 141 155 145 040 141 156 144 040 160 162 145 163 163 040 133
0000060 4e45 4554 5d52 203a 635c 2022 720a 6165
E N T E R ] : \ c " \n r e a
105 116 124 105 122 135 072 040 134 143 042 040 012 162 145 141
0000100 2064 6176 5f72 616e 656d 0a0d 6365 6f68
d v a r _ n a m e \r \n e c h o
^^
144 040 166 141 162 137 156 141 155 145 015 012 145 143 150 157
0000120 2220 6f59 7275 6e20 6d61 2065 7369 203a
" Y o u r n a m e i s :
040 042 131 157 165 162 040 156 141 155 145 040 151 163 072 040
0000140 7624 7261 6e5f 6d61 2265 0a0a
$ v a r _ n a m e " \n \n
044 166 141 162 137 156 141 155 145 042 012 012
0000154
Enter your name and press [ENTER]: Pax
': not a valid identifierar_name
Your name is:
In other words, verysimilar to what you're seeing.
换句话说,与您所看到的非常相似。
As an aside (now that I have access to my Cygwin environment), what you're seeing is the output:
顺便说一句(现在我可以访问我的 Cygwin 环境),您看到的是输出:
abcdefghij.sh: line 99 read: `var_name
`: not a valid identifier
where the second line overwrites the first, giving:
其中第二行覆盖第一行,给出:
`: not a valid identifierad: `var_name
In other words, the strange word identifieradis actually made up of identifierand the final ad:from read:. The reason it's only similar to my example above (as opposed to exact) is because your file name and line number will be different to my little test script.
换句话说,这个奇怪的词identifierad实际上是由identifier和最后的ad:from 组成的read:。它仅与我上面的示例相似(而不是精确)的原因是因为您的文件名和行号将与我的小测试脚本不同。
回答by ghostdog74
you can just use readto prompt user. No need the echo
您可以仅用于read提示用户。不需要echo
read -r -p "Enter your name and press: " var_name

