Cannot run a shell script created on Windows [duplicate] - linux

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.

Related

How to run bash file with one word [duplicate]

This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
Issue
I want to run a bash file more easily, I've seen some applications where you only need to type word to execute the script.
Instead of typing ~/folder/file.sh in the terminal,
I only have to type a_word to run the file.
Is this possible with bash?
And also, this is on RPiOS's terminal, not sure if it differs.
Save your file to a location named in PATH. /usr/local/bin/a_word (no .sh) is a great example of such a location. Make sure it has executable permissions and starts with a shebang (like #!/usr/bin/env bash).
When you want to install something just for your own account, it's common practice to create a ~/bin directory and add it to your PATH (as by adding something like PATH=$PATH:$HOME/bin in ~/.bash_profile).
You have to define a so called alias.
Edit the file $HOME/.bashrc and add alias a_word = '$HOME/folder/file.sh', then logout and logon again.

/bin/bash^M: bad interpreter: No such file or directory

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.

how to convert a text/plain to text/x.shellscript

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

Linux bash script doesn't run correctly, but when run at the command line everything is fine

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.

executing windows command line operation from python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call external command in Python
I want to execute a windows command line operation using python. To execute the command I have to go to a particular directory within my system and then execute the command.
for example
1) Go to a particular directory c:\some\directory
2) then use command somecommand -x -y
I saw some posts on this topic but I was not able to figure them out properly.
Thanks
I assume you want to change the working directory then execute a command. So:
os.chdir(DIRECTORY);
os.system(COMMAND);
os.chdir - Set the current working directory.
os.system - Execute a "system" command.
If setting the working directory is not required you could just specify the full path to os.system.
Also, you might want to check out subprocess as it might be more what you're looking for.

Resources