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.
Related
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.
I simply want to source KSH with:
. ./.kshrc
within an expect script.
Is it possible to source KSH and run a KSH command within an expect script?
Sure, just launch a ksh interpreter to run it in:
exec ksh -c {. ./.kshrc; some_command}
I can't get the following to work:
c:\cygwin64\bin\bash -c /cygdrive/c/myscript.sh myargument
Specifically, myscript.sh is NOT getting "myargument" passed in as $1.
The script works exactly as I want it to if I hard-code "myargument" inside the script.
It's been 5 years since I have done any shell scripting and rust has accumulated!
The option -c is not used with scripts
Try this instead:
c:\cygwin64\bin\bash /cygdrive/c/myscript.sh myargument
Here I created a shell script to run another script in terminal and also I need to keep a log file, I knew 'script' command is used to track all user inputs and output. Then how can I use 'script' command in below lines.
#!/bin/bash
gnome-terminal --full-screen -x ./user_script_file.sh
I need to use script user_screen.log in above code. Please help me to find a solution,
gnome-terminal --full-screen -x script mylogfile.txt -c ./user_script_file.sh
-x can take many arguments so there is no need to quote.
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.