Unix\AIX Script Error: "0403-015 Cannot access a required executable file. It is in use" & "Cannot Open or Remove a file Containing a Running Program" - linux

Most of the times you create/place a script on/from Windows or WinSCP to a Unix machine without using vi command you will most likely encounter the errors below.
From what I saw it always happens because of special characters that look like simple space but are preformatted from Outlook or other tools.
Errors Encountered:
ksh:
0403-015 Cannot access a required executable file. It is in use.
bash:
/bin/bash^M: bad interpreter: Cannot Open or Remove a file Containing a Running Program

Place your script on a Linux machine and run the following command:
sed -i -e 's/\r$//' Your_Script.sh
Command Explained:
sed
stream editor for filtering and transforming text
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-e script, --expression=script
add the script to the commands to be executed

Upload the script in WinSCP using the "Text" mode.
See https://winscp.net/eng/docs/transfer_mode
You better enable the "Automatic" mode, instead of explicit "Text", so that you won't laster inadvertently upload binary files like images using the text mode.

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.

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.

Run Perl script in Linux environment

I'm trying to run code on Linux environment
Here's the code (saved as hello.pl):
#!/usr/bin/perl
use strict;
use warnings;
print "Hello You\n";
Here's what I tried on my linux environment:
%perl hello.pl
I tried listing out the path starting from C:\Users\... and so on
I keep getting error that says:
Can't open perl script "hello.pl": No such file or directory
You have to be located in the same folder with the hello.pl in the "window" (aka terminal, or console) that you try to execute perl hello.pl.
On linux, you can determine the folder that you're in by issuing pwd.
If you're not in the same folder (the most probable cause of your error), you have 2 options:
Navigate to that folder with cd /path/to/your/script/location you have to replace the /path/to/your/script/location in the example, with your actual path
Execute the file with perl /path/to/hello.pl - of course, you have to replace the /path/to/ in the example, with your ac
Also, you can try and view the file from the console running a less hello.pl
In cygwin you might try: /cygdrive/c/Users/bonan/Desktop/perl/hello.pl.
Alternatively at your prompt try tying in just perl without hitting enter, and then drag the hello.pl file from its file explorer location into the terminal window. That should paste the full file path to the file as text into the command prompt. If you're using cygwin I forget it if properly pastes the path with forward-slashes, like /cygdrive/c/Users/bonan/Desktop/perl/hello.pl, or if it pastes what it would in cmd with backslashes as you've indicated you typed yourself.
The other thing to do that's relatively easy is right click the file and choose to open a terminal or shell here, which for cygwin you can get in your context menu by running chere -i once (it actually says "Bash prompt here" I think). And there's similar context menu options for cmd, powershell, an actual linux bash, or mac os x's terminal ... once you're in the same path as the file, you can just type perl heltab and autocomplete the filename assuming no other files in the same folder start with "hel".

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

How to get a filename in a bash script without the keyboard

I want to write a script for linux, that will first copy a movie/series file to cache with something like:
cat /filepath/filename > /dev/null
and than open the same file in vlc.
The problem is getting the file name and path in to the script. I would like to simply double click a file, or somehow make this a faster process than typing this manually (especially because the file names of some series are just inconsistent and hard to type, even with auto-complete).
This is useful for watching movies or series on a laptop/netbook, since it allows the disk to spin down.
You should be able to create your own 'program' in a bash script which takes its first argument to be the filename using the convention "$1".
The bash script should look something like the below. I tested it, storing the script in the file cachedvlc.sh. The inverted commas helping to handle whitespace and weird characters...
#!/bin/bash
cat "$1" > /dev/null
vlc "$1"
...and will need to be made executable by changing its permissions through the file manager or running this in the terminal...
chmod u+x cachedvlc.sh
Then within your operating system, associate your bash script with the type of file you want to launch. For example on Ubuntu, you could add your script and call it 'Cached VLC' to the Menu using the 'Main Menu' application, then right-click on the file in Nautilus and choose 'Open with' to select your bash script.
After this, double-clicking or right-clicking on a file within your filemanager should be good enough to launch a cached view. This assumes what you say about caching is in fact correct, which I can't easily check.

Resources