Cat a script, pipe it, then pass arguments to it?

suppose i have a perl script that is normally run this way:

./checkdisk.pl -H hostname -w 40 -c 80

but, for whatever reason, i cannot run the script directly as it should. But i can cat it through pipe. How can i pass the arguments "-H hostname -w 40 -c 80"?

so this is what i'm doing, which is clearly not going to work:

cat checkdisk.pl | perl

or

cat checkdisk.pl | perl -H hostname -w 40 -c 80

what im trying to do here doesn't just apply to perl scripts. i may have to do this for shell scripts, ruby scripts, python scripts.

i need to be able to pass the original arguments to the script if it is being piped.

any ideas?

perl checkdisk.pl -H hostname -w 40 -c 80
$ echo 'echo asdf $1 $2 $3'

asdf $1 $2 $3

# For bash scripts, sh -s arg1 arg2 arg3 ...
# Be sure to use the appropriate shell, bash for bash scripts,
# ksh for ksh scripts, etc.
$ echo 'echo asdf $1 $2 $3' | sh -s a b c

asdf a b c

# For perl scripts:
$ cat script.pl | perl - arg1 arg2 arg3 ...

You can tell this is kind of language specific.

1 Like

looks like this is exactly what i needed.

so we have answers for:

shell scripts
perl scripts

any ideas on:

python?
ruby?

As stated above, this is language specific and I can't answer for every single possible language.

So I can't speak for ruby, but python seems to work the same way as perl:

$ echo 'import sys; print(sys.argv[2]);' | python - a b c

b

$

Try the same for ruby and get back to me.

1 Like

Also, I should note, there's an important difference between running scripts like this and running them from a file: When you run from a pipe, stdin will not be a keyboard. So if any of these scripts are interactive, that may be a problem.

it worked. thank you so much!

1 Like