Home > Net >  pass inline arguments to shell script saved in in hdfs
pass inline arguments to shell script saved in in hdfs

Time:01-12

I have a shell script on hdfs that accepts 8-9 parameters. Normally, I can carry it out as follows:

sh sample.sh -mode FULL -status DELETE -id 1456 -region AP -path </path/to/filepath>

I tried hadoop fs -cat /dev/test/sample.sh | exec bash -mode FULL -status DELETE -id 1456 -region AP -path /dev/resultsFolder Even though I pass these arguments they are not read and script executes without the arguments. Threw error as No such file or directory What is the best way to deal with this?

CodePudding user response:

bash -c "$(hadoop fs -cat /dev/test/sample.sh)" bash -mode FULL -status DELETE -id 1456 -region AP -path /dev/resultsFolder

would work also.

-c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.

Perhaps more readably (although bash -s is rather elegant)

code=$(hadoop fs -cat /dev/test/sample.sh)
bash -c "$code" bash -mode FULL -status DELETE -id 1456 -region AP -path /dev/resultsFolder

CodePudding user response:

bash -mode FULL passes the -mode and FULL parameter to bash, not to your script. From the bash man page, describing the options:

-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.

and:

-s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.

I would therefore invoke it as

.... | bash - --mode FULL ....

According to the comment given by Gordon Davisson, we need to use -s here:

.... | bash -s -- -mode FULL ....
  •  Tags:  
  • Related