Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [exit-status]

Use this tag If your question revolves around determining or utilizing the exit status (return code) of a command. Common syntax involves the $? variable and the && and || symbols.

3
votes
4answers
3k views

Bash: Elegant way to set status? [closed]

true && echo foo will print foo. false && echo foo will not print foo. bash -c "exit $a" && echo foo will print foo depending on $a. Is there a more elegant way to write the ...
3
votes
3answers
761 views

Is the exit status of a command implemented by the command or a shell process which executes the command?

From bash manual The exit status of an executed command is the value returned by the waitpid system call or equivalent function. Exit statuses fall between 0 and 255, though, as explained below,...
3
votes
2answers
422 views

Why can't I get pgrep output right to variable on bash script?

I'm trying to make a script to either quit compton if it's running or start it if it's not running. I've read from man that it should exit 1 if process is found, so I've tried to make a script that ...
3
votes
3answers
7k views

What return/exit values can I use in bash functions/scripts?

I want to know what return values we can use that will not be mistaken by for ex. SIGINT? ex.: $sleep 10 $#hit ctrl+c $echo $? 130 so I know I must not use anything like return 130 or exit 130 so ...
3
votes
2answers
961 views

What is the correct way of dealing with exit code of the last command?

I am currently doing this in a POSIX shell script: success=false pv --wait "$input_filename" | openssl enc -aes-256-cbc -d -salt -out "$output_filename" && success=true if [ "$success" = "...
3
votes
5answers
1k views

How to gzip only if mysqldump does not error?

I want to zip the sql dump if and only if mysqldump does not produce any error. I try to put the wrong password, but it still perform the gzip. mysqldump -u username -ppassword dbname |& if [ $? ...
3
votes
1answer
3k 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

Bash Script Function Return True-False

I want to query the function by returning the value. My codes are as follows; check(){ file=/root/Turkiye.txt local funkx=$1 while read line; do if [ "$line" == "$funkx" ] ...
3
votes
2answers
979 views

Get PID and return code from 1 line bash call

Imagine we have a long command sleep 10 (for instance). We would like to execute it on another server using python (the ssh library paramiko to be specific). I need a 1 line command that starts the ...
3
votes
1answer
2k views

Why is “Doing an exit 130 is not the same as dying of SIGINT”?

From Stéphane Chazelas's reply at https://unix.stackexchange.com/a/230568 Ideally, we'd want to report to our parent that we died of a SIGINT (so that if it's another bash script for instance, ...
3
votes
1answer
75 views

Can't determine why exit code is not being logged

Weird one, so the following will not log "bash exit code..." #!/usr/bin/env bash OUTPUT_PATH=${PROJECT_ROOT:-$PWD}/npm-install-output.log npm --loglevel=warn --progress=false install > ${...
3
votes
3answers
766 views

How to reverse error code in Makefile

I want to test if file contain a pattern (skipped jasmine tests that should make my build fail, when run from travis). I have code like this: skipped_tests: $(if $(shell grep -E 'iit|ddescribe' ...
3
votes
2answers
2k views

How to get the exit code of commands started by find?

I am using "find" in a Travis-CI to check a particular file type with a program. (To be exact, it is a shellcheck check.) However, when using find the exit codes of the command(s)/subshells executed ...
3
votes
1answer
582 views

Bash pipeline's exit status differs in script

The following bash pipeline returns 1: $ false | true $ echo $? 1 However it returns 0 when executed in an script: $ cat test.sh #!/usr/bin/env bash false | true echo $? $ bash test.sh 0 Can ...
3
votes
2answers
85 views

Get specific result from function

Is there a way to return a specific value in an echoing function? return allows me to return an exit status for the function. I need to return a more sophisticated data structure such as an array, or ...
3
votes
1answer
2k views

What does exit code 130 mean for postgres command?

When attempting to start PostgreSQL server using the /usr/pgsql-9.2/bin/postgres command I get an exit value code of 130. However I could not find any documentation for the exit codes of this command....
3
votes
2answers
2k views

Is there a specific reason why iptables would return an exit code of 3 (instead of 1?) when executed without sufficient privileges?

I simply forgot to use sudo: usr@arch ~[0] $ iptables -L iptables v1.4.21: can't initialize iptables table `filter': Permission denied (you must be root) Perhaps iptables or your kernel needs to be ...
3
votes
2answers
3k views

how to detect if command execute failed in shell?

I use Java to call a shell script in remote machine. I want to know if the shell execute failed. But I found that, checking the return code $? after the script is finished doesn't work. $? only ...
3
votes
1answer
137 views

How to determine program success when running a sequence of similar tasks?

I am writing a series of CLI tools that share the same parent command, similar to programs like git. program verb OPTIONS One of the action verbs, install, is designed to git clone as many ...
3
votes
3answers
3k views

Get rsync to report if any local files were updated

I'm running rsync to do pull from a remote host to the local host. rsync -a --update user@remotehost:/files/ ~/files/ I'm running that command from java and using the exit code to work out if it was ...
3
votes
1answer
1k views

Why does the exit code of a script appear to have a different value than what is returned?

I have a script (run-docker-container.sh) that calls another script (create-docker-container.sh). create-docker-container executes a curl script against the docker remote api and returns the http code ...
3
votes
1answer
795 views

typeset and return status in ksh

I'd like to be able to declare a local var as the result of a command AND to be able to capture return status of command. Something like this: #!/bin/ksh typeset -r command_output=$(command) typeset ...
3
votes
1answer
3k views

Does an init script always return a proper exit code when running status?

I'm trying to write a cron job script that will check if my services are running and restart them if they aren't so I don't have to do it manually. Now, I've looked up online how to check the status ...
3
votes
1answer
1k views

How to get exit code of a parallel process launched by xargs?

I'd made a script for launching parallel rsync process: #! /bin/bash LIST=$1 DEST_DIR=$2 RSYNC_OPTS=$3 #echo "rsyncing From=$SRC_DIR To=$DEST_DIR RSYNC_OPTS=$RSYNC_OPTS" echo $LIST|xargs -n1 -d, echo|...
3
votes
2answers
989 views

systemd status=127 for /usr/bin/install when trying to start MariaDB

I just upgraded my MariaDB installation to 10.2. When I now try to start the service, it tells me it failed an checking the status shows me this rather strange message: root@server # systemctl status ...
3
votes
1answer
1k views

Get return value from `find .. -exec ..` command?

In a bash script, I have a function that does some processing on the argument passed to it. I'd like to set a return value depending on how the processing went. Problem is that the function is called ...
2
votes
4answers
464 views

What exit modes exist in shell-scripting in general and in Bash in particular?

I know that in shell-scripting an "exit" usually means voluntarily or at least successfully terminating a session (or a process within a session) and that there are several different exit modes; the ...
2
votes
2answers
3k views

Log the output of ssh shell and preserve the exit status

I have some script to be executed on remote server(server-2) through ssh from server-1 and I have to write that output into a log file named file.log on server-1. I am trying this: sc.sh echo '...
2
votes
1answer
885 views

Checking to see if rsync was successful

Is it possible to check if an rsync is successful and potentially collect an error if its not? As I am using --link-dest, I think this may be harder to detect. I'm fairly new to bash, so any ...
2
votes
1answer
696 views

Pass through exit status code after using ||

I want to write an alias that's something like: alias baz="false || echo bar" (It's not actually false, it's a command that can fail). However, I want to pass the failed status code through to the ...
2
votes
1answer
303 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 ...
2
votes
3answers
736 views

How to assign the success/fail status of a function to a variable in Bash?

I know that a way to do it is : # run a command here, like [[ $A == *"ABC"* ]] result=$? if (( result == 0 )) then result=true else result=false fi Then I can do : if $result then ... Is there ...
2
votes
2answers
300 views

if statement based on a command

I found this way to do if commands: $if echo test | grep st ; then echo yes ; fi ; echo $? test yes 0 Looks good. Let's try a non-match: $if echo test | grep 123 ; then echo yes ; fi ; echo $? 0 ...
2
votes
1answer
2k views

From a shell script, execute another one with arguments and obtain its return code

On a i686 / 32-bit dual CPU, with a fresh Debian Stretch installation, I've installed Octave 4.2.1 and run ./mytest after providing it with execution privileges: #!/bin/bash ./mytest.m where test.m ...
2
votes
1answer
300 views

Aggregate status code from sub-scripts in a script

I have a bash script like this #!/bin/bash do-this parm someotherparam do-something-else $varz $(commands) and-also-do-this -something finally-do-something-different -yes I want this script to ...
2
votes
2answers
2k views

Execute command and store everything to variable in bash

In bash script I'm developing I'm trying to execute command and capture in variable(s): stdout stderr status code how to achieve that? The command is tar, if it is of any significance. I tried the ...
2
votes
2answers
632 views

Access return code of last command in AWK

Command not found should produce return code 127: $ foo; echo $? bash: foo: command not found... 127 I tried to assign $? to variable rc and then print it, but RC is always 0. $ foo | awk -v rc="$?"...
2
votes
1answer
132 views

Why do bash variable assignments affect the last exit code $? and where is this documented?

I was surprised to find out that setting an environment variable modified the last exist code variable $?: $ false $ echo $? 1 $ false $ FOO=BAR $ echo $? 0 $ The bash manual says only this about $?...
2
votes
1answer
159 views

Command don't return expected value inside .zshrc

Trying to run: function which_terminal { return (ps -p$PPID | awk "'NR==2'" | cut -d "' '" -f 11) } inside .zshrc to get a varible with which terminal emulator is running so I can configure ...
2
votes
1answer
63 views

Redirections and Return Codes

So I have a program that may or may not segfault (./segf) that also writes other data to stderr. When the program segfaults, its return code is 139 (i.e. echo $? will print 139). I would like to ...
2
votes
1answer
286 views

ldd equivalent which fail when library not found

Assume I perform ldd /bin/ls with the pthread library removed. I obtain linux-vdso.so.1 (0x00007ffcc3563000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f87e5459000) libcap.so.2 => /...
2
votes
2answers
403 views

How to detect an error using process substitution

This question is similar to the following link, but focused on using the command line (bash shell). Using a simple example, when doing the following command: $ cat <(date); echo $? Fri Jul 7 21:...
1
vote
4answers
5k views

How to use case statement to deal with multiple return values

Take a look at these attempts: $ case `true` in 0) echo success ;; *) echo fail ;; esac fail $ if `true` ; then > echo "success" > else > echo "fail" > fi success Now, why is the case ...
1
vote
3answers
291 views

Difference between `exit;` and `exit $?;`

Is there any difference between these two commands: exec "$(dirname "$0")/suman-shell"; exit $?; and exec "$(dirname "$0")/suman-shell"; exit; is the $? redundant in the first case?
1
vote
1answer
151 views

tput command doesn't work in if statement

I'm trying to use the tput command in a bash script if statement but for some reason it's behaving unexpectedly. When running the command and then checking the exit code only in the if statement ...
1
vote
1answer
1k views

The meaning of [ $? == 1 ] && return 0

I need to understand this command: [ $? == 1 ] && return 0
1
vote
2answers
2k views

Why do high exit codes on Linux shells (>= 256) not work as expected?

I discovered a strange behaviour (reproducible with zsh and bash on my system): $ # here everything is still normal $ bash -c 'exit 1';echo $? 1 $ bash -c 'exit 255';echo $? 255 $ zsh -c 'exit 255';...
1
vote
3answers
720 views

Remote Command executed via ssh does not return proper return code. Return error code is always “0”, even condition failed

I am not getting proper exit return code during execution of remote script on a Linux machine. This script is intended for checking whether a user exists or not. My Script is below :- #!/bin/bash ...
1
vote
3answers
3k views

Return an exit code without killing caller's terminal?

I'm having a hard time getting the return value from a script. The script that is giving me trouble is second.sh below. But there's an interaction present with a script called first.sh, and that ...
1
vote
1answer
92 views

Exit status of chained commands

I want to run a potentially invalid command in a variable cmd. I can't afford to have it just fail me and retrieve the exit status. What I can try to do is pipe it to : and then I'll have a successful ...