kensuke1984
kensuke1984

Reputation: 995

Use Bash with script text from stdin and options from command line

I want to use /bin/bash (possibly /bin/sh) with the option -f passed to, and handled by, the script. Precisely,

while getopts f OPT
do
  case $OPT in
    "f" ) readonly FLG_F="TRUE" 
   esac
done

if [ $FLG_F ]; then
  rm -rf $KIBRARY_DIR
fi

and when these lines are in a file http://hoge.com/hoge.sh,

I can do this, for instance,

wget http://hoge.com/hoge.sh
/bin/bash hoge.sh -f

but not

/bin/bash -f hoge.sh

I know the reason but I want to do like this,

wget -O - http://hoge.com/hoge.sh | /bin/bash

with -f option for hoge.sh not for /bin/bash

Are there any good ways to do this?

/bin/bash <(wget -O - http://hoge.com/hoge.sh) -f

worked. but this is only for bash users, right?

Upvotes: 0

Views: 768

Answers (1)

Diego Torres Milano
Diego Torres Milano

Reputation: 69188

Using bash you can do

wget -O - http://hoge.com/hoge.sh | /bin/bash -s -- -f

as with -s commands are read from the standard input. This option allows the positional parameters to be set too. It should work with other POSIX shells too.

Upvotes: 4

Related Questions