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 [error-handling]

Use this tag for questions on how to handle errors generated in shell scripts.

75
votes
2answers
22k views

Why is pattern “command || true” useful?

I am currently exploring Debian packages, and I have been reading some code samples. And on every line in, for example, the postinst script is a pattern. some command || true another command || true ...
41
votes
4answers
123k views

How to exit a shell script if one part of it fails?

How can I write a shell script that exits, if one part of it fails? For example, if the following code snippet fails, then the script should exit. n=0 until [ $n -ge 5 ] do gksu *command* &&...
33
votes
3answers
36k views

chmod + silent mode + how force exit code 0 in spite of error

Is it possible execute chmod and ignore error from command Example ( remark file.txt not exsist to show the example ) When I type chmod 777 file.txt I get error on the output chmod: cannot ...
30
votes
7answers
215k views

How to close X Server, to avoid errors while updating nVidia driver?

I tried to update my nVidia driver but I got an error when I ran the driver installation. See the error: ERROR: You appear to be running an X server; please exit X before ...
25
votes
5answers
41k views

Trap, ERR, and echoing the error line

I'm trying to create some error reporting using a Trap to call a function on all errors: Trap "_func" ERR Is it possible to get what line the ERR signal was sent from? The shell is bash. If I do ...
24
votes
5answers
19k views

Prevent grep from exiting in case of nomatch

This script does not echo "after": #!/bin/bash -e echo "before" echo "anything" | grep e # it would if I searched for 'y' instead echo "after" exit It also would if I removed the -e option on the ...
23
votes
2answers
101k views

Where can I find a list of 'make' error codes?

I am trying to compile a program written in Fortran using make (I have a Makefile and, while in the directory containing the Makefile, I type the command $ make target, where "target" is a system-...
23
votes
2answers
83k views

How to check whether a command such as curl completed without error?

I am using curl to upload a file to a server via an HTTP post. curl -X POST -d@myfile.txt server-URL When I manually execute this command on the command line, I get a response from the server like "...
20
votes
6answers
7k views

How can I suppress output only if the command succeeds?

I would like to simplify the output of a script by suppressing the output of secondary commands that are usually successful. However, using -q on them hides the output when they occasionally fail, so ...
16
votes
2answers
5k views

rsync: skip files for which I don't have permissions

I'm using rsync -rlptD to copy a directory from another user. There are a few files (I have no way of knowing these in advance) which I don't have permission to copy. Is there a way have rsync ignore ...
15
votes
7answers
2k views

How can I check if a file can be created or truncated/overwritten in bash?

The user calls my script with a file path that will be either be created or overwritten at some point in the script, like foo.sh file.txt or foo.sh dir/file.txt. The create-or-overwrite behavior is ...
14
votes
4answers
9k views

How to make bash abort the execution of a script on syntax error?

To be on safe side, I'd like bash abort the execution of a script if it encounters a syntax error. To my surprise, I can't achieve this. (set -e is not enough.) Example: #!/bin/bash # Do exit on ...
13
votes
2answers
12k views

How to trigger error using Trap command

I am using Ubuntu 12.04.2. I am trying to use "trap" command to capture abnormal or error in my shell script but I am also trying to manually trigger "Error" exit. I have tried exit 1, but it won't ...
13
votes
4answers
2k views

How do I capture the exit code / handle errors correctly when using process substitution?

I have a script that parses file names into an array using the following method taken from a Q&A on SO: unset ARGS ARGID="1" while IFS= read -r -d $'\0' FILE; do ARGS[ARGID++]="$FILE" done &...
12
votes
5answers
12k views

Suppress server message in rsync

When I rsync or scp from a particular server I get a rather long message telling me the rules of the server. However when I setup regular pulls, I'd like to be able to suppress those messages in my ...
11
votes
5answers
7k views

Disable cron emails unless there are errors?

How can I only receive emails from cron if there are errors? In the overwhelmingly vast majority of cases, the tasks will run just fine - and I truly do not care about the output. It is only in the ...
11
votes
3answers
5k 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 ...
11
votes
5answers
83k views

How to catch an error in a linux bash script?

I made the following script: # !/bin/bash # OUTPUT-COLORING red='\e[0;31m' green='\e[0;32m' NC='\e[0m' # No Color # FUNCTIONS # directoryExists - Does the directory exist? function directoryExists {...
11
votes
1answer
203 views

How can I test my shell script's file-handling robustness?

I've written a shell script that handles some "regular" filenames, but I've read Why does my shell script choke on whitespace or other special characters? and Why you shouldn't parse the output of ls ...
10
votes
3answers
8k views

Best practice to use $? in bash?

When I read this answer about $? another question comes to mind. Is there any best practice for how to use $? in bash? Let's have a example: We have a linear script and I we would like to know that ...
10
votes
1answer
24k views

What are the standard error codes in Linux?

When I write a code in bash for instance that copies a file, when the file doesn't exist, in the terminal I see an error similar to "file not found", if the script user isn't granted the permission ...
10
votes
3answers
2k views

Is there a way to have a function in my bash script auto run on any command error?

I'm writing a shell script that needs to do a bunch of commands and every command depends on every previous command. If any command fails the entire script should fail and I call an exit function. I ...
10
votes
4answers
985 views

Silent disk errors and reliability of Linux swap

My understanding is that hard drives and SSDs implement some basic error correction inside the drive, and most RAID configurations e.g. mdadm will depend on this to decide when a drive has failed to ...
9
votes
4answers
1k views

Bash program not executed if redirection would fail

In bash, I notice that if a command using redirection would fail, any programs which run prior to that are not run. For example, this program opens the file "a" and writes 50 bytes to file "a". ...
9
votes
3answers
63k views

blk_update_request: I/O error, dev fd0, sector 0

I recently started noticing some blk_update_request: I/O error, dev fd0, sector 0 errors on my second computer running Arch Linux that I use as a server. This began when I had to reboot the computer ...
8
votes
1answer
423 views

What is Linux for VMS DCL “ON ERROR”?

Using VMS DCL command scripts one can catch errors by using the on error command: $ on error then goto MyErrorHandler $ directory samiam.txt $ delete x.txt $ exit $MyErrorHandler: $ print "Something."...
7
votes
3answers
3k views

why bash increment: `n=0;((n++));` return error? [duplicate]

n=0; ((n++));echo "ret=$?;n=$n;" ((n++));echo "ret=$?;n=$n;" ((n++));echo "ret=$?;n=$n;" from n=1 on, ((n++)) works correctly, only when n=0, ((n++)) return error, and I am using a trap '' ERR that ...
7
votes
2answers
1k views

Making sure a command has executed in bash

I have a bash file with commands to merge pcap files. I want to know if there is anyway to make sure a line in bash runs or not. Like in windows bat file we have %ErrorLevel% ¨ Say a command touch -r ...
7
votes
4answers
595 views

Which data should be excluded from log files , error messages etc. if posted online?

What data should one erase from log files, error messages etc., if posting them as a screenshot so as not to expose sensitive information? To narrow down my question: What should a Linux User be ...
7
votes
2answers
24k views

How to use $? and test to check function?

#!/bin/sh function checkExit(){ if test "$?" != "0"; then echo Command $1 exited with abnormal status exit 1; else echo $? fi } function echoThenRun () { # echo and then run ...
7
votes
2answers
14k views

What does ESRCH mean?

Out of: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html It's always pretty clear why an errno code is named a particular way except for this one. How does SRCH relate to No such ...
7
votes
1answer
2k views

Bash capture any error like -e but don't exit, do something else

I want to set a flag in a shell script (bash) so that if anything returns non-zero, a flag is set (i.e. set a variable like errors="True"). So far, I've thought of calling the script with ...
6
votes
3answers
814 views

How can I output a command to a file, without getting a blank file on error?

I'm trying to run a command, write that to a file, and then I'm using that file for something else. The gist of what I need is: myAPICommand.exe parameters > myFile.txt The problem is that ...
6
votes
2answers
9k views

What does this hard disk error message mean: “Current Pending Sector Count …”?

I grabbed this information from Disk Utillity on Linux Mint: Current Pending Sector Count: Normalized: 200 Worst: 200 Threshold: 0 Value: 22 sectors What does it mean? Is it possible to ...
6
votes
1answer
2k views

“set -e” does not terminate script when error occurs in conditional

The following script has a syntax error or error of some sort: #!/usr/bin/env bash set -euo pipefail if [ ! -f /custom.log]; then echo "test" fi abcxyz The script fails with output of: ./test.sh:...
6
votes
2answers
1k views

How to implement logwatch functionality on top of systemd journal?

Logwatch scans logfiles with regular intervals, collects errors and stuff, then mails it to the admin. This was back in syslog and logfiles days. What's the corresponding solution for systemd's ...
6
votes
1answer
2k views

bash: Accessing function call stack in trap function

Working on a bash function call stack trace... Script traps errors and runs a callStack() function. But on trapping, It always shows a call stack for the callStack() function itself instead of the ...
6
votes
2answers
3k views

How does Linux md-RAID handle disk read errors?

There are 2 cases: the read command times out at kernel level (30 seconds by default), the drive reports its unability to read a given sector before the kernel lose patience (the case I'm ...
6
votes
0answers
7k views

Proper usage of EXIT & ERR traps in bash scripts [closed]

I'm working on a bash script and as I've been going I've learned about traps, signals, function return codes and other such features I've not previously used. I may be thinking about things ...
5
votes
3answers
721 views

How to get further information about the origin of an exit code?

Sometimes I need to maintain programs that invoke shell scripts which invokes other programs and scripts. Therefore, when the main shell script ends with exit code 126, it is a struggle to find out ...
5
votes
1answer
9k views

Bash 'unbound variable' error when variables are referenced in multiline comment (set -u) [duplicate]

I have an unexpected behaviour- following snippet fail with 'unbound variable' error: #!/bin/bash set -u <<EOF a=a b=$a EOF Tested on: GNU bash, version 4.4.12(1)-release GNU bash, version 4....
5
votes
1answer
1k views

Getting gcc piped-to-less to properly show colors on the terminal

I'm compiling a file with gcc (actually I'm make'ing, but never mind), which sends some colorized error messages to stderr. So, I do: gcc a.c 2>&1 | less but what I get is lines such as: a.c:...
5
votes
3answers
1k views

Crontab job not emailing on failure

I have a cron job on Debian: 1 * * * * /home/paradroid/bin/myscript.sh >/dev/null There is an installed and configured MTA, and I have received emails when the script has a syntax error, so I ...
5
votes
4answers
28k views

Can I fix bad blocks on my hard disk with a single command?

I'm working on fixing the errors in my Hard Disk, It's possible to do that manually but it might take hours. After typing this command sudo badblocks -b 512 /dev/sda, I got hundreds of results and I ...
5
votes
2answers
4k views

Why to set errno to zero at the time of initialization of the program and can't test it before error occurred? [closed]

I am reading the material available on errno variable of <errno.h> header file. I read : " A value of 0 indicates that there is no error in the program." and "As a good practice, developer ...
5
votes
2answers
5k views

Is it possible to get the error message from previous command which failed when the conditional command runs using ||

I have a helper function: function error_exit { /opt/aws/bin/cfn-signal ... exit 1 } This helper function is used to signal an error. Here is an example of usage: /opt/aws/bin/cfn-init -s .....
4
votes
4answers
6k views

Understand error codes in Linux

I am working on Linux (Kernel Version 2.6.32.28) laptop. After I inserted/did file io/removed a SD combo card, I got following errors: mmcblk0: error -123 sending status command mmcblk0: error -...
4
votes
2answers
3k views

Simultaneously check for empty output and successful exit status

I'd like to write the following test in an installer script1: if [ -n "`/etc/grub.d/30_os-prober`" ]; then install_dual_boot else install_linux_only fi However, it's also possible that 30_os-...
4
votes
2answers
6k views

How to Resolve the Insecure warning in Swapon?

I do #!/usr/bin/env bash # http://askubuntu.com/q/799834/25388 # http://stackoverflow.com/a/69808/54964 set -e SWAP_FILE="/media/masi/SamiSwapVirtual/.swap_file_20.7.2016" SIZE_MB=16000 dd if=...
4
votes
2answers
1k views

Suppress all output in yum except for errors

In yum when you do an update with yum update you normally get all success and error messages output on the CLI. Is there any way to have yum suppress all successful package installations and only ...