LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 04:23 PM   #1
cesarsj
Member
 
Registered: Mar 2019
Location: Patos de Minas, MG, Brazil
Distribution: Slackware
Posts: 41

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, 05:10 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,423

Rep: Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574
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, 07:45 PM   #3
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,095

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
#2 brings up the question: How are you “canceling” the MySQL?
 
Old 12-05-2019, 12:44 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,685

Rep: Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492Reputation: 2492
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, 07:32 AM   #5
cesarsj
Member
 
Registered: Mar 2019
Location: Patos de Minas, MG, Brazil
Distribution: Slackware
Posts: 41

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

Rep: Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207Reputation: 4207
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:51 PM
directing cron output to terminal screen sathish167 Red Hat 1 09-07-2010 06:59 AM
Directing Script output to Memory but without using a pipe | telecom_is_me Programming 4 06-29-2008 01:17 AM
Directing sound output to microphone Night Ink Linux - Software 2 12-28-2007 01:52 AM
Directing xosd output to X display from crontab BWebb Linux - General 3 06-30-2005 02:09 PM

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

All times are GMT -5. The time now is 01:37 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration