LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators (https://www.linuxquestions.org/questions/programming-9/bash-print-usage-statement-and-exit%3B-otherwise-continue-using-bash-shorthand-operators-405864/)

stefanlasiewski 01-20-2006 12:51 PM

Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators
 
I'm trying to write a Bash script using some shorthand Bash operators.

I want a short script to test for a commandline argument.
- If $1 is not present, then print a Usage statement and exit from the function.
- If there is a value, then continue with the rest of the function.

Code:

function testIt {
  # If no arguments, print usage statement & exit from function
  [[ -n "$1" ]] || ( echo "Usage: testIt foo"; exit 0 ; )
  # Else, continue and print the arguments
  echo "FOO=$1"
}

Here's the problem. If I run the command with no arguments, the 'echo "FOO=$1"' is still printed.

Code:

$ testIt
Usage: testIt foo
FOO=

I know this is simple, but I just can't wrap my head around it this morning. It's been a long week.

Any help is appreciated.

-= Stefan

dogpatch 01-20-2006 01:19 PM

Try this:
Code:

#!/bin/sh
if test "$1" == "" ; then
        echo $'\a'Supply 1 parameter, e.g:
        echo $0 BAR
        exit
fi
echo FOO=$1


gilead 01-20-2006 02:27 PM

I think the reason you were still getting the "FOO=" output is that ( echo "Usage: testIt foo"; exit 0 ; ) runs in a subshell because of the brackets. It exits from the subshell at your exit statement and continues with the script. dogpatch's example executes in one level...

stefanlasiewski 01-20-2006 08:20 PM

Thanks Dogpatch. The 'if ; then; fi' certainly works.

I was just trying to figure out how to do this with the shorthand operators like [[ TEST ]] || and && .

Since I write these sorts of scripts often, I was hoping to find a one-liner to do this. Plus, it'd slick.

It *should* work at some level, right?

Hrm, I'll have to work on this more.

jlliagre 01-20-2006 08:55 PM

Here is one way to do it:
Code:

..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; exit 0 ; }
..


gnashley 01-21-2006 12:07 PM

[[ -n "$1" ]] && echo "FOO=$1" || echo "Usage: testIt foo"

jlliagre 01-21-2006 01:21 PM

Quote:

Originally Posted by gnashley
[[ -n "$1" ]] && echo "FOO=$1" || echo "Usage: testIt foo"

You are missing the exit requirement.

stefanlasiewski 01-24-2006 03:56 PM

Quote:

Originally Posted by jlliagre
Here is one way to do it:
Code:

..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; exit 0 ; }
..


This statement will exit from the current shell. And actually, this is probably the preferred behavior within a shell script-- the script spawns a subshell (e.g. #!/bin/bash ), and the script & subshell will exit if it encounters an error.

However, if I put this in function within my current interactive shell, my current shell will exit.

I'm not saying that your solution is wrong ;) It is just an interesting thing to notice.

jlliagre 01-25-2006 01:12 AM

Quote:

Originally Posted by stefanlasiewski
This statement will exit from the current shell. And actually, this is probably the preferred behavior within a shell script-- the script spawns a subshell (e.g. #!/bin/bash ), and the script & subshell will exit if it encounters an error.

However, if I put this in function within my current interactive shell, my current shell will exit.

I'm not saying that your solution is wrong ;) It is just an interesting thing to notice.

If you want only the function to be exited, here's a way:
Code:

..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; return ; }
..


stefanlasiewski 02-07-2006 05:20 PM

Thank you jlliagre. Both of your solutions work.

Thanks!


All times are GMT -5. The time now is 09:33 PM.