LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-22-2021, 01:34 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Bash function checks


I have a bash function that uses echo and sets a return value before exiting. I am using the following to set the variable var to the value sent to standard output.

Code:
var=$( myfunc --opt $arg )
I also use this inside if statements

Code:
var=$( myfunc --opt $arg )
if (( var == 1 )) && echo "something"


if myfunc --opt $arg 
then
  echo "do something"
fi

Both of these constructs use the standard output used by "echo".

For using the exit status, is it always the case that one uses "$?" ?

I would use in this way


Code:
$( myfunc --opt $arg )
if (( $? > 0 )) then
  echo "do something else"
fi

Here is myfunc

Code:
myfunc ()
{
 # do things here
 echo "$vb"
 return "$opst"
}
 
Old 12-22-2021, 02:28 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by Faki View Post
For using the exit status, is it always the case that one uses "$?"
No. For me, this is rather an exception. I prefer AND (&&) and OR (||) lists, or using the command itself as part of conditional construct.

Last edited by shruggy; 12-22-2021 at 02:32 PM.
 
Old 12-22-2021, 02:56 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,708

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
By definition the function exit status is either the value from the last run command or as specified using the return keyword. The value is 0 for success or 1-255 for failure and is assigned to $?.
The returned value via echo command is not limited to 0-255 values.

Do something if myfunc exit status=0
Code:
if myfunc --opt $arg 
then
  echo "do something"
fi
In the case command 1 && command 2, command 2 will only run if command's 1 exit status=0.
In the case command 1 || command 2, command 2 will only run if command's 1 exit status not 0.
 
Old 12-23-2021, 01:50 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Code:
# do not need $( command args )
command args
if (( $? > 0 )) then
....
fi
# and
if command args; then
....
fi
are identical
you can only return a single byte (from a function) = exit code of that function, return "string" is pointless, will not do what you want.
 
  


Reply

Tags
bash, function



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why no Ethernet function. Cable checks out okay on other computers Davidicus Linux - Networking 1 06-17-2013 04:04 AM
[Bash] MD5 Checks with Script. BiFo Programming 5 08-17-2010 06:08 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
bash script that checks authentication failures and sends mail panoskan Programming 4 06-06-2009 05:24 PM
Bash script thats checks for faild login attempts. k1piee Programming 4 02-11-2009 09:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration