redirect an awk string output to a script input with pipes

Hi,

I have a function in a bash script that returns a string after some operations using awk.

The following code returns 555

$VARIABLE="EXAMPLE"
get_number $VARIABLE

this value I'd like to pass it as a second argument of another script with the following usage

myscript.sh <param1> <param2>

I'm trying to pass the second argument using a pipe but it does not work, no idea why not

get_number $VARIABLE | myscript.sh param1
myscript.sh error: second argument is missing.

Can anyone help me?
many thanks,
rid

try this :

get_number $VARIABLE | xargs -I{} myscript.sh param1 {}

myscript.sh param1 "`get_number $VARIABLE`"

Thanks both.

This line worked

For info, the following one also works

myscript.sh <param1> $(get_number $VARIABLE)

Still don't understand why it does not work with the pipe :frowning:

You need to have a command in your script that can receive the pipe, like read.
I concocted this:

if ! [ -t 0 ]; then  # if stdin is not a terminal
  read params
  set -- $params
fi
echo $1
echo $2
 ./test  555 123
555
123
$ echo 555 123 | ./test
555
123

In this example the pipe overrides the command line parameters, but you can change that of course..

It seems you are conflating pipes and command line options. They are unrelated concepts.

A pipe redirects the standard output of one command into the standard input of another. To read what is being sent in via a pipe, a process has to read from standard input using file i/o.

Command line options are usually made available to a program/script through an array (argv for C executables) or special variables ($*, $@, $1, $2, etc) for shell scripts. getopt (a function in the C library and a command for shell script use) is used to process the command line options.

These are two different mechanisms for passing information. If you want to pass what is in param2 in via a pipe, myscript.sh would have to read it from its stdin. However, it's probably using the variable $2 as it expects it on the command line.

Hopefully that helps clear it up. If not, perhaps it's a start.

Take care,
alister

I got it, thanks for the explanation. I was kind of confused but now I see that pipes read directly from stdin.

rid

.....