Linked Questions

273 votes
4 answers
102k views

Piping command output to tee but also save exit code of command [duplicate]

I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile. #!/bin/bash ... mvn clean install $@ | tee $logfile echo $? # Does not show the return code ...
Wolkenarchitekt's user avatar
10 votes
1 answer
4k views

how to get the exit status of the first command in a pipe? [duplicate]

I made a simple script: $ more test.bash #!/bin/bash echo test exit 1 When I run the script , the exit status should be 1 $ /tmp/test.bash echo $? 1 But when I run this as the following /tmp/test....
maihabunash's user avatar
  • 1,712
13 votes
4 answers
5k views

Pipe command output, but keep the error code [duplicate]

How do I get the correct return code from a unix command line application after I've piped it through another command that succeeded? In detail, here's the situation : $ tar -cEvhf - -I ${...
SamiBOB's user avatar
  • 243
8 votes
2 answers
7k views

Get exit code when piping into grep -v [duplicate]

I have a script like this: #!/bin/sh echo "hello" echo "goodbye" exit 1 When I run it on its own, I get the failed exit code as I expect. $ ./fail.sh hello goodbye $ echo $? 1 However, when I run ...
limp_chimp's user avatar
  • 15.5k
4 votes
6 answers
1k views

piping output through sed but retain exit status [duplicate]

I pipe the output of a long-running build process through sed for syntax-highlighting, implemented as a wrapper around "mvn". Further I have a "monitor" script that notifies me on the desktop when ...
Bastl's user avatar
  • 1,471
3 votes
2 answers
4k views

How to catch mysqldump error in bash script? [duplicate]

mysqldump --verbose -h $dbhost --max_allowed_packet=1G --extended- insert --single-transaction --add-drop-database --opt $dbname -- user=$dbusername --password=$dbpassword | gzip -1 > $filename....
gurpsone's user avatar
  • 503
1 vote
1 answer
2k views

LInux script error return codes [duplicate]

I have the following code .. while read copy do cp -v $copy | tee -a $LOGFILE echo $? if [[ $? -ne 0 ]] then echo "Error copying files. Exiting ..." | tee -a $LOGFILE ...
user3738064's user avatar
3 votes
2 answers
185 views

How to read return code of command when piped with another command [duplicate]

I have a bash script where I execute a command and tee to a file. On checking the return code, it is always 0 which is for the tee <> command. make all | tee output.log if [[ $? -ne 0 ]]; then ...
oak1208's user avatar
  • 305
0 votes
1 answer
395 views

How to know if a command in a pipe run successfully [duplicate]

I am trying to write a script which will download git using apt and after successful download of git I want to set the config for git. I wrote a script that does the work. But I am curious to know is ...
atiq1589's user avatar
  • 2,335
1 vote
1 answer
268 views

How to return error code when using tee in bash [duplicate]

I want to use tee to write the output of any command (first command) to a file, but I want to get the return code from the first command instead of the tee command. Example 1: The following code will ...
stackbiz's user avatar
  • 1,798
0 votes
1 answer
318 views

Bash pipe swallows exit code when pipe like sed or tee [duplicate]

I'm using turbo feature called turbo prune which creates folder out with partial copy of my monorepo, but unfortunately that means that when running in GitHub Actions commands like turbo run lint will ...
JLarky's user avatar
  • 10.3k
1 vote
0 answers
92 views

Get the exit code status of second to last command that was run [duplicate]

say I have a set of commands like touch /tmp/file.json tool test --json |& tee /tmp/file.txt firstchar=$(head -c 1 /tmp/file.txt) ret=$? #begin conditions. if ((ret==0); then ##do something ...
Jshee's user avatar
  • 2,680
0 votes
0 answers
34 views

i have this script in nmap but he dont do what i need [duplicate]

i have this script in nmap but he dont do what i need i need if host done off do ./2.sh mv 1.sh ~/1sh #!/bin/bash count=$(nmap -sP -iL hostlist -oG pingscan | grep from* | wc -l) >>/root/host....
estranho's user avatar
1 vote
0 answers
30 views

Bash command with pipe('|') alway return exit code of 0, even in error case [duplicate]

Code a.py import sys import errno def main(): try: raise Exception("asdf") sys.stdout.write('{"v1": "kk"}') sys.stdout.flush() except ...
user3595632's user avatar
  • 5,770
1 vote
0 answers
15 views

bash, getting exit status in pipe [duplicate]

I'm running some test scripts with this command, so I can see the test output & have a log of the output. How can I access the exit status of "python test.py", or exit if "python test.py" fails ...
user1977661's user avatar

15 30 50 per page