Stack Exchange Network

Stack Exchange network consists of 174 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 [return-status]

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

0
votes
2answers
24 views

Repeat command execution attempts until status is not specific, preserve data

I have a case where a first few command execution attempts after daemon restart are returning 2 (followed by stderr messages), than everything works correctly. In order to bypass this bug I've come ...
2
votes
1answer
161 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 ...
5
votes
3answers
136 views

bash script executed over ssh returns incorrect exit code 0

I am trying to automate a process which involves running scripts on various machines via ssh. It is vital to capture both output and the return code (for the detection of errors). Setting the exit ...
4
votes
2answers
76 views

How to run multiple processes and exit if any of them exits or fails

I am running multiple processes and want to exit with the appropriate exit code (this means error on failure, success otherwise) if any of them fails or exists. Additionally if any child process ...
1
vote
1answer
67 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 => /...
0
votes
2answers
310 views

ls error code 2

I was reading the man of ls and in the end it talks about the Exit status of ls. It says: Exit status: 0 if OK, 1 if minor problems (e.g., cannot access subdirectory), 2 if ...
0
votes
1answer
27 views

Allow automated script to incorporate git rebase

I have this in a script: set -e; base="remotes/origin/dev"; git checkout --no-track -b "$new_branch"; git rebase "$base"; on occasion, there are conflicts of course, and what happens is that git ...
0
votes
2answers
48 views

Using shell 'printf' where the format string is in a variable and doesn't have a fixed number of field placeholders?

I am writing a shell function which makes an external API call via cURL (the external API syntax isn't under my control). I've approached it like this (simplified): #!/bin/sh template_get_entry='...
2
votes
1answer
96 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
483 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
3answers
442 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' ...
1
vote
2answers
154 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 ...
1
vote
2answers
34 views

Using the value of a variable as an exit code from a script

I can find plenty of information about how to assign the exit code of a script to av variable, but I want to do the opposite: use the value of a variable (defined within a script) as the exit code of ...
6
votes
2answers
1k views

Why does the exit status differ when running `systemctl start; systemctl is-active` and `systemctl is-active` separately?

The following sequence gives me the return value of the first command, not the 2nd as I would have expected (no matter if I run the 1st command in a subshell): sudo systemctl start x; sudo systemctl ...
0
votes
1answer
408 views

Return value error in bash scripts

I have bash script like this: rm ~/sqoop/"$TABLE"/* rmdir ~/sqoop/"$TABLE" return $? After execution this script it returns a value to next process but even though the script run successfully and ...
1
vote
1answer
61 views

Tracking child processes sequentially

I'm running few commands on a remote server, by sshpass. gnome-terminal --maximize --tab --title='aTask' -e "sshpass -p "afsd@#$5u89f" ssh root@192.168.0.125 'command1 && command2'" \ ...
0
votes
2answers
664 views

ASH : check for command exit code inside if statement

I'm trying to return a 0 exit code if a command exit with the a code 143 (timeout, from the "timeout command"), 1 otherwise. Due to external constraint (CI script), I have to start the command and do ...
1
vote
1answer
329 views

apt-get update failure returns 0 error code [duplicate]

When we run apt-get update and if it fails, a non zero error code should be returned, but in my case its still 0. See below: root@ubuntu-base:/home/user# apt-get update Err http://security.ubuntu.com ...
0
votes
0answers
36 views

Why does the $? variable return 130 when I interrupt a program I wrote? [duplicate]

The situation is this: I run a program that I wrote in C. The program is set to return the value of errno as its return value. I send SIGINT while it's running in while loop that takes text input ...
32
votes
4answers
8k views

What is the min and max values of exit codes in Linux?

What is the min and max values of the following exit codes in Linux: The exit code returned from a binary executable (for example: a C program). The exit code returned from a bash script (when ...
1
vote
3answers
387 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 ...
6
votes
2answers
5k views

return value from eval

The bash manual states: eval [arg ...] The args are read and concatenated together into a single com- mand. This command is then read and executed by the shell, and ...
8
votes
6answers
357 views

Elegant way to construct a pipeline based on return value and not exit code?

When the status code is useless, is there anyway to construct a pipeline based on output from stdout? I'd prefer the answer not address the use-case but the question in the scope of shell scripting. ...
0
votes
1answer
3k views

Script handling systemd service's return code

I'm writing a systemd service which can be started or stopped using systemctl commands. However this program can also return with several return codes and I would like to handle those return codes. ...
0
votes
1answer
32 views

Checking if a command fails - After redirects or before?

We have an SQL script that we execute while redirecting STDOUT and STDERR to two separate files. Due to recent problems with the SQL script, we want to exit the script with an error in the event the ...
8
votes
1answer
390 views

Why does exiting with a stored returncode of a nested command result in different returncodes in Dash and Bash?

Running bash -c 'bash -c "echo test1; exit 1;" &> /tmp/x; buildresult=$?; tail -n 100 /tmp/x; exit $buildresult;' results in test1 being printed to console and echo $? to print 1 which in my ...
1
vote
3answers
191 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?
2
votes
2answers
441 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="$?"...
0
votes
3answers
101 views

Why does finger not have a non-zero return value?

I often use the return/status value of a command in scripts. I'm curious why the finger command doesn't have a return value. E.g., $ finger kevin Login: klang Name: Kevin Kang etc......
3
votes
1answer
434 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 ...
4
votes
1answer
672 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
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 [ $? ...
2
votes
1answer
1k 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 ...
1
vote
1answer
277 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:...
4
votes
4answers
21k views

get output and return value of grep in single operation in bash

I am writing a bash script; I execute a certain command and grep. pfiles $1 2> /dev/null | grep name # $1 Process Id The response will be something like: sockname: AF_INET6 ::ffff:10.10.50....
5
votes
5answers
3k views

Getting “ensure” / “finally” functionality in a shell command (not script)?

I need to know whether a command has succeeded or failed, and unconditionally run some cleanup afterward. Neither of the normal options for executing sequential commands seem to be applicable here: ...
1
vote
1answer
505 views

How to get return value from MATLAB function

I am calling a MATLAB function with a bash script matlab -nodesktop -nosplash -nodisplay -r "matlabfunction input1 input2; quit" I am trying to get a one digit return value from the matlab function, ...
0
votes
1answer
872 views

Exit status of sh command?

Hi I am calling a shell script from inside another shell script and if that script call goes successful I have to proceed further and I am using sh command to call another script like : sh payloadjob....
2
votes
2answers
2k 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
514 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
2answers
805 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 ...
2
votes
1answer
2k views

Can I get the exit code from a sub shell launched with $(command)? [duplicate]

I am setting a variable like this: myvar=$(command --params) I want to check the exit code ($?) of my command afterwards. Checking $? like this always returns 0 because it successfully set the ...
2
votes
1answer
268 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
1answer
189 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 ...
1
vote
1answer
2k views

How to get exit status of a background process?

I am running two mysqlsp in a bash script. I want to start both of them simultaneously (they are running in remote servers) and put them in background, once completed proceed to next step. I want to ...
10
votes
2answers
384 views

Assignments are like commands with an exit status except when there's command substitution?

See the following examples and their outputs in POSIX shells: false;echo $? or false || echo 1: 1 false;foo="bar";echo $? or foo="bar" && echo 0: 0 foo=$(false);echo $? or foo=$(false) || ...
1
vote
2answers
389 views

Trigger a non-zero exit code when I break out of a loop

I'd like to run a series of tasks but stop should any of them fail, hence I've written (something like): for task in [TASKS]; do process "$task" || break commit "$task" done This works fine, but ...
1
vote
4answers
4k 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 ...
11
votes
3answers
3k views

Why do I get different exit status for ps | grep in a script?

I am running below script : #!/bin/bash ps ax | grep -q [v]arnish if [ $? -eq 0 ];then echo varnish is running... exit 0 else echo "Critical : varnish is not running " ...
3
votes
1answer
74 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 > ${...