Linked Questions

11
votes
3answers
7k views

How can I pipe output to another process, but retain the error state of the first process? [duplicate]

Possible Duplicate: Get exit code of process that's piped to another I am using the following command line (in a makefile) to pipe the verbose error messages from my compiler through a perl ...
10
votes
2answers
22k views

How do I capture the return status and use tee at the same time in korn shell? [duplicate]

Consider Source code: 1. Parent.sh #!/usr/bin/ksh # No tee ksh Child.sh; exit_status=$?; echo "Exit status: ${exit_status}" # Using tee ksh Child.sh | tee -a log.txt; exit_status=$?; echo "Exit ...
4
votes
2answers
1k views

How to capture the exit status of a non-final command in a pipeline? [duplicate]

I want to capture the exit status of a command that takes place somewhere in a pipeline before the last position. For example, if the pipeline is something like command_1 ... | command_2 ... | ...
5
votes
2answers
123 views

Use ts without losing exit value [duplicate]

I have a build script that executes a long command that produces lots of output which went like this: ./compile In order to troubleshoot compilation performance, I want to use ts (from moreutils) ...
2
votes
2answers
4k views

exit code of the command **before** last? [duplicate]

I'm running a script within a script, release.sh and caller.sh (caller.sh calls release.sh). Both are bash. release.sh contains a bunch of conditions with 'exit 1' for errors. caller.sh contains a ...
1
vote
1answer
2k views

Check for failure of piped command [duplicate]

Possible Duplicate: Get exit code of process that's piped to another If I pipe the output of one command into another, is there a way for me to check explicitly whether the first command ...
1
vote
1answer
2k views

Get exit code from preceding command before pipe [duplicate]

I have this: node ${SUMAN_RUNNABLE} | tee -a run.sh.log EXIT_CODE=$?; but it looks like the exit code is always 0 because tee is providing the code not the node executable. Is there a way to ...
2
votes
1answer
446 views

How to get exit status of a particular command in a pipeline? [duplicate]

I am running this while loop in a script to take mysqldump and compress it, but I want to exit the script if the table doesn't exist. Below is what I have tried. while read TABLES; do sudo ...
4
votes
1answer
940 views

returncode of command in a pipline [duplicate]

The following script runs on Solaris using /bin/ksh and on Linux using /bin/sh cmd | tee -a cmd.log | tail exit $? The output of cmd is saved in a file cmd.log and the last lines are displayed on ...
8
votes
0answers
262 views

how to get exit status from the command before the last [duplicate]

I have solaris machine ( solaris 10 ) please Look on the following simple commands (haconf -makerw | grep -iq "Cluster already writable") # haconf -makerw VCS WARNING V-16-1-10364 Cluster ...
1
vote
0answers
65 views

How to capture "$?" before tee smashes it? [duplicate]

I've got a test script that repeatedly builds a library under different configurations. It also tee's the output for postmortem failures: $MAKE" static dynamic cryptest.exe 2>&1 | tee -a "$...
1
vote
0answers
24 views

Test execution output of a command before a pipe [duplicate]

I would like to test the execution of a php script that redirect its stdout and stderr to a file with tee. php /path/to/script --param 2>&1 | tee -a /path/to/log if ( test $? -ne 0 ); then ...
0
votes
1answer
23 views

Determine failing command in Bash pipe when working with set -o pipefail [duplicate]

I'm using set -o pipefail in my scripts as usual. That works perfectly well and as documented for something like curl ... | jq ... | psql ... However, is there a way to determine which command along a ...
283
votes
6answers
35k views

What's the difference between $(stuff) and `stuff`?

There are two syntaxes for command substitution: with dollar-parentheses and with backticks. Running top -p $(pidof init) and top -p `pidof init` gives the same output. Are these two ways of doing the ...
181
votes
9answers
317k views

tar: Removing leading `/' from member names

root@server # tar fcz bkup.tar.gz /home/foo/ tar: Removing leading `/' from member names How can I solve this problem and keep the / on file names ?
11
votes
4answers
6k views

Bash exit status used with PIPE

I am trying to understand how exit status is communicated when a pipe is used. Suppose I am using which to locate a non-existent program: which lss echo $? 1 Since which failed to locate lss I got ...
11
votes
5answers
1k views

Prevent the second command from starting in the bash pipe if the first command fails

After learning (more or less) some useful discussions about pipes like Get exit status of process that's piped to another and Exit when one process in pipe fails I still can't avoid starting the ...
7
votes
3answers
835 views

Piping 'false' does not give non-zero result code

I understand these: true; echo "$?" # 0 false; echo "$?" # 1 true | echo "$?" # 0 But not this: false | echo "$?" # 0 ...Why doesn't it print 1? And how could I force a ...
5
votes
3answers
14k views

Parent script continues when child exits with non-zero exit code [closed]

I have a script that calls another script. When the child script fails, I'd like the parent to fail as well. In the child script child_1.sh, I have something like this: if [ $SOME_BAD_CONDITION ] ; ...
3
votes
1answer
4k views

Chaining pipe | with &&

I have this command where I want to filter make output: cd /app && make && sudo nginx -g 'daemon off;' What is the correct way to insert make | pv -q -L 100 here?
3
votes
2answers
1k views

.DELETE_ON_ERROR behavior with make and pipelines

The .DELETE_ON_ERROR target will tell make to delete a target if a rule fails. But this doesn't work for pipelines, because the exit status value $? holds the value of the last program in the pipeline....
4
votes
2answers
454 views

Command substitution declaration syntax in bash - which of these two is better practice? [duplicate]

This is part of a bash find loop, and I wondered which is more correct syntax and why? filename="$(echo "$i" | cut -c5-)"; filename=`echo "$i" | cut -c5-`; Both function for the purpose of getting ...
2
votes
3answers
2k views

Get return code of first piped command using eval?

I wish to execute different commands and check the return code afterwards before moving to the next steps in the script. At the same time I also wish to log the output of the executed commands to a ...
0
votes
1answer
2k views

exit status and no output

I have a command such that bar > /dev/null and I want to know the exit status of bar. I read some posts su about ${PIPESTATUS[0]} but this works when one pipes the output via | and I can't make ...
1
vote
1answer
2k views

how to get the timeout exit code while run a command in background?

I'm writing a shell script to run ping command in background, and in the same time, set a timeout value for ping command, I want to get the exit code if the ping is ended by timeout, I use below: ...
3
votes
2answers
1k views

exit a script if an ls line did not find any match

I am writing this script that have this line: ls -lrt /dir1/dir2/filename*.txt | tail -1 | awk '{print $9}' | read variable What I wanted is to exit the script (without using if-statement) if it ...
0
votes
1answer
245 views

Fail script if command fail but run all commands in any case

There are a few commands in a script I need to run, it's a custom linter for source code. Each run generates a report and fails with exit code 1 in case of rules violations. I need to run all commands ...
0
votes
2answers
397 views

How can I shorten an if then else using the [] format

I want to shorten this to a one liner: if ls --version 2>/dev/null | grep -q 'coreutils'; then alias ls='ls --color=always' else alias ls='ls -G' fi I frequently use [] for conditionals, e.g. [ -...
0
votes
2answers
289 views

What's the difference of get command output using`command`and $(command) in Shell? [duplicate]

What is the difference in collect a command output in Shell script using `command` and $(command)? For example: # IP1=$(ifconfig eth0 | grep -e "inet " | cut -d: -f2 | awk '{print $1}') # IP2=`...
1
vote
2answers
377 views

How to check left command's exit status in pipe "left | right" in dash when left command produces large output

It seems that the variable "PIPESTATUS" is unavailable in dash. Simple separate execution is not working because the left command produces very large output. I used a fifo to do this task: #!/bin/...
2
votes
2answers
227 views

Remove files with no data and only header

I want to remove the files which have only header and no data. I tried the below command and it is working fine, Only issue is it is giving exit status as 1 which should be zero. wc -l /Path/File_* |...
0
votes
0answers
109 views

How can I get exit status of subshell?

A classical example of piping: $ false | true $ echo ${PIPESTATUS[@]} 1 0 Same example run in a subshell and assigning results into a variable: $ process="$(false | true)" $ echo ${PIPESTATUS[@]} 0 ...
1
vote
1answer
41 views

Best way to prefix output of a command and check its return value [closed]

is there a better way than the following code to prefix the output of a command but still being able to check its return value? command > "$tmpfile" 2>&1 ret=$? sed 's/^/ | /' "$tmpfile" ...