0

I have couple of scripts which call into each other. However when I pass

Snippet from buid-and-run-node.sh

OPTIND=1 # Reset getopts in case it was changed in a previous run
while getopts "hn:c:f:s:" opt; do
    case "$opt" in
        h)
            usage
            exit 1
            ;;
        n)
            container_name=$OPTARG
            ;;
        c)
            test_command=$OPTARG
            ;;
        s)
            src=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

$DIR/build-and-run.sh -n $container_name -c $test_command -s $src -f $DIR/../dockerfiles/dockerfile_node

Snippet from build-and-run.sh

OPTIND=1 # Reset getopts in case it was changed in a previous run
while getopts "hn:c:f:s:" opt; do
    case "$opt" in
        h)
            usage
            exit 1
            ;;
        n)
            container_name=$OPTARG
            ;;
        c)
            test_command=$OPTARG
            ;;
        f)
            dockerfile=$OPTARG
            ;;
        s)
            src=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

I am calling it as such

build-and-run-node.sh -n test-page-helper -s ./ -c 'scripts/npm-publish.sh -r test/test-helpers.git -b patch'

with the intention that npm-publish.sh should run with the -r and -b parameters. However when I run the script I get

build-and-run.sh: illegal option -- r

which obviously means it is the build-and-run command that is consuming the -r. How do I avoid this?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user3626708
  • 727
  • 2
  • 8
  • 24

1 Answers1

1

You need double quotes around $test_command in buid-and-run-node.sh, otherwise that variable is being split on the white space and appears to contain arguments for buid-and-run.sh. Like this:

$DIR/build-and-run.sh -n $container_name -c "$test_command" -s $src -f $DIR/../dockerfiles/dockerfile_node

Further Info

As the comment below rightly points out, it's good practice to quote all variables in Bash, unless you know you want them off (for example, to enable shell globbing). It's also helpful, at least in cases where the variable name is part of a larger word, to use curly braces to delineate the variable name. This is to prevent later characters from being treated as part of the variable name if they're legal. So a better command call might look like:

"${DIR}/build-and-run.sh" -n "$container_name" -c "$test_command" -s "$src" -f "${DIR}/../dockerfiles/dockerfile_node"
Community
  • 1
  • 1
SpinUp __ A Davis
  • 5,016
  • 1
  • 30
  • 25
  • 1
    Might as well add quotes everywhere else they're missing while you're at it, no? Better to encourage folks to enable string-splitting and globbing (by leaving off quotes) only for expansions where they explicitly *want* these behaviors, than to have them on-by-default and then off-by-necessity; in the latter case, the correctness of one's scripts correlates much too closely with the breadth of one's test cases exercised in determining where necessity exists. – Charles Duffy Oct 14 '16 at 16:56