Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
2 answers
38 views

How to capture exit code of subshell (while in pipe)

somefile contains this: aaa bbb ccc This is the subshell which can exit before completing all iterations. In my particular case is while but it could be for or until: cat "somefile" | while ...
Smeterlink's user avatar
0 votes
1 answer
47 views

Evaluating exit code of pipe or fifo

So i have something along the lines of: set -eo pipefail ssh localhost "ls; exit 1" | cat > output I thought pipefail would prevent cat to write to ouput (because of the exit code from ...
PKSWE's user avatar
  • 103
1 vote
2 answers
53 views

Ensuring Distinguishable Exit Codes for Shell Scripts

Say I have two shell scripts, a.sh and b.sh, where a.sh calls b.sh somewhere inside. Then, say that b.sh could exit with exit codes 0, 1, or 2. What are some techniques that can be employed to ensure ...
Pacopenguin's user avatar
2 votes
1 answer
104 views

Detect if the previous line was a command or not

I'd like to enable Windows terminal shell integration in bash. (Don't judge me.) One nice feature is that the terminal can show a mark in the scrollbar for each command, with a color depending on ...
N.I.'s user avatar
  • 230
0 votes
1 answer
65 views

Does there exist a tar implementation that exits 0 when an error occurs?

Vim runtime commit 3d37231437fc0f761664a7cabc8f7b927b468767 tries to fix a tar-editing plugin by removing the assumption that a filename doesn't contain the words warning, error, inappropriate or ...
D. Ben Knoble's user avatar
1 vote
1 answer
506 views

zsh: check exit code of pipeline of commands

I have long pipeline of commands in zsh script: pv /dev/sda > sda.raw | sha256sum > sda.raw.sha256 | cut -c61-64 | read SHASUM how can I check inside an if statement, that all commands exited ...
Martin Vegter's user avatar
0 votes
1 answer
121 views

Calling a script from within csh with output redirection

What I would want: #!/bin/csh # call /myscriptpath/myscript.any and redirect 2>&1 and get retval and output set my_msg = `/myscriptpath/myscript.any` set my_ret = `echo $?` How to capture the ...
paulj's user avatar
  • 238
3 votes
2 answers
123 views

How and why does using redirection or writing files within an if statement affect exit code?

In zsh echo 'a string' > test.txt echo $? 0 and [[ $(echo 'a string') ]] echo $? 0 whereas [[ $(echo 'a string' > test.txt) ]] echo $? 1 another example curl -so 'curl-8.2.1.tar.gz' https://...
pming's user avatar
  • 33
0 votes
3 answers
601 views

Does `return 0` equal `true` (in sourced script to shell's environment)?

I am working on a highly portable script that users shall source to their shells, forcing me to use POSIX scripting. There are many useful functions in the script, one of them is special though, as ...
Vlastimil Burián's user avatar
6 votes
2 answers
689 views

Why does `trap` passthough zero instead of the signal the process was killed with?

Consider the following: #!/bin/bash trap 'echo $?' INT kill -INT $$ Output: 0 Here I would expect 130 for my system. Of course, if I do a Ctrl + C then I get 130. The same thing happens for any ...
Elliot Killick's user avatar
0 votes
1 answer
530 views

Negate exit status of process

In a project, I have a test setup where I read the exit status $? of a command. In my test, I want to ensure that the string foobar is not present in the output. When I run the command: ./program | ...
user388557's user avatar
0 votes
2 answers
937 views

Get exit status of the first command in a pipeline *in the second command*

My question is similar to Get exit status of process that's piped to another, but I want to get the exit status of the first command in the pipe within the pipe. Here's an example: false | echo &...
MilkyDeveloper's user avatar
-1 votes
2 answers
1k views

systemctl can't find executables while root can?

Strange thing: I've created a service, that should start a springboot fat archive webserver. Service: #!/bin/bash [Unit] Description=JalouWeb [Service] Type=simple WorkingDirectory=/opt/jaloucontrol ...
fbstov's user avatar
  • 9
1 vote
1 answer
481 views

Getting first line of piped output sets exist status 141 which fails bash script with set -eo pipefail

I have a bash script that runs a command from which I want to get only the first line of output. The obvious answer is some_command | head -1, right? Well, this works 99% of the time, but when the ...
EM0's user avatar
  • 486
0 votes
1 answer
1k views

Make systemd treat unexpected exit as failure

I'm wrapping a 3rd party executable in a systemd service unit to manage it. I can't alter the behavior of this program and I don't really trust its exit codes. I would like to treat any exit that ...
davolfman's user avatar
  • 692
2 votes
1 answer
268 views

Using set -e (errexit) with block of commands and executing another on fail of that block (SC2181)

I just found out about set -e as I was searching for an answer to "how to run many commands (without &&) and immediately stop on non-zero exit code of any of them?". The set -e was ...
Andrew15_5's user avatar
11 votes
1 answer
988 views

Why does the "dc" command exit(1) when using q(uit)?

$ echo "$?" 0 $ echo 'q' | dc || echo "$?" 1 Why is using q to quit dc considered an error? (The test was run on Ubuntu 20.4.)
Ray Butterworth's user avatar
4 votes
2 answers
2k views

How to print the failed command that caused the script's failure?

I'm using the -e flag. Usage: #!/bin/bash -e Explained: -e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status When a command in the script fails, the ...
AlikElzin-kilaka's user avatar
1 vote
1 answer
613 views

How might I only write stdout to file if exit code is 0?

The sponge command "soak[s] up standard input and write[s] to a file", which lets us pipe from and to the same file. I want to do something similar, where I pipe to a sponge-like utility, ...
billkw's user avatar
  • 123
1 vote
1 answer
2k views

error handling in a bash subshell

I'm writing some bash code, and I'd like the script to exit if there's any error. The set -e trick works pretty well, but not with subshells. Here's a simplified example: set -e chmod a=r file.txt x=`...
jyoung's user avatar
  • 121
1 vote
2 answers
352 views

How to make a script fail when there is an error in here string?

I have a script similar to this: #!/bin/bash set -euo pipefail IFS=$'\n\t' while read -r l; do echo "${l}" done <<< "$(cat input.txt)" echo Success The command cat ...
Thomas Leplus's user avatar
2 votes
1 answer
925 views

How to suppress the impact of 'File system loop detected' warnings on the exit status of the 'find' command?

I use find command to find all regular files older than some number of minutes in a directory tree containing some symbolic links that need to be resolved: find -L . -type f -mmin +300 -print But ...
Ivan Olshansky's user avatar
0 votes
2 answers
86 views

Bash IF statement is not behaving as expected

I cannot understand the logic of what's going on with this simple Bash script: #!/bin/bash # Check if file is present which ./vendor/bin/non_existent_file &> /dev/null printf "Exited ...
Chuck Le Butt's user avatar
2 votes
1 answer
334 views

I want to understand the intuition of the output

bash-3.2$ echo foo > foo.txt bash-3.2$ if cmp foo.txt foo.txt; then echo EQUAL; fi EQUAL bash-3.2$ echo bar > bar.txt bash-3.2$ if cmp foo.txt bar.txt; then echo EQUAL; fi foo.txt bar.txt differ:...
Niklas Rosencrantz's user avatar
1 vote
1 answer
349 views

test returns wrong value (but no errors) depending on the presence of spaces around the operator

If there are spaces around the operator, test returns the correct value of the expression. But if there are no spaces, it doesn't throw any syntax error and always return true. $ test "A" == ...
robertspierre's user avatar
2 votes
1 answer
103 views

Return value inside unbuffer command

I have the following bash code to capture the result of a build while printing the output to the screen and provide parsing of the build results. output=$(unbuffer cmake ... | tee /dev/tty) ...
Coder909's user avatar
1 vote
2 answers
3k views

Shell script exit codes

I have a question about bash exit codes. Can I basically define what each code I return means? If so is there some standard way of defining or documenting that? I've been looking into it and I see ...
doubleopinter's user avatar
0 votes
2 answers
436 views

Can sed execute a command on empty file (quit command)

I have a shell script in which I want to validate the content of a file. The expected content should have a specific first line, say ^MYFIRSTROW, and not be empty. So I wrote this check using GNU sed :...
PlasmaBinturong's user avatar
8 votes
2 answers
960 views

Is there a pipefail equivalent for dash?

I have a perl script that runs a system() call to execute shell commands and I would like to run more than one command and pipe data between them. Something like (in perl): system("command1 | ...
terdon's user avatar
  • 245k
0 votes
0 answers
2k views

collect 2: error: ld returned 1 exit status when compling

I'm trying to install xschem on a remote computer (centos 7). I ran ./configure and it was successful (i.e. no missing packages) However when I'm trying to run make in the same directory I get: /cs/...
engineer1155's user avatar
0 votes
1 answer
714 views

What does systemd exit code EXIT_FDS mean?

I get this message from systemd status after I have stopped my service: Actice: failed (Result: exit-code) <...> Main PID: 4747 (code=exited, status=202/FDS) Status FDS is defined in the docs ...
Lii's user avatar
  • 177
14 votes
3 answers
19k views

Print exit status code after each command in terminal

I would like to print at every lauched command in zsh exit code value. E.g. $ cat file_not_present cat: file_not_present: No such file or directory Exit code 1 I only know that I can print error code ...
lecciovich95's user avatar
4 votes
1 answer
38k views

Systemctl service failed Exit-code

My systemctl Code dont work ● <appname>.service Loaded: loaded (/etc/systemd/system/<appname>.service; disabled; vendor prese Active: failed (Result: exit-code) since Mon 2022-04-04 ...
Rotesass's user avatar
0 votes
1 answer
428 views

grep: if input file is also the output, then why clearing the input file? [duplicate]

Scenario: $ cat t0.txt xxx $ grep xxx t0.txt > t0.txt grep: t0.txt: input file is also the output # exit status 2 $ cat t0.txt <nothing> Question: if input file is also the output AND the ...
pmor's user avatar
  • 629
2 votes
1 answer
1k views

Bash named pipes, parallel commands and exit status

I'm trying to use a named pipe so I can keep stdout for status output from a long process. I could use stderr for status output, but I'd like to keep that for errors. Here is an example: #!/bin/bash ...
A. Que's user avatar
  • 653
0 votes
2 answers
3k views

Exit bash script if a command succeeds

I have some simple smoke test scripts that generally look like: set -e run-command run-other-command echo ok This works great when I expect commands to succeed; if any fails, the set -e will exit ...
jmou's user avatar
  • 103
0 votes
0 answers
262 views

Why does this conditional statement, based on exit code, work in the reverse of what is expected? [duplicate]

The script is test.sh. #!/bin/sh if cmp -s file.a file.b; then echo diff else echo same fi When checking the exit code from cmp -s I see: When file.a and file.b are different. cmp -s file.a file....
Appleoddity's user avatar
0 votes
2 answers
2k views

Why the Unix command exit with non-zero value in Shell and evaluates to True when used in bash if condition?

The command patch -R -p0 -s -f --dry-run < abc.patch fails with exit code 1 and prints a error message from patch command but when used with negation in a if condition it passes (i.e prints Pass), ...
Alex Swift's user avatar
0 votes
1 answer
2k views

Reboot and relaunch a script if error

I have a script (not written by me, I cannot modify it) that has to run for days, that sometimes fails (exits with an error). In this case all I have to do is just reboot the server (there is no ...
Basj's user avatar
  • 2,581
1 vote
0 answers
871 views

TAR returns 1 on big directories - how to ignore this?

I'm running the following sequence for backup purposes: $> set -o pipefail $> tar -c /vary/large/folder1 /very/large/folder2 | xz ... | gpg ... | aws ... $> echo "${PIPESTATUS[0]} ${...
manifestor's user avatar
  • 2,493
8 votes
0 answers
1k views

How to tell systemd to start a service after a clean exit of another service

I'm trying to start a systemd. service after another service exits properly (e.g : in a application it could be exit(EXIT_SUCCESS) ). I've seen that there might be an OnSuccess option in the later ...
oneshepherdssheep's user avatar
5 votes
1 answer
2k views

When does inherit_errexit not work?

#!/usr/bin/env bash set -e shopt -s inherit_errexit a=$(cat no-such-file) echo survived $ /tmp/a.sh cat: no-such-file: No such file or directory #!/usr/bin/env bash set -e shopt -s inherit_errexit ...
x-yuri's user avatar
  • 3,473
2 votes
1 answer
333 views

zsh pipestatus disappears in the following prompt

I am on zsh 5.8 (arm-apple-darwin20.2.0). Unlike the way PIPESTATUS in bash is available to read during the next shell prompt, pipestatus of pipe in zsh seems to disappear in the following prompt. ❯ ...
codepoet's user avatar
  • 606
1 vote
0 answers
17 views

Catch the root command exit code from a regular user [duplicate]

I've been researching umount exit codes for some error cases, running the following command repeatedly: $ sudo sh -c "umount /home; echo $?" And got surprisingly strange results. The error ...
tsul's user avatar
  • 111
3 votes
1 answer
2k views

pdsh and command exit code

I would like to use pdsh to assert that a command successfully run on multiple nodes, if the command fails on any nodes the exit code (from pdsh) should be non-zero. Consider the following examples: $ ...
yellowhat's user avatar
  • 133
3 votes
1 answer
4k views

Exit sh script if condition is met

Maybe this is a rookie question, but I am fairly new to sh scripting and I have to wait about two hours to make sure my logic is right, so I just wanted to ask here. I have a sh-script which should ...
kekw's user avatar
  • 143
10 votes
1 answer
903 views

Why 'let' exits with code 1 when calculation result equals to 0?

I came across this question whose author had dealt with problem caused by: let x=1-1 exits with code 1. According to bash manual: If the last expression evaluates to 0, let returns 1; otherwise 0 is ...
Alexander Blazhkov's user avatar
2 votes
1 answer
403 views

Difference between if [ ... and test ... statement in bash

Consider the following: echo "hello" > file.txt is_match1 () { local m m=$(cat "file.txt" | grep -F "$1") if [ -z "$m" ]; then return 1 fi } ...
fuumind's user avatar
  • 449
5 votes
2 answers
2k views

BASH: Change exit status in trap

Can I assign a different exit status to a shell script in a trap handler? Through trial-and-error, I found that calling exit in trap the exit status can be changed. Normal commands, whether fail or ...
Zoltan K.'s user avatar
  • 505
1 vote
1 answer
1k views

zsh: How to store the return code of a statement without triggering `set -e`?

Suppose I want to capture the return code of (exit 56) If I use set -e or a zerr handler, I can no longer do: (exit 56) ret=$? If I use (exit 56) || true to avoid set -e, the return code would ...
HappyFace's user avatar
  • 1,640

1
2 3 4 5 6