bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exit application with two function calls


From: abi
Subject: Re: Exit application with two function calls
Date: Sun, 27 Jan 2008 16:11:34 -0600 (CST)
User-agent: SquirrelMail/1.4.9a

> abi@e-arroyo.net schrieb:
>> Hi,
>>
>> I am working on a script and ran into an unusual program. Consider the
>> following script which I called how.sh.
>>
>> =====
>>
>> #!/bin/bash
>>
>> error () {
>>
>>    echo -e "\n\terror: ${*}\n"
>>    exit;
>>
>>    # kill $$
>>
>> }
>>
>> check_file () {
>>
>>    input="$*"
>>
>>    if [ -e $input ]; then
>>       echo "$input"
>>    else
>>       error "invalid directory: $input"
>>    fi
>>
>> }
>>
>>
>> chkFile="`check_file $1`"
>> echo $chkFile
>> echo "don't print"
>>
>> =====
>>
>> When I run the command above I get this output:
>>
>> # ./how.sh /asdf
>> error: invalid directory: /asdf
>> don't print
>
> The reason is that `check_file` is executed in a subshell, so 'exit'
> just leaves the subshell, not the script itself.
>
> The command substitution is unnecessary anyway, as the result (if any)
> will simply be $1 itself. Thus, if you replace the main part by
>
>      check_file $1
>      echo $1
>
> it should work as expected.
>
> Regards,
> Bernd
>
> --
> Bernd Eggink
> monoped@sudrala.de
> http://sudrala.de
>


Thanks for the reply! While you are correct, this isn't exactly the
intended functionality. In the end I'd like $chkFile variable to contain
the path or exit the script printing the error message. This is probably
unclear due to the fact that only a snippet is posted and not the entire
script.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]