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.
272
questions
1
vote
2
answers
52
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 ...
2
votes
1
answer
90
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 ...
0
votes
1
answer
61
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 ...
1
vote
1
answer
435
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 ...
0
votes
1
answer
101
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 ...
3
votes
2
answers
120
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://...
0
votes
3
answers
471
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 ...
6
votes
2
answers
681
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 ...
0
votes
1
answer
453
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 | ...
0
votes
2
answers
862
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 &...
-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
...
1
vote
1
answer
415
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 ...
0
votes
1
answer
981
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 ...
2
votes
1
answer
245
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 ...
11
votes
1
answer
983
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.)