I am facing
/bin/bash^M: bad interpreter: No such file or directory
issue and I have already got the solution for it from this stack flow answer
-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory
which works fine.
My question is every time when I restart my ubuntu machine I have to redo everything
That is I execute
dos2unix -k -o filename
every time I start my system.
Is there any way this can be just once?
Please note: I had to create a new question because I was not able to ask the question or comment in the existing question due to less reputation
The first line of your bash script should be the Shebang (#!/bin/bash).
I see the error says: /bin/bash
But is should be changed to: #!/bin/bash
Then run:
$ dos2unix my_script
This will change all the line terminators from \r\n (Windows) to \n (Linux), this will modify the original my_script file so it will persist even after a reboot.
This is a very common problem of running a bash script from a file saved with Microsoft OS machine (a virtual machine maybe?) such as Windows or DOS.
So you know the fix to your problem.
Now you should prevent your problem reoccurring every time you login. Identify how the file is generated/copied/damaged by another resource. Like .bash_profile script or crontab script or any other management deamon.
Related
I did found questions on
Bad interpreter: No such file or directory thing on SO.
My issue is also solved when I changed the script from
#!/usr/bin/bash
echo -e "\t\t\e[92mHello from the Test Script!\e[39m"
to:
#!/bin/bash
echo -e "\t\t\e[92mHello from the Test Script!\e[39m"
after
I did the first line change from looking an answer here.
Shell script: Bad interpreter.No such file or directory
I can not understand why removing the /usr from the first line helps.
P.S.I am learning about linux file permissions and I was unable to execute my file even after changing the permission using '755'.
So, please if anyone can explain me this.Thanks in advance.:)
On your system, the bash shell lives in /bin/bash and not /usr/bin/bash.
The path after the ! should be the path to an executable that will be passed the contents of the script as an argument.
You can read more about this at wikipedia
As for the second part of your question; it would not have mattered what the permissions are; as the file was pointing to a bad interpreter.
For more on unix file permissions, I suggest reading this entry on wikipedia.
That's because there is no bash binary at /usr/bin/bash and the correct path for bash is /bin/bash.
The #! line at the top of scripts, called the shebang, determines what program (sh, bash, ruby, perl, python, etc.) is used for running the script.
This post covers this topic well:
https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script
In my case adding sh before script name solved the issue.
You can also call your script by adding "./" at the beginning in case you call it from the local directory. The other solution is to call it by specifying its full path.
This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 7 years ago.
I seem to be having a weird problem, the solution for which might be dead simple and I am just being blind.
My development environment is Windows. I create a deployment archive file, within which contains a shell script file (called install.sh). I sftp this archive over to a linux environment, untar it and try to run execute the script (after chmod to make it executable) and I get this error:
syntax error: unexpected end of file
I don't notice any errors in the file. I delete this file, create a new install.sh, copy over the exact contents from my Windows env, chmod it again, run it again and this time it runs fine!
I have no idea why it does not run the first time I untar it. Any help appreciated!
Check your file:
cat --show-nonprinting file
Remove carriage returns from Windows/DOS:
tr -d "\r" < file > fixed_file
Linux and Windows uses different end of line (CL or CLRF), or maybe it's an encoding problem. You should check the differences between the running sh and the non-running one.
Getting "command not found" when trying to run shell script file. The file itself is stored in a directory, that is added to PATH, but still, terminal doesn't recognize it as shell script.
Here's the sequence I try:
tajimura/GAMIT% echo $PATH
/usr/local/bin:/usr/bin:/bin:/user/games:/usr/X11R6/bin:/usr/bin/X11:/usr/lib64/jvm/jre/bin:/home/tajimura/GAMIT/gamit/bin:/home/jaimura/GAMIT/kv/bin:/home/tajimura/GAMIT/com
tajirmura/GAMIT% ls /home/tajimura/GAMIT/com/sh_steup
/home/tajimura/GAMIT/com/sh_setup
tajimura/GAMIT% sh_setup
sh_setup: Command not found.
tajimura/GAMIT% sh sh_setup
sh: sh_setup: No such file or directory
tajimura/GAMIT% l /home/tajimura/GAMIT/com/sh_setup
-rwxr-xr-x 1 tajimura users 11109 Aug 20 2013 /home/tajimura/GAMIT/com/sh_setup
Here is a screenshot:
PS: Opensuse 12.1 here.
ADDED: I was executing it just fine during first 4 days, so I guess hashbang is not an issue. But I can't guarantee that workstation wasn't rebooted between my sessions, so maybe (just may be) -noexec is the cause. However, script sits in my home directory on hard disk, it's not a removable drive.
ADDED: The first five lines of sh_setup:
/home/tajimura% sed 5q /home/tajimura/GAMIT/com/sh_setup
#!/bin/csh -f
#
#doc Check and setup the GAMIT tables directory
#doc
#
Your script is either not executable (make it so with chmod +x sh_setup) or it specifies a broken (non-existing) interpreter on its hash-bang line (the first line of the sh_setup file, starting with #!).
Your sh sh_setup invocation fails because it doesn't use $PATH and you're in the wrong directory.
EDIT: Your script is clearly executable (I didn't spot this in your screenshot at first), which leaves us with a possibly incorrect interpreter.
I've found an unexpected solution of the problem. If I call the script under bash instead of csh everything is good.
I am sending a .sh file created from a windows machine to a linux so that I could run it there. The problem is that I keep on getting an error called bad interpreter.But when I program the shell script in the linux machine it runs with no problems even though it has the same code with the one sent from the windows machine. After my ivestigation, I found out that the windows machine .sh script is a text/plain file(using file -bi) and the other one from the linux machine is a text/x.shellscript. Is there a way to convert the text/plain to a text/x.shellscript? thank you
this is the script:
#!/bin/bash
date
sudo apt-get update
I tried a solution by doing another .sh file in a linux box containing only
#!/bin/bash
Then the windows machine only sent a file containing test commands like :
date
hostname
Then I append the file from the windows box to the linux one with
cat windows.sh >> linux.sh
It did not work if I run linux.sh. It says errors like:
./linuxh.sh: line 2 $'date\r':command not found
./linuxh.sh: line 2 $'hostname\r':command not found
However, if I open Linux.sh then save it again without doing anything. It works
I'm summarising below the steps you need to take so other users can see easily what needs doing:
Firstly, you need to check your script has the correct path to your interpreter after the "#!" in the very first line. This is should probably be:
#!/bin/bash
or
#!/usr/bin/bash
and you can find which is correct by typing:
which bash
on your Linux box.
Secondly, you need to make sure that any Windows carriage returns (or "^M") at the ends of the lines are removed before expecting your Linux box to run the script. You can do this with:
dos2unix yourscript
Just for reference, you can easily see weird characters such as TABs or linefeeds or carriage returns in Linux by using:
cat -vet yourfile
or
sed -n l yourfile
Thirdly, you need to make sure your script is executable on Linux, using chmod like this:
chmod +x yourscript
Finally, when you have done all that, you need to either add the directory where the script is located to your PATH variable (and export it) or give the full path to your script like this if your script is in the current directory:
./yourscript
or like this if it is located somewhere else
/some/directory/some/where/yourscript
Originally I thought this was a db administration issue, but I think the lines have become a little blurred. I'm going to keep this strictly a scripting problem here. If you want more background info I posted this on ServerFault here:
https://serverfault.com/questions/511780/postgresql-pgdump-script-on-rhel5-failed-fe-sendauth-no-password-supplieddat
My Script
I distilled my original script down to this to keep things simple. This is what I have in a .sh named pg_dump_script:
/home/myusername/lappstack-1.2-5/postgresql/bin/pg_dump --host=127.0.0.1 --port=5433 --username=myusername mydbname
When I run this I get the following error:
$ bash pg_dump_script.sh
Password:
" does not exist database "corenection to database "mydbname
I can copy and paste the line in the script file and run it from the command line and everything works fine.
Here are the permission on the script file:
-rwxr-xr-x
I used chmod 755
I'm not sure what I'm doing wrong here? I'm not an expert on scripting, but I thought if I could run it from the console it would of course run fine from a script. Let me know if any information is needed. I'm trying to keep this simple.
Updates
I removed all parameters. This is all the text in the .sh
/home/myusername/lappstack-1.2-5/postgresql/bin/pg_dump
I receive this error:
$ ./pg_dump_script.sh
: No such file or directoryine 1: /home/myusername/lappstack-1.2-5/postgresql/bin/pg_dump
: command not foundst.sh: line 2:
Run dos2unix on your .sh file to convert to proper UN*X line endings and it should work:
dos2unix pg_dump_script.sh
When editing with vi check to make sure it's not in dos edit mode. If you start with an original text file with CR/LF in it, vi will automatically go into dos mode unless you tell it otherwise.