Questions tagged [error-handling]

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

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 ...
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 ...
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 ...
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 ...
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 ...
0
votes
1answer
197 views

apt vs apt-get error code

Strange thing I have here, when apt is locked by something. Respectively, the error codes don't match up. apt update seems to always return success 0, whereas apt-get update returns a proper error ...
30
votes
7answers
216k 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 ...
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* &&...
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 ...
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 ...
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 ...
4
votes
0answers
175 views

Repeatably generate a read error for testing? [duplicate]

I'm trying to test the error handling in some software, in particular what happens when an error occurs reading from a file or pipe. Is there a simple way to send a certain amount of data to stdout ...
2
votes
1answer
2k views

how to locally redefine 'command_not_found_handle'?

I'd like to make a particular bash script failfast when it cannot find a command, while retaining globally the usual friendly command_not_found behavior. E.g., if I save the following to /tmp/foo.sh, ....
1
vote
2answers
83 views

Move directories despite of errors

When moving a directory to another drive, if there are errors in the process, nothing will be deleted. I am getting this kind of errors: mv: cannot stat ‘originaldirectory/longpath/irrelevantfile’: ...
0
votes
1answer
595 views

error: ‘TCP_NODELAY’ was not declared in this scope [closed]

I am trying to compile ChatScipt v7.55 in Ubuntu 16.04. But when I use make server command, I get this error: evserver.cpp: In function ‘int settcpnodelay(int)’: evserver.cpp:263:40: error: ‘...
0
votes
0answers
1k views

collect2: error: ld returned 1 exit status Makefile:107: recipe for target ‘binary’ failed make: *** [binary] Error 1

I am trying to compile ChatScript V7.55 on Ubuntu 16.04 but I got this error message: collect2: error: ld returned 1 exit status Makefile:107: recipe for target ‘binary’ failed make: *** [binary] ...
-2
votes
1answer
215 views

How to Do Error-trapping and Swapoff if Error/Warning?

Pseudocode where incomplete pseudo try-catch after cas' comment #!/usr/bin/env bash # http://askubuntu.com/q/799834/25388 # Use: `sh myScript`; sudo does not work with Matlab. #set -e # no ...
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 ...
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
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 -...
3
votes
2answers
718 views

bash 'nounset' on sub-shell/child doesn't force main script to exit, how to workaround that globally?

With this script: tstNounset.sh #!/bin/bash set -x set -o nounset;set -u echo `echo $str` function FUNC1() { echo $str; };export -f FUNC1;bash -c FUNC1 function FUNC2() { set -u;echo $str; };export ...
2
votes
0answers
305 views

wget a zip file from GitHub, OpenSSL error [closed]

$ wget -O sindresorhus-is-online-master.zip https://github.com/sindresorhus/is-online/archive/master.zip --2018-11-03 13:48:20-- https://github.com/sindresorhus/is-online/archive/master.zip Resolving ...
2
votes
1answer
877 views

How to have a bash script perform a specific action on errors of _each_ command?

Similarly to set -e performs an exit on "uncaught" $? != 0, I'd like to have this event trigger the execution of another command, e.g. a logger call. How can this be done?
1
vote
1answer
367 views

Catch unexpected input on single character read in bash [duplicate]

I have a bash script, which, at one point asks the user for confirmation. I do this by reading a single character, which is then transformed to lower case and checked if it is 'y'. If not, the script ...
0
votes
2answers
5k views

Why “syntax error near unexpected token”? [closed]

I have about 10k (approx 180x50x2) CSV files which I want to join together as following, but the inner for loop fails because of some syntax error; I cannot see the error in lastFile #!/bin/bash dir='...
0
votes
3answers
662 views

Upgrade from fedora 25 to 27 - Conflict issue

I tried to upgrade from fedora 25 to fedora 27. When running sudo dnf system-upgrade download --releasever=27 --allowerasing I encountered a transaction check error regarding python. The error ...