Shell script help

Joined
May 22, 2011
Messages
8
Reaction score
0
Points
1
Hi all,
I'm kinda new to making bash scripts and I need some help. This is what I have:

Code:
#!/bin/bash

echo "What is the name:"
read HI_FILE

echo "$HI_FILE"

exit

However, when I go into terminal and do
sh test.sh (thats the name of the file), it'll give me:

: command not found
What is the name:

Then I type something, press enter and it'll reply with:


': not a valid identifierI_FILE
: command not found

: command not found


What am I doing wrong??
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
The script itself is correct and should run just fine. You should be able to chmod it to allow execution and run it with, or without the sh actually (since the shebang specifies the shell). Since it's obviously executing the file, it's being found. What editor did you create the script in? What's the output of cat -vn test.sh in terminal?
 
OP
H
Joined
May 22, 2011
Messages
8
Reaction score
0
Points
1
reply

I created it in Xcode, but it displays fine in Dashcode too.

The output:


Code:
     1	#!/bin/bash^M
     2	^M
     3	echo "What is the file name (no extension):"^M
     4	read HI_FILE^M
     5	^M
     6	echo "$HI_FILE"^M
     7	^M
     8	exitMac-Pro:desktop Adminstrator$
 
OP
H
Joined
May 22, 2011
Messages
8
Reaction score
0
Points
1
reply

I removed the blank lines and saved it in textedit. Result:

Code:
blablabla: sh test.sh < I typed that
What is the file name (no extension):
ttt < I typed that
': not a valid identifierI_FILE
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Ahhhhh that's why. See the ^M's? That's what's doing it.

Try this in terminal

tr -d '\015\032' < test.sh


this is one of those times I wish OS X had dos2unix in the default build.
 
OP
H
Joined
May 22, 2011
Messages
8
Reaction score
0
Points
1
Responds with:

tr -d '\015\032' < test.sh
#!/bin/bash
echo "What is the file name (no extension):"
read HI_FILE
echo "$HI_FILE"Mac-Pro:desktop Administrator$
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Responds with:

tr -d '\015\032' < test.sh
#!/bin/bash
echo "What is the file name (no extension):"
read HI_FILE
echo "$HI_FILE"Mac-Pro:desktop Administrator$
Now try running it :)
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Ok, it's been a while since I've done this on a box that didn't have dos2unix on it. Bear with me :D

Try..

tr -d '\015\032' < test.sh >> test1.sh; chmod 755 test1.sh

then try running test1.sh


The problem is those ^M's are really (oops) carriage returns, and the command not found you're getting is referring to those. I'm still unclear why X Code would insert those.. I should really launch mine and take a look. Did you copy this from a windows machine by any chance?
 
OP
H
Joined
May 22, 2011
Messages
8
Reaction score
0
Points
1
Really strange..
Googled it and maybe it has to to something with PATH=$PATH? Don't know what it means but when I type it in terminal it shows me some long path

edit: To your post: didn't work :/
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Excellent! Welcome to the shell :D
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top