Results 1 to 5 of 5
this may be a simple task for most of the linux users, but as a newbie i find this difficult to do. I were participating in a competition which mandated ...
- 09-04-2011 #1Just Joined!
- Join Date
- Sep 2011
- Posts
- 1
Bash help
this may be a simple task for most of the linux users, but as a newbie i find this difficult to do. I were participating in a competition which mandated me to change the interpreter by executing the command '#!/usr/bin/bash' via shell script. But i found out like the bash file was in '/bin' folder instead of '/usr/bin' folder. So i changed it to appropriate path and executed the script. Unfortunately, it gives the 'bad interpreter' message. Whats strange is that if i execute it via terminal (instead of putting it in a script and running it ), it executes perfectly fine without any errors. What am i doing wrong here?
- 09-04-2011 #2Just Joined!
- Join Date
- Dec 2009
- Location
- California
- Posts
- 68
You probably created the script on windows and then uploaded it to Linux and you've got control-Ms at the end of every line.
Try this:
$ dos2unix foobar.sh
where foobar.sh is the name of your script.
Then it should execute.
- 09-07-2011 #3
Could you please reproduce the exact formulation of the task?
The '#!'-expression ('shebang') can only be located at the beginning of a script file and is used to set the interpreter which will run the corresponding script file. Again, your statement #!/usr/bin/bash should be exactly the first line of your script; no comments or blank lines are allowed before it.
On the other hand, if you put this line into interactive instance of bash, this line will be interpreted as comment and will therefore be discarded.
Does this clear the matters a bit?
abarclay, why do you think the problem has anything to do with newlines?
- 09-07-2011 #4Just Joined!
- Join Date
- Dec 2009
- Location
- California
- Posts
- 68
Unlimited, it's not the new-lines that are the problem, it's the carriage returns.... Unix text files have only newlines (\n) while dos/windows text files have carriage return and line feed (\r\n). The way I came to the conclusion is elementary my dear Watson...
1) writer is self-proclaimed newbie
2) newbie's don't like vi or emacs or even the UI because it's different, so they often write the script in notepad and upload via ftp
3) bad interpreter is a common message when you have something invalid as the first line of your script.
The proof is in the pudding, however.
I just created two scripts which look identical when displayed with cat:
$ ls -l /tmp/t.sh
-rwxr-xr-x. 1 abarclay abarclay 28 Sep 7 16:04 /tmp/t.sh
$ cat /tmp/t.sh
#!/bin/sh
echo hello world!
$ ls -l /tmp/q.sh
-rwxr-xr-x. 1 abarclay abarclay 30 Sep 7 15:57 /tmp/q.sh
$ cat /tmp/q.sh
#!/bin/sh
echo hello world!
Notice both those scripts are executable....
When I execute the first one, I get:
$ /tmp/t.sh
hello world!
When I execute the second one, I get:
$ /tmp/q.sh
sh: /tmp/q.sh: /bin/sh^M: bad interpreter: No such file or directory
If I use "od" to look at the scripts, you will see the difference:
$ od -c /tmp/t.sh
0000000 # ! / b i n / s h \n e c h o h
0000020 e l l o w o r l d ! \n
0000034
$ od -c /tmp/q.sh
0000000 # ! / b i n / s h \r \n e c h o
0000020 h e l l o w o r l d ! \r \n
0000036
If I strip the carriage returns, the script executes fine:
$ dos2unix /tmp/q.sh
dos2unix: converting file /tmp/q.sh to UNIX format ...
$ /tmp/q.sh
hello world!
- 09-09-2011 #5


Reply With Quote
