ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.