Skip to main content
Post Made Community Wiki by Paulo Almeida
Correct misunderstanding on passing variables
Source Link

This should work:

#!/bin/bash set -x NARGS=$# function main { if [ $NARGS -lt 1 ]; then usage fi echo good } function usage { echo "Usage: $0 <outputdir>" exit 1 } main $NARGS 

I left the set -x there on purpose. It's useful to debug these errors. You can see that ifIf you don't pass $NARGSany arguments to the mainmain function, $# isbecomes 0 inside it (replace $NARGS with $# in the if condition to see this).

This should work:

#!/bin/bash set -x NARGS=$# function main { if [ $NARGS -lt 1 ]; then usage fi echo good } function usage { echo "Usage: $0 <outputdir>" exit 1 } main $NARGS 

I left the set -x there on purpose. It's useful to debug these errors. You can see that if you don't pass $NARGS to the main function, $# is 0 (replace $NARGS with $# in the if condition to see this).

This should work:

#!/bin/bash set -x NARGS=$# function main { if [ $NARGS -lt 1 ]; then usage fi echo good } function usage { echo "Usage: $0 <outputdir>" exit 1 } main 

I left the set -x there on purpose. It's useful to debug these errors. If you don't pass any arguments to the main function, $# becomes 0 inside it (replace $NARGS with $# in the if condition to see this).

Source Link

This should work:

#!/bin/bash set -x NARGS=$# function main { if [ $NARGS -lt 1 ]; then usage fi echo good } function usage { echo "Usage: $0 <outputdir>" exit 1 } main $NARGS 

I left the set -x there on purpose. It's useful to debug these errors. You can see that if you don't pass $NARGS to the main function, $# is 0 (replace $NARGS with $# in the if condition to see this).