LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bin/bash: bad interpreter (https://www.linuxquestions.org/questions/linux-newbie-8/bin-bash-bad-interpreter-4175573551/)

iFunction 02-29-2016 08:52 AM

bin/bash: bad interpreter
 
Hi there,

can anyone tell me why I am getting this error from this script, I wrote it on this linux computer, so it is not a Windows/Mac to linux problem:

Code:

#!/bin/bash

myfilename=$0
echo $myfilename

I have changed the permissions using chmod for all to be able to execute, and I am calling it with:
Code:

pi@mybox: ~/bash_scripts $ ./testfile
In case it is needed:
Code:

which bash
returns:
Code:

/bin/bash
any help on this would be much appreciated

pan64 02-29-2016 09:09 AM

I think you mistyped something. Please post the content of the file, not only lines-letters. Please post the full response, not only parts.
Put set -xv at the beginning of the script to see what's happening.

iFunction 02-29-2016 09:37 AM

I don't understand, this is my complete script. What is the content of the file?

in emacs, I typed (without the codeblocks)
#!/bin/bash

myfilename=$0
echo $myfilename


Then I did [ctrl]+x [ctrl]+s to save the file.
What is the contents of the file that you are requesting? I am trying to follow a tutorial about arguments and trying to learn about $0, $1 etc. Mu assumption is that $0 refers to the filename that was used to call the script which in this case was "testfile.sh", well actually, it was "tes" and then the tab key to autocomplete to make sure I have the correct file name.

hydrurga 02-29-2016 09:44 AM

I'll leave pan64 to clarify what he is asking for, but can you please cut and paste (not retype) the actual error message that you are receiving (the whole line and any lines around it that may refer to the error).

grail 02-29-2016 09:46 AM

I cannot see anything in particular, but do note that your last post refers to "testfile.sh", but in your running example it is "./testfile", ie. without the .sh

iFunction 02-29-2016 09:49 AM

Ok, I can't figure this one out, so I wrote another script to see if that worked and it works fine, it's just a shame that there was no way of troubleshooting this problem so I can learn from it.

hydrurga 02-29-2016 09:53 AM

Quote:

Originally Posted by iFunction (Post 5507986)
Ok, I can't figure this one out, so I wrote another script to see if that worked and it works fine, it's just a shame that there was no way of troubleshooting this problem so I can learn from it.

There is always a way to troubleshoot problems.

You were asked to provide specific info to allow us to help you to do that. If you still want to determine what caused the problem, let's continue with that. On the other hand, if you want to just put it down to "c'est la vie" then that's fine.

iFunction 02-29-2016 11:39 AM

Unfortunately, I deleted the file, I had posted all there was, it was literally just those three lines, and the error message was in the terminal was just that one line:
Code:

bash: ./testfile.sh: bad interpreter: No such file or directory
So when more was asked for, I didn't have anything else to post up.

Ultimately, this is a Raspberry Pi I am currently working with, and although I have emacs running on here, there are some quirks in it can't just be opened up from the start menu or an icon and used, as it only opens in read only. So this has to be opened from Terminal prefixed with Sudo. There are other quirks also, for some reason it only reads the .emacs file if run from the start menu, but as mentioned earlier, it only opens as read only mode. I am suspecting that it is on of these quirks that caused the problem in the first place.

TBPH, there are too many unknowns to follow this one through now at this point, but my initial reaction is that it was not my script causing the problem, but was something that had been sent to the shell from emacs that did not agree with it, hence the "Bad Interpreter"

Kind regards

iFunc

hydrurga 02-29-2016 11:55 AM

Quote:

Originally Posted by iFunction (Post 5508018)
Code:

bash: ./testfile.sh: bad interpreter: No such file or directory

That's unusual, because this error message is normally of the form:

bash: <bash script path>: <interpreter declaration>: bad interpreter: No such file or directory

which would indicate that your script was being interpreted as having no characters at all after the initial #! since there was nothing in the error message in the <interpreter declaration> field.

For info, that was why a cut and paste of both the original script and the output was useful - to see exactly how the first line in the script was formed, and likewise for the error.

It is most likely that one or more non-printing characters had wheedled their way in there, the sort of thing that can happen with cut and paste from web sites or other systems, or perhaps a slip of the fingers in a text editor.

Habitual 02-29-2016 04:20 PM

Run in debug-mode (regardless of attributes) in Terminal >
Code:

bash -vx ./testfile.sh
Observe and Report. :)

iFunction 02-29-2016 04:49 PM

Yes, I think you are quite correct, sorry for not following this one through, I will pay a little more attention next time. As I said, I have got it working now, by just writing it again, it's a missed opportunity I'm afraid.

Thanks very much for your patience.

BW-userx 02-29-2016 05:23 PM

I got it to work, without error

code:

Code:

#!/bin/bash

myfilename=$0
echo $myfilename

saved as testfile
Code:

chmod +x testfile
output from executing in the directory the script is off of the command line:
Code:

# ./testfile
./testfile
#

you do not actually need the .sh at the end of a script. that has nothing to do with getting it to work.
All that is is a naming convention to allow anyone that sees that file in a directory to just know it is a script just by looking at it. nbd

it's contents and permissions and how it is executed are all that matters to get it to work.

and yes I'm in root. Because I can roll like that :D

BW-userx 02-29-2016 05:36 PM

Quote:

Originally Posted by iFunction (Post 5508150)
Yes, I think you are quite correct, sorry for not following this one through, I will pay a little more attention next time. As I said, I have got it working now, by just writing it again, it's a missed opportunity I'm afraid.

Thanks very much for your patience.

Code:

pi@mybox: ~/bash_scripts $ ./testfile
if you look closely you'll see what I do. you have a space between the $ (and) ./testfile
that was your error
cheers :D


All times are GMT -5. The time now is 09:04 AM.