LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2014, 07:19 AM   #1
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
bash, return value and $? from a command call


hi,

when I execute a program in a script I get $? with 0 if it works and 1 if it fails

in a script ,if I do

myvar=$(some_command)
$? is always 0,even if some_command fails,

also with
myvar=`some_command`
$? does not tell me if some_command was successfully
?$ -eq 0.
do I do something wrong?

how can I have the return value of a program and the information if it was successfully
 
Old 05-26-2014, 07:47 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably this discussion helps a bit: http://www.unixli.com/q/answers-how-...ail-23026.html
 
Old 05-26-2014, 09:01 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Works fine for me
Code:
$ X=$(grep "if" /etc/profile)
$ echo $?
0
$ X=$(grep "plugh" /etc/profile)
$ echo $?
1
That's in bash 4.x. What shell are you using?
 
Old 05-26-2014, 09:27 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Works for me, too. Example: set 'erase' if 'kbs' is defined
Code:
X=$(tput kbs) && stty erase "$X"
 
Old 05-26-2014, 09:50 AM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
What is "some_command"?
 
Old 05-27-2014, 02:10 AM   #6
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Original Poster
Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
the problem seems to be that I am in a function and use a local variable

Code:
func(){
  local foo=$(sqlite3 doesnotexist "absolut no sql;")
  echo $?
}

func
this prints 0, but I expected 1
 
Old 05-27-2014, 02:56 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
try in two steps:
Code:
func() {
  local foo
  foo=$(sqlite3 doesnotexist "absolut no sql;")
  echo $?
}
 
1 members found this post helpful.
Old 05-27-2014, 03:23 AM   #8
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Original Poster
Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by NevemTeve View Post
try in two steps:
Code:
func() {
  local foo
  foo=$(sqlite3 doesnotexist "absolut no sql;")
  echo $?
}
this works, basically
I had local -r foo=$(some_command), so I need to remove the -r or use 2 steps
checked also that using declare does change $?
 
Old 05-27-2014, 03:45 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
if you are dead-set on using 'readonly', then it will be longer:

Code:
func() {
  local tmp
  tmp=$(sqlite3 doesnotexist "absolut no sql;") || { echo $?; exit; }

  local -r foo="$tmp"
  echo 0
}
 
Old 05-29-2014, 01:19 PM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Partially doing a double-take here.

(1) Yes it should work as others have said

(2) Why the need to check $? when you've assigned the result to a variable? If I do that, I check "the variable". (That's where I'm doing the double-take.)

(3) When debugging a script, start by adding "set -xv" at the top of the script to see debug information.

Some tips Blog Entry: Bash Scripting for Dummies and Geniuses
 
Old 05-29-2014, 01:29 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by rtmistler View Post
(2) Why the need to check $? when you've assigned the result to a variable? If I do that, I check "the variable". (That's where I'm doing the double-take.)
I'm not sure I get your meaning. The variable will contain the program's stdout. $? will contain the program's exit status. The two are not the same and can not be used interchangeably. Trying to parse a program's stdout to see if an error occurred would be slow, cumbersome, and could even be impossible with certain commands.
 
Old 05-29-2014, 01:59 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by suicidaleggroll View Post
I'm not sure I get your meaning. The variable will contain the program's stdout. $? will contain the program's exit status. The two are not the same and can not be used interchangeably. Trying to parse a program's stdout to see if an error occurred would be slow, cumbersome, and could even be impossible with certain commands.
Sorry, you are correct. I was incorrectly expecting exit status to be stored in that variable. But of course the easiest thing to verify my ignorance was to write a short script and include "set -xv".
 
  


Reply



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
bash script read command issue with carriage return itamarm Linux - General 3 06-17-2013 07:37 AM
System Call (not bash command) to format Linux partition raj010 Linux - Newbie 4 06-17-2011 08:26 PM
return value of select() call sinu_nayak2001 Programming 9 10-09-2009 05:33 AM
Bash command/script to return group of file IHateFriction Programming 3 03-09-2007 12:31 PM
How do i call bash command from exe in wine jacktheripperis Programming 1 04-12-2006 03:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 PM.

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