开发者

Bash Shell; read command; using Cygwin on Windows 7

开发者 https://www.devze.com 2022-12-26 09:31 出处:网络
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.

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.

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.

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.


You should do an od -xcb on that script. My guesstimate is that it's almost certainly going to have the wrong line ending character in it.

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-return rather than the more normal 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 Windows editor which uses CR/LF where Cygwin expects just LF.

I suspect you'll find that doing an od -xcb on your actual script shows that you have those Windows line endings on one or more of your script lines.


In fact, I just tested this under Ubuntu by putting a CTRL-M at the end of just the read line and the output was (slightly modified to show the 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, very similar to what you're seeing.


As an aside (now that I have access to my Cygwin environment), what you're seeing is the output:

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 identifierad is actually made up of identifier and 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.


you can just use read to prompt user. No need the echo

read -r -p "Enter your name and press: " var_name
0

精彩评论

暂无评论...
验证码 换一张
取 消