LinuxQuestions.org
Visit Jeremy's Blog.
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-04-2019, 03:23 PM   #1
cesarsj
Member
 
Registered: Mar 2019
Location: Patos de Minas, MG, Brazil
Distribution: Slackware
Posts: 159

Rep: Reputation: Disabled
Question How do I get the exit code while directing the command output to a file?


I am canceling the mysql command on purpose to have error exit code. In the command below, the exit code is being 0 successful because of the tee. How can I solve this?

Code:
mysql --defaults-extra-file=/etc/my.cnf.d/.mylogin.cnf -e"use radius; call sp_monthly_table_backup(@Msg);" | tee -a $CLEARLOG $ZBXLOG > /dev/null;

EXITCODE=$(echo $?);
 
Old 12-04-2019, 04:10 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
I am canceling the mysql command on purpose to have error exit code.
Why? What are you trying to accomplish?
Code:
sleep 10 | tee -a myfile.txt
echo $?
0

sleep 10 | tee -a myfile.txt
^C
echo $?
130
Give more info, maybe someone can shove you in the right direction.
 
2 members found this post helpful.
Old 12-04-2019, 06:45 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
#2 brings up the question: How are you “canceling” the MySQL?
 
Old 12-04-2019, 11:44 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just to be clear, you only get the exit code for the last cmd in a pipeline, so in your case, the tee cmd ...
Code:
exit_code=$?
No need for echo & sub-shells - plenty of examples on the web
 
Old 12-05-2019, 06:32 AM   #5
cesarsj
Member
 
Registered: Mar 2019
Location: Patos de Minas, MG, Brazil
Distribution: Slackware
Posts: 159

Original Poster
Rep: Reputation: Disabled
I got what I wanted with echo $ {PIPESTATUS [0]}
 
Old 12-05-2019, 08:08 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
yes, that is the array PIPESTATUS, but you need to use first set -o pipefail, otherwise it will not work.
 
  


Reply

Tags
exit code, linux



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
exit(communication error), terminated by calling exit(), return code: 255 miliboy Linux - Newbie 11 02-27-2013 01:51 PM
directing cron output to terminal screen sathish167 Red Hat 1 09-07-2010 05:59 AM
Directing Script output to Memory but without using a pipe | telecom_is_me Programming 4 06-29-2008 12:17 AM
Directing sound output to microphone Night Ink Linux - Software 2 12-28-2007 12:52 AM
Directing xosd output to X display from crontab BWebb Linux - General 3 06-30-2005 01:09 PM

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

All times are GMT -5. The time now is 02:23 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