Piping a command directly to the shell executable - bash

I am trying to understand the difference between the following
Approach 1:
Launch a bash shell
On the bash shell type a command my_command myargs
Approach 2:
Launch a bash shell
Execute the following directly my_command myargs | /bin/bash/
My command passes in approach 1 but does not in approach 2. I was thinking both approaches were equivalent. Of course in approach 1 I have not done any commands/changed the path variables before doing my_command. In approach 2 I am just creating a brand new shell and piping my command into it.
Can someone explain the difference? Is it missing $PATH, environment variables etc? If so how can I echo/find out?

Your "approach 2" pipes the output of my_command to bash.
It's equivalent to
$(my_command myargs)
If you instead pipe the literal text, it will work:
echo my_command myargs | /bin/bash/

Related

Can I set variables based on command arguments in a Shell script? [duplicate]

This question already has answers here:
What is the best way to parse command line options in bash shell?
(1 answer)
Specify command line arguments like name=value pairs for shell script
(5 answers)
Closed 1 year ago.
My goal is to make a shell script that I can run that will generate a static HTML page and I would like to change some of the data in the file, like some text or a class name and I would like to get those variables from a command line argument, like this:
sh createfile.sh --title "Example" --author "Example"
Can you even handle command arguments with a shell script?
There are a lot of ways to parse command line arguments in a script. The most basic is just to reference them via $1, $2, etc. But for your use case, it's probably easier to not bother. Just do something like:
$ cat createfile.sh
#!/bin/sh
cat << EOF
${title?}
Written by ${author?}
EOF
$ author=doug title='my document' sh createfile.sh
my document
Written by doug
There certainly are reasons to avoid using the environment this way, but if you want to pass the value as parameters there's no reason to enforce using -- as an indicator. Just do:
#!/bin/sh
for x; do
if ! test "$x" = "${x#*=}"; then
eval "$x"
fi
done
cat << EOF
${title?}
Written by ${author?}
EOF
$ sh createfile.sh author=foo title=bar
bar
Written by foo
By avoiding the environment, you prevent accidentally grabbing unexpected values. Also, this will work if you are using one of the shells from the csh family. This is certainly not robust, and any use of eval is suspect, but these are 2 reasonably useful techniques.

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.

How do I write a shell script that takes a data file name as an argument to run my perl script?

I am learning Perl and Shell scripting and of the challenges I was given is to write a shell script that takes a csv file as an argument and then have that shell script run my perl script (test.pl). I can't seem to get the proper syntax down and I have terminate every time because it hangs up my terminal.
For example shell script is test.sh
#/bin/bash
./test.pl $i`
on my terminal I write type out
test.sh testfile.csv
Ultimately I want the test file to be read by my perl script so it can run.
I think your error comes from the $i` part.
First the trailing backquote is probably a typo and should raise a syntax error. Second, the i variable is not defined, so $i resolve to an empty string. As it is not quoted, shell will omit it and call test.pl without any arguments... Thus your terminal is probably hanging because your perl script is waiting for input.
As #fra suggested, you should use $1 instead of $i, hence passing the first argument passed to your bash script, to your perl script.
Depending on your perl script (shebang, execution write) you may or may not call the perl executable manually.

Pass command to cygwin shell script

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

Problem in running a script

i have unix shell script which is need to be run like below
test_sh XYZ=KLMN
the content of the script is
#!/bin/ksh
echo $XYZ
for using the value of XYZ i have do set -k before i run the script.
is there a way where i can do this without doint set -k before running the script. or is there something that i can do in the script where i can use value of the parameter given while running the script in the below way
test_sh XYZ=KLMN
i am using ksh.
Any help is appreciated.
How about running this?
XYZ=KLMN ./test_sh //running from directory where test_sh is
If your script needs no other arguments, a quick and dirty way do to it is to put
eval "$#"
at the start of your script. This will evaluate the command line arguments as shell commands. If those commands are to assign a shell/environment variable, then that's what it will do.
It's quick-and-dirty since anything could be put on the command line, causing problems from a syntax error to a bad security hole (if the script is trusted).
I'm not sure if "$#" means the same in ksh as it does in bash - using just $* (without quotes) would work too, but is even dirtier.
It looks like you are trying to use the environment variable "INSTANCE" in your script.
For that, the environment variable must be set in advance of executing your script. Using the "set" command sets exportable environment variables. Incidentally, my version of ksh dates from 1993 and the "-k" option was obsolete back then.
To set an environment variable so that it is exported into spawned shells, simply use the "export" command like so:
export INSTANCE='whatever you want to put here'
If you want to use a positional parameter for your script -- that is have the "KLMN" value accessed within your script, and assuming it is the first parameter, then you do the following in your script:
#!/bin/ksh
echo $1
You can also assign the positional parameter to a local variable for later use in your script like so:
#!/bin/ksh
param_one=$1
echo $param_one
You can call this with:
test_sh KLMN
Note that the spacing in the assignment is important -- do not use spaces.
I am tring this option
#!/bin/ksh
echo $1
awk '{FS="=";print $2}' $1
and on the command line
test_sh INSTANCE=LSN_MUM
but awk is failing.is there any problem over here?
Probably #!/bin/ksh -k will work (untested).

Resources