[ale] bash return and exit
JD
jdp at algoloma.com
Tue Sep 17 16:01:37 EDT 2013
I realize this question is not necessarily showing the real end-purpose of the
script, but do people still script package management stuff?
There are lots of tools to make that aspect easier, though some of the tools are
worse than the root problem. When I finally sat down to learn Ansible, it took
me about 30 minutes to get a working "task" and to understand enough to manage
package manager installations, make settings inside a conf file and push
pre-made config files. Using template files took a little longer, but not too
much. If the change isn't needed, nothing is done and there is positive feedback
if nothing was needed or if changed.
Sorry, if this isn't the end-goal of this effort, but Ansible really has
simplified my package management and CMDB life. Only 1 machines needs ansible,
the clients requirements have always been met before I tried anything. Never
needed to install any packages on the client machines to be managed.
On 09/17/2013 01:50 PM, leam hall wrote:
> Very nice, all, thanks!
>
> function is_rpm_installed() {
> # Return 0 if the RPM is installed, otherwise 1
> rpm -q $1 > /dev/null
> return $?
> }
>
>
> if is_rpm_installed fred; then echo "have it"; else echo "nope"; fi
> nope
>
> if is_rpm_installed kernel; then echo "have it"; else echo "nope"; fi
> have it
>
>
>
>
>
> On Tue, Sep 17, 2013 at 12:24 PM, Scott Plante <splante at insightsys.com
> <mailto:splante at insightsys.com>> wrote:
>
> I'm not sure I understand what you're trying to do, but it appears you're
> confusing standard out with return code. Backticks `cmd` and $(cmd) capture
> the standard output of a command. That's separate from the return code,
> which is meant to be zero if all works or some other number indicating the
> type of error. The $? resolves to the most recent return code, but you could
> also do this:
>
> if do_something
> then echo ok
> else echo sorry bub
> fi
>
> and bypass the $?
>
> Sorry, in a hurry. Hope I didn't typo or make a mistake.
>
> Scott
> --------------------------------------------------------------------------------
> *From: *"leam hall" <leamhall at gmail.com <mailto:leamhall at gmail.com>>
> *To: *"Atlanta Linux Enthusiasts" <ale at ale.org <mailto:ale at ale.org>>
> *Sent: *Tuesday, September 17, 2013 12:06:44 PM
> *Subject: *[ale] bash return and exit
>
>
> I've looked at a few google searches and am not sure I understand what I'm
> seeing. In bash, I want to have a function do a test, and have a variable in
> the calling program set based on the function's actions. So far it seems as
> if I have to either "echo" the result or have the calling function use $?.
>
> Calling script:
>
> MY_VAR=`my_function`
>
> # This fails:
> my_function() {
> do_something
> return $?
> }
>
> # This works:
> my_function() {
> do_something
> if [ $? -eq 0 ]
> echo 0
> else
> echo 1
> fi
> }
>
> The first function works if the parent's calls:
>
> MY_VAR=`my_function; echo $?`
>
> Am I looking for something that's just not in bash?
>
More information about the Ale
mailing list