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

learn more… | top users | synonyms

2
votes
3answers
24 views

How to catch and handle nonzero exit status within a Bash function?

Say I have the following (pointless) Bash function: myfunc() { ls failfailfail uptime } I run it as follows: myfunc || echo "Something is wrong." What I want to happen is ls runs (as ...
8
votes
4answers
524 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". ...
4
votes
1answer
70 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 ...
1
vote
1answer
44 views

trap errors in bash scripts

I have several questions I want to understand in regards to error trap. why does the code below execute only 1 trap and not 2 and specifically only the code with "mydir2" and not "mydir1"? why at ...
-2
votes
2answers
59 views

Print out a log file with error from a cp command

I am writing an sh file I have this command cp test/*.txt NewDirectory/ from my understanding it will go one by one in the test folder 1.txt 2.txt 5.txt 6.txt but won't copy 3.txt 4.txt ...
0
votes
0answers
18 views

Redirect stderr to stdout and tee to file and check return value [duplicate]

I know how to redirect stderr and stdout to a file: curl -sSo file http://foo.bar >> log 2>&1 I know how to get stdout to appear on the console and in a file: curl -sSo file ...
1
vote
1answer
59 views

Catch unexpected input on single character read in bash

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
1answer
307 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 ...
2
votes
1answer
39 views

Error exit script from within command substitution

If I have a function: myfunction() { [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; } global_var=somevalue } And I call it from within another ...
19
votes
6answers
1k 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 ...
1
vote
1answer
206 views

Bash script to select files to zip/unzip

I'm trying to zip and unzip files with a bash script, but I'm new and I have problems. I will explain what the script currently does, and then what I wanted it to do. User selects “compress”: a file ...
2
votes
1answer
28 views

how to differentiate error returned while reading and for EOF in shell script

I have this task of uploading a delimited file and processing it. Once the processing is done, i either say its successful and if parsing fails, i need to throw the error. I'm reading this file line ...
0
votes
0answers
18 views

redirect all errors of my script into errors.txt [duplicate]

I need to redirect all errors into other file which called errors.txt. This is my script shell : data_in=$1 fichier=$2 cd $data_in #Purge des fichiers rm -f Split/INIT_ENC.dat rm -f ...
0
votes
0answers
13 views

Labeling error messages

When dealing with errors, utilities often print their name followed by the error message, for example, sed, cat, awk, etc. sed: foo: No such file or directory Others print the word fatal, followed ...
0
votes
2answers
375 views

Debian live iwlwifi firmware error

When booting Debian live onto my computer, I encountered the errors: [ 19.456064] iwlwifi 0000:03:00.0: firmware: failed to load iwlwifi-7265-9.ucode (-2) [ 19.456183] iwlwifi 0000:03:00.0: ...
1
vote
2answers
149 views

What's the most efficient way to log crontab shell script errors with error occurred time?

A common way to log crontab task errors looks like below: 1 */8 * * * sh /pseudo_path/test.sh 2>> my_err_log It's a concise command, but it can't record the error occurred time, and the path ...
3
votes
2answers
120 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 ...
0
votes
2answers
6k views

apt-get update failed to fetch and /sources.list.d/ empty

Running the sudo apt-get update command, I got the following error: Fetched 988 kB in 16s (59,3 kB/s) W: Failed to fetch ...
0
votes
1answer
28 views

Warning message after updating

I have Linux Mint 17 Cinnamon edition and after updating using the terminal: sudo apt-get && sudo apt-get upgrade I got this warning message: ... Building dependency tree Reading ...
1
vote
2answers
868 views

Capture errors in a SFTP script

I have a sub sh file which puts receives an input of local directory and path to the remote directory and sends the file to the remote directory. If the remote path is invalid the error cannot be ...
1
vote
1answer
42 views

Print nth pipeline command in bash

In bash, the PIPESTATUS array holds the return values of commands in a pipeline. Suppose that in the pipeline cmd1 | cmd2 | cmd3, the second command returns an error. Checking PIPESTATUS gives the ...
1
vote
1answer
1k views

Shell script `rm -r`: Redirect write-protected prompt and answer “n”

I have a few lines in a shell script whose function is to iterate through directories, match some names, and delete them. Some areas of the file system are write protected and I would like to leave ...
-1
votes
2answers
312 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 ...
4
votes
2answers
226 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 ...
1
vote
0answers
875 views

Repeated error : #error “Only <glib.h> can be included directly.”

I have a research on author attribution compression distance. And I should use "Complearn" ,which is a compression program, in my work. But in the beginning I have problem in installing this program. ...
1
vote
1answer
450 views

open() return new file descriptor posix [closed]

I have to set the return values for open() in posix. How do I return "the new file descriptor" as it is said in the Linux man pages: RETURN VALUE open(), openat(), and creat() return the new ...
2
votes
1answer
782 views

How to get non-zero exit code from Puppet when configuration cannot be applied?

I've tried the following Puppet configuration to disable the root password: class users { user { 'root': password => '*', } } But after applying it with no warnings or error messages I ...
2
votes
4answers
420 views

Mute system error message in a script file and display mine

I have created a simple script to move all the files with .sh suffix in the current working directory to a designated directory If there was no such file, the script should output only an error ...
3
votes
1answer
303 views

How to make dd fail if it invalid skip value is specified?

When I invoke dd with an invalid value for skip it prints an error message and does nothing, but doesn't return with a value different from 0, e.g. $ cd /tmp $ mkdir xy $ cd xy $ truncate -s 2M ...
4
votes
2answers
215 views

Shell script error handling while assigning STDOUT to variable

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash? My script looks like this: #!/bin/bash try() { "$@" code=$? ...
1
vote
1answer
378 views

TCL script try catch

I have a TCL script that sets a number of variables and reads several files. The last file it reads may contain sensitive information, so I need to make sure this file gets deleted even if an error ...
4
votes
3answers
517 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 ...
3
votes
1answer
243 views

Errtrace and local

I am using the following script: #!/bin/bash -Eu trap 'echo Hi' ERR exit_failure() { echo "Hello, World!" return 1 } sub_failure() { res=$(exit_failure) } sub_failure It results in the ...
3
votes
2answers
350 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 ...
0
votes
1answer
346 views

Using trap with ERR: How do I send $?

In bash 4.x If I have: err_handler() { echo You received error $1|mail -s "script error" MAILTO } trap err_handler ERR how do I send the actual stderr output to the function when the trap is ...
8
votes
3answers
499 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 ...
0
votes
2answers
1k views

Stopping .bashrc without exiting shell

My .bashrc file is modularized and I've got some error checking in it. In bash scripts, I typically have a function _errexit (or something similar) that I call when I detect an error and want to exit. ...
4
votes
0answers
4k 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 ...
3
votes
1answer
594 views

Strange error (?) when I run `man` command from a folder that no longer exists

Let's have a look at the followings: radu@Radu:~$ mkdir test radu@Radu:~$ cd test radu@Radu:~/test$ rmdir ~/test radu@Radu:~/test$ man ls man: can't change directory to '': No such file or directory ...
5
votes
2answers
15k 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 ...
7
votes
2answers
600 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 ...
12
votes
3answers
7k 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 ...
5
votes
2answers
651 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 ...
2
votes
1answer
454 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?
5
votes
2answers
2k 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 ...
5
votes
1answer
940 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 ...
1
vote
1answer
433 views

Unwanted messages every time I attempt to login?

I am using Linux Mint Olivia and it worked fine for past couple of months but from yesterday it keep showing these message whenever I want to login to my PC: Keys: Press S to skip mounting or M for ...
2
votes
2answers
90 views

Different error messages when using different strings in terminal

When I insert, for example, unix.stackexchange.com followed by Enter in terminal, I get the following error: unix.stackexchange.com: command not found This is ok and as I expected. But when I insert ...
5
votes
5answers
20k 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 ...
3
votes
2answers
495 views

Getting the complete command line that caused an ERR , while trap

How do I make trap to return the command that caused an ERR? $function err_handler() { echo "$0 caused the error"; } $ trap err_handler ERR $ grep -ci "failed" test4 &>/dev/null -bash caused ...