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

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

Related

"cannot execute binary file" when trying to run a shell script on linux

I am very new to linux and shell scriprting.
I am trying to run a shellscript from secure shell (ssh) on linux using following commands:
chmod +x path/to/mynewshell.sh
sh path/to/mynewshell.sh
I get this error:
path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.
Tried using this command:
bash path/to/mynewshell.sh
I get the same error.
Tried with this command: su - myusername sh path/to/mynewshell.sh
It is asking for my password and giving me this error: no such file or directory.
1.The result of cat -v path/to/mynewshell.sh is:
^#^#^#^#^#^#^#^#Rscript "$dir"/diver_script.R
done
2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:
#!/bin/bash/Rscript^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
for dir in /path/to/* ; do
^#^#^#^#^#^#^#^#Rscript "$dir"/myRscript.R
done
3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"
Please give any advice on how I can try executing the shellscript.
chmod -x removes execution permission from a file. Do this:
chmod +x path/to/mynewshell.sh
And run it with
/path/to/mynewshell.sh
As the error report says, you script is not actually a script, it's a binary file.
I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.
From a proposed duplicate:
run_me.sh.xz: run_me.sh.xz: cannot execute binary file
This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.
xz -d ./run_me.sh.xz
chmod +x ./run_me.sh # probably not necessary if you already did that before
./run_me.sh
Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.
To anyone else having the problem i had.
i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.
open the text file click on "Format" at the top, go down to "Make Plain Text" click it.
open your terminal type chmod 777 /path/to/file.sh
put in terminal: /path/to/file.sh to run it
That script is simply not a shell script.
A shell script is usually readable and contains shell code.
The output your cat command shows looks indeed like it's a binary of some sort.
As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.
You can check what it is identified as with the file command so:
file path/to/mynewshell.sh
Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?
Make sure the R scripts point to the right R script executioner.
In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

Execute bash and sh file on Mac without writing extension

I have a .sh file and I want to execute it from shell without writing the extension.
What I did:
I created a directory and added it to $PATH
I gave to the file.sh chmod 711
and the file contain #!/bin/sh (I tried also bash).
However when I try to execute myscript without sh I get command not found
while if I try with myscript.sh I get the right result.
How could I do?
I read also: How to run a shell script on a Unix console or Mac terminal? and executing shell script without calling sh implicitly but no solution
Result of ls -l
ls -l /Users/Mitro/scripts
total 8
-rwx--x--x 1 Mitro staff 22 Nov 26 10:25 myscript.sh
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/Mitro/scripts
Two problems...
Firstly, file is already an executable program in /usr/bin that tells you the type of a file - i.e. whether it is an image, or a song or a database. Quick example:
file a.png
a.png: PNG image data, 1 x 1, 1-bit colormap, non-interlaced
So, file is a bad name for a shell script - likewise is test.
Secondly, if you want to execute a script or program in your current directory, also known as dot (.), you either need to have dot in your PATH, or you need to explicitly tell your shell that the file you want to run is in the current directory. The easier option is the second, which means if your script is called fred, you run it with
./fred
which tells the shell it is in your current directory.
The longer option, if you want to always be able to run scripts in the current directory, is to add dot to your PATH. So, you locate your login script (probably $HOME/.profile) and you find the line that sets your PATH and you add the current directory to it.
export PATH=$PATH:.
Once you have set that, you are best off logging out and back in to have it take effect.
Some folks disapprove of the idea of adding dot to their PATH - I don't. YMMV.
You can add alias. If you have /some/path/to/script.py, do:
alias my_script='/some/path/to/my_script.py'
Now when you enter my_script, your script would be executed.
For mac the profile file is ~/.bash_profile as opposed to ~/.profile which did not work.

Linux shell to windows batch file

I have a Windows dedicated server and I installed Multicraft to run Minecraft servers, and I wanted to add a accept EULA button, since I did not know much about bat files, so I found a sh file online and I did not want to use Cygwin because I don't know how to install it and make it run properly.
I want to change this to a .bat file:
#!/bin/sh echo 'eula=true' > "$SERVER_DIR/eula.txt"
it basically finds the line eula=false in eula.txt and changes it to true from what i understand
I would guess that this is what you're looking for:
echo eula=true>>%SERVER_DIR%\eula.txt
Given the double greater-than symbol it should append the line to the indicated file. Note that we're using a backslash here. You might want to look at the contents of that SERVER_DIR environment variable to see if it will work on a Windows-based computer.
If you want to mirror the behavior in the UNIX script then use a single greater-than symbol to overwrite the file content.

Terminal cannot run shell script files stored in PATH directories

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.

sh script with scp variables

I am trying to writing a simple script which will figure out the latest version of a file by the filename and then download that file to the local computer.
What I cant figure out is why my code will work in the shell, but not work when I run it as a script. I am also running my script on cygwin, not sure if that will make a difference.
Here is the script
#!/bin/sh
x=$(ssh user#hostname 'ls -r -t /vgf/day1*.gif | tail -1')
echo $x
scp user#hostname:"${x}" /images/day1.gif
x is correctly assigned, but when I get to the scp command I receive something along the lines of
: No such file or directoryif
However if I run the scp command in the shell it will work
$ sh download.sh
/vgf/day1.gif
: No such file or directoryif
$ scp user#hostname:"${x}" /images/day1.gif
day1.gif 100% 22KB 22.1KB/s 00:00
I would be open to different solutions. If I could prevent the version increasing via some linux administration, I may follow that route, although I am still wondering what the problem is here.
By version, I mean day1_001.gif and the new version becoming day1_002.gif and so on. So when the file saves a file day1.gif, it will overwrite the original without creating another version.

Resources