bash ignores shebang when running file passed as argument - bash

How can I have bash respect the shebang when passing a file as an argument?
eg:
hello.py
#!/usr/bin/env python
print("Hello Python")
When passed to bash:
$ bash hello.py
hello.py: line 2: syntax error near unexpected token `"Hello Python"'
hello.py: line 2: `print("Hello Python")'
In the environment I’m using I unfortunately can’t execute hello.py directly, it has to be done by supplying args to bash.

Don't run scripts with an explicit interpreter. Always execute them by just typing the script name; that way the interpreter listed in the shebang will be used.
$ ./hello.py
This will require that the script is executable so make sure to do that if you haven't already.
$ chmod +x hello.py
If you have to run bash then use -c to pass a full command and stop it from trying to read it as a bash script:
$ bash -c './hello.py'

Related

Move linux command into shell script [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Pass command text to `bash` from `sh` without a script file?

I am trying to run a single command using bash in a sh script. There is no way to use bash for the script, I have to use sh. However, I need to run a bash-only command in sh.
Basically, I want something like the following:
bash --command_in "echo foobar"
Is this possible? I don't want to make a second script file just to run that one command in bash (like bash my_script.bash).
Derp, it's the -c flag. This wasn't easy to Google, and the --help is prety brief.

Bash script works manually but not as cron job [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

"source" command in shell script not working [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 5 years ago.
I have a file to be sourced in Centos 7.
It just works fine if I do :
$ source set_puregev_env
however, if I put this in a shell script, it doesn't work..
$ sh xRUN
xRUN: line 3: source: set_puregev_env: file not found
this is my shell script : xRUN
#!/bin/bash
source set_puregev_env
can anyone tell me what I might be doing wrong, or missing?
source is a command implemented in bash, but not in sh.
There are multiple ways to fix your script. Choose either one.
Run the script using bash interpreter
When you are invoking the xRUN script - you are explicitly telling it to be interpreted by sh
$ sh xRUN
To change and interpret the script with bash instead do
$ bash xRUN
This will make bash interpret the source command, and your script will work.
Use dot command to make script bourne compatible
You can also change the source with a dot command which does the same thing but is supported in both bourne and bash.
Change the line:
source set_puregev_env
With:
. set_puregev_env
Now the script will work with either sh or bash.
Make script executable
You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this:
$ ./xRUN
It will then use the command specified in the shebang and use the rest of the script as input. In your case it will use bash - since that is specified in the shebang.

Start Perl script via "Windows Command" through Cygwin

i have the following issue:
I try to start a perl script from the windows scheduler through cygwin
Steps i do:
Call process.bat file
In the process.bat i call bash with the parameter for the perl script
Symptoms:
If i call "perl scriptpath" directly from cygwin it works like a charm
If i call the script from the windows cmd with bash it will not work.
Command: C:\cygwin\bin\bash.exe --login /cygdrive/c/scripts/testscript.pl
It prints the following:
Line 3: use: command not found
Line 4: use: command not found
Can't find file Test
Script:
#!/usr/bin/perl
use strict;
use warnings;
print "Test";
Probably i'm making only a small mistake and cannot see it. It seems to interpret it with the windows cmd instead of the perl.
The parameter passed to bash will be treated as a Bash script, not a Perl script. There is no reason to use Bash in this case - just invoke Perl directly:
C:\cygwin\bin\perl.exe /cygdrive/c/scripts/testscript.pl
If you really want to do it your way - calling a cmd script which calls a Bash script which calls a Perl script - then you would need to write a Bash script to invoke your Perl script:
#!/bin/sh
/cygdrive/c/scripts/testscript.pl
And pass that Bash script as the parameter when you invoke bash.
You might like to use option -c to have bash execute a command, like this:
C:\cygwin\bin\bash.exe --login -c /cygdrive/c/scripts/testscript.pl
From the bash's man-page:
-c string
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

Resources