Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

[SOLVED] Bash command exit status

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
humbletech99
Veteran
Veteran
User avatar
Posts: 1229
Joined: Thu May 26, 2005 9:30 am
Location: London

[SOLVED] Bash command exit status

  • Quote

Post by humbletech99 » Thu Jul 06, 2006 9:32 am

When I do

Code: Select all

somecommand | head -n 2
$? is always the exit status of the "head" command rather than that of "somecommand"

Is there a way to do this and to test the exit status of "somecommand" rather than "head" (which will always be success even if somecommand fails) ?
Last edited by humbletech99 on Thu Jul 06, 2006 12:41 pm, edited 1 time in total.
The Human Equation:

value(geeks) > value(mundanes)
Top
Genone
Retired Dev
Retired Dev
User avatar
Posts: 9656
Joined: Fri Mar 14, 2003 6:02 pm
Location: beyond the rim

  • Quote

Post by Genone » Thu Jul 06, 2006 12:22 pm

Code: Select all

foo="$(somecommand)"
rval=$?
echo $foo | head -n 2
Top
humbletech99
Veteran
Veteran
User avatar
Posts: 1229
Joined: Thu May 26, 2005 9:30 am
Location: London

  • Quote

Post by humbletech99 » Thu Jul 06, 2006 12:24 pm

this doesn't work properly because the output's lines are crushed into a single line when assigned to the variable foo so you can't "head" it later...
The Human Equation:

value(geeks) > value(mundanes)
Top
GordSki
Guru
Guru
User avatar
Posts: 329
Joined: Mon Oct 18, 2004 7:48 pm
Contact:
Contact GordSki
Website

  • Quote

Post by GordSki » Thu Jul 06, 2006 12:36 pm

This works, needs the double quotes round $foo in the echo:

Code: Select all

foo="$(somecommand)"
rval=$?
echo "$foo" | head -n 2
G.
Top
humbletech99
Veteran
Veteran
User avatar
Posts: 1229
Joined: Thu May 26, 2005 9:30 am
Location: London

  • Quote

Post by humbletech99 » Thu Jul 06, 2006 12:39 pm

you're right, so it does!

those double quotes round the variable reference with the echo make all the difference.

thanks!
The Human Equation:

value(geeks) > value(mundanes)
Top
mv
Watchman
Watchman
User avatar
Posts: 6795
Joined: Wed Apr 20, 2005 12:12 pm

Re: [SOLVED] Bash command exit status

  • Quote

Post by mv » Thu Jul 06, 2006 2:51 pm

humbletech99 wrote:When I do

Code: Select all

somecommand | head -n 2
$? is always the exit status of the "head" command rather than that of "somecommand"

Is there a way to do this and to test the exit status of "somecommand" rather than "head" (which will always be success even if somecommand fails) ?
PIPESTATUS[0] (see man bash)
Top
Post Reply

6 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic