Script execute script without executing permission - linux

I have perl script which has no execute permission (only rw) when I try to run this script "./script.pl" I get "Permission denied" and it is clear for me. But when I run this script using "perl -e script.pl" it works and script is executed though its own permission.
Could somebody explain this strange behaviour?

When you run the command
perl script.pl
the perl executable is executed from the shell (with some of the exec() system calls). The file /usr/bin/perl will then need +x because every file that is executed with exec() needs that. Perl will run with the argument script.pl and perl is programmed to open a file that is given to it on the command line and process the content.
When you run the file in this way
./script.pl
the shell will run the file script.pl directly by using some of the exec() system calls and then script.pl is the file that will need +x.
In other words, if you want to run the file like ./script.pl, you need to
chmod +x script.pl
You will also need a "shebang" as first line of your script, telling which command you want to use to run it. It will look like this:
#!/usr/bin/perl

In the first case you are trying to execute the script.
In the second case (if we ignore the -e) you are trying to execute perl, and are telling it to run the code in the file script.pl.
(The -e causes it to try to run the literal text script.pl as Perl code)

Related

How to solve bash error "syntax error at line 3: 'CYBER_UNAME=$' unexpected"?

This error happens when I run a software containing bash script with beggining like this:
#! /bin/sh
CYBER_UNAME=$(uname)
CYBER_UNAME_M=$(uname -m)
I tried to execute these two commands in terminal and it works fine. This error only happens when I run the shell script. What should I do?
The result of 'uname' is SunOS. This shell script cannot be modified since it's protected on our server.
The line
#! /bin/sh
should read:
#!/bin/bash
So, that script will probably never really work.
If you cannot modify the script in situ, you might want to copy it to your local directory and correct it.
Otherwise,
tail +2 scriptname|/bin/bash
might work.

shell script throwing "No such file or directory" error while command line works perfect

I am trying to run a simple piece of shell script which return No such file or directory while executing the statement from command line works perfect.
Any leads ??
#!/bin/bash
cat /home/dev/.bashrc
I am executing the script like this:
bash ~/Desktop/test.sh
You have a \r at the end of /bin/bash so it's trying to run /bin/bash\r which does not exist. Use just \n to terminate lines. You can convert existing files with dos2unix, or a similar tool if you have it available:
dos2unix /home/dev/.bashrc
The \r in the shebang line isn't actually coming into play yet because you're invoking it as bash test.sh so it isn't using it, but it's the same problem

Can't run a script

I tried to create a script in linux, on a Synology server over SSH
so I wrote a file test.sh
#!/bin/bash
echo "this is a test"
I saved the file.
after that I did
chmod 755 test.sh
the I did
./test.sh
then i got this error
-ash "./test.sh" is not found
the file was created in
/root
I don't understand
Your shell (ash?) is trying to execute your script and is getting an ENOENT (no such file or directory) error code back. This can refer to the script itself, but in this case it refers to the interpreter named in the #! line.
That is, /bin/bash does not exist and that's why the script couldn't be started.
Workaround: Install bash or (if you don't need any bash specific features) change the first line to #!/bin/sh.
This is one of the quirks with hash bang programs. If the interpreter is not found (i.e. the program interpreting the script), you don't get a completely useful error like /bin/bash: no such file, but a completely useless and misleading test.sh: not found.
If this isn't in the Unix Hater's Handbook, it should be. :-)
You can either use #!/bin/sh or #!/path/to/bash or #!/usr/bin/env bash (which searches PATH for bash).

How to change Example.bat to Example.pl?

I have read other threads enter link description herethat discuss .bat to L/unix conversions, but none has been satisfactory. I have also tried a lot of hack type approach in writing my own scripts.
I have the following example.bat script that is representative of the kind of script I want to run on unix.
Code:
echo "Example.bat"
perl script1 param.in newParam.in
perl script2 newParam.in stuff.D2D stuff.D2C
program.exe stuff.D2C
perl script3 stuff.DIS results.out
My problem is I don't know how to handle the perl and program.exe in the unix bash shell. I have tried putting them in a system(), but that did not work. Can someone please help me?
Thank you!
Provided that you have an executable file named program.exe somewhere in your $PATH (which you well might — Unix executables don't have to end in .exe, but nothing says they can't), the code you've pasted is a valid shell script. If you save it in a file named, say, example.bat, you can run it by typing
sh example.bat
into the shell prompt.
Of course, Unix shell scripts are usually given the suffix .sh — or no suffix at all — rather than .bat. Also, if you want your script to be executable directly, by typing just
example.sh
rather than sh example.sh, you need to do three things:
Start the script with a "shebang" line: a line that begins with #! and the full path to the shell interpreter you want to use to run it (e.g. /bin/sh for the basic Bourne shell), like this:
#!/bin/sh
echo "This is a shell script."
# ... more commands here ...
Mark your script as executable using the chmod command, e.g.
chmod a+rx example.sh
Put your script somewhere along your $PATH. On Unix, the default path will not normally contain the current directory ., so you can't execute programs from the current directory just by typing their name. You can, however, run them by specifying an explicit path, e.g.
./example.sh # runs example.sh from the current directory
To find out what your $PATH is, just type echo $PATH into the shell.

Linux: Run a binary in a script

i want to run a program via script.
normally i type ./program in the shell and the program starts.
my script looks like this:
#!/bin/sh
cd /home/user/path_to_the_program/
sh program
it fails, i think the last line went wrong...
i know this is childish question but thx a lot!
If ./program works in the shell, why not use it in your script?
#!/bin/sh
cd /home/user/path_to_the_program/
./program
sh program launches sh to try and interpret program as a shell script. Most likely it's not a script but some other executable file, which is why it fails.
When you type
./program
The shell tries to execute the program according to how it determines the file needs to be executed. If it is a binary, it will attempt to execute the entry subroutine. If the shell detects it is a script, e.g through the use of
#!/bin/sh
or
#!/bin/awk
or more generally
#!/path/to/interpreter
the shell will pass the file (and any supplied arguments) as arguments to the supplied interpreter, which will then execute the script. If the interpreter given in the path does not exist, the shell will error, and if no interpreter line is found, the shell will assume the supplied script is to executed by itself.
A command
sh program
is equivalent to
./program
when the first line of program contains
#!/bin/sh
assuming that /bin/sh is the sh in your path (it could be /system/bin/sh, for example). Passing a binary to sh will cause sh to treat it as a shell script, which it is not, and binary is not interpretable shell (which is plain text). That is why you cannot use
sh program
in this context. It will also fail due to program being ruby, awk, sed, or anything else that is not a shell script.
You don't need the sh and looks like you don't have the path to the program in your $PATH.
Try this:
#!/bin/sh
cd /home/user/path_to_the_program/
./program
You don't need the "sh" here. Just put "program" on the last line by itself.
This should be enough:
/home/user/path_to_the_program/program
If that does not work, check the following:
executable bit
shebang line of the program (if it is a script)

Resources