Tag Info

Hot answers tagged

12

I am not sure what shell sh.exe provides (since there are multiple shells that use that name for their Windows executables), but if it is bash or similar, you can use the $PIPESTATUS array. For your example, you would do: g++ -c source.cpp -o source.o 2>&1 | perl /bin/gSTLFilt.pl echo "${PIPESTATUS[0]}"


11

Shells are built to do that sort of thing easily. if touch -r dcn_file "${dir_dcn}"/"${TAG1_dcn}".pcap then echo "Command ran successfully." else echo "Command had an error: $?" fi You will occasionally find some proprietary command that doesn't exit with 0 status on success and non-zero on failure, but thankfully, those abberations have almost ...


10

The error codes aren't from make: make is reporting the return status of the command that failed. You need to look at the documentation of each command to know what each status value means. Most commands don't bother with distinctions other than 0 = success, anything else = failure. In each of your examples, ./dpp cannot be executed. When this happens, the ...


9

One common way is: die() { echo "$*" 1>&2 exit 1 } then you use it like this: mkdir -p some/path || die "mkdir failed with status $?" Or if you want it to include the exit status, you could change it to: die() { echo "FATAL ERROR: $* (status $?)" 1>&2 exit 1 } and then using it is a bit easier: mkdir -p some/path || ...


9

Bash has an option pipefail: The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit success- fully. So: set -o pipefail && ...


9

The bash variable $? hold the exit status of the last command run. For typical programs, the value 0 is success and any other value is failure. Specific return code should be documented in the man pages of the programs in question. You can use code like: touch -r dcn_file "${dir_dcn}"/"${TAG1_dcn}".pcap [ $? -ne 0 ]; then echo "Command failed: touch: ...


8

This is a broad ranging question and can probably only be answered with the same sorts of brushstrokes. Ultimately, it comes down to what you, the user, wish to protect. Fundamentally, you should not post anything that would allow someone else to find it easier to compromise your system, or any of the other connected tools or accounts you use. For this ...


8

I think you want the trap function, specifically: error_func() { echo 'An error occurred!' exit 1 } trap error_func ERR Errors later will jump to the function. This is supported by at least bash, zsh, and ksh.


7

It means that there are 22 sectors that could not be read. The next time you write to those sectors, if they can not be correctly written to, they will be remapped to a spare sector. You can use the badblocks utility to locate the bad sectors, and dd to write to them: sudo badblocks -b 512 /dev/sda For each sector listed, first verify that it can not be ...


6

Well, there are a couple of cases: This disk is part of a RAID array. Good. Just have md 'repair' the array like this: echo 'repair' > /sys/block/md0/md/sync_action. Problem solved without data loss. (I'm guessing this isn't the case for you, but you really ought to consider changing that.) You don't care about the data on the disk (or there isn't any). ...


6

You could rewrite your code like this: #!/bin/bash function try { "$@" code=$? if [ $code -ne 0 ] then echo "$1 did not work: exit status $code" exit 1 fi } try mkdir -p some/path try cd some/path try run_some_command If you don't actually need to log the error code, but just whether the command succeeded or not, you ...


6

In traditional shells, the status of the first command in a pipeline is not reported at all to the script. Only the status of the last command is available, in $?. In bash ≥3.0, when you want to do is stop if an error occurs anywhere in the pipeline, use the pipefail option. g++ -c source.cpp -o source.o 2>&1 | perl /bin/gSTLFilt.pl More ...


5

There's a simpler way of what you're doing. If you use set -x, the script will automatically echo each line before it's executed. Also, ss soon as you execute another command, $? is replaced with the exit code of that command. You'll have to back it up to a variable if you're going to be doing anything with it other than a quick test-and-forget. The [ is ...


5

I'm assuming your cron email settings are all correct, and you otherwise get emails. Your sending all stdout to /dev/null, so anything that prints error messages must output them to stderr. You might want to make sure everything in the script is outputting correctly. At times I have had to work with third party code, which was sending everything to ...


5

Wrapping the whole into a function seems to do the trick: #!/bin/bash -e main () { readonly a=(1 2) # A syntax error is here: if (( "${a[#]}" == 2 )); then echo ok else echo not ok fi echo status $? echo 'Bad: has not aborted execution on syntax error!' } main "$@" Result: $ ./sh-on-syntax-err $ ...


4

You can redirect the error output to a file and then retrieve that output: trap "rm -f /tmp/cfn-error.txt" 0 1 2 3 15 /opt/aws/bin/cfn-init -s ... 2>/tmp/cfn-error.txt || error_exit $(</tmp/cfn-error.txt) You should always clean up your mess, so don't forget to delete any temp files you create.


4

There are standard error values, defined in errno.h. You can look at this file on your system to see the numerical values. On most systems, they're in /usr/include/errno.h or a file that it includes. On Linux, most are in /usr/include/asm-generic/errno-base.h or /usr/include/asm-generic/errno.h, with a few more in /usr/include/bits/errno.h. If you have a ...


4

From the shell, you can run perror: $ perror 123 OS error code 123: No medium found That comes with MySQL. If you don't have MySQL, you can use Perl or Python, e.g.: $ perl -MPOSIX -e 'print strerror(123)' No medium found $ python -c 'import os; print os.strerror(123)' No medium found In a C program you can use the function with the same name: ...


4

If you really want to exit on an error and are using Bash, then you should also consider set -e. From help set: -e Exit immediately if a command exits with a non-zero status. This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls ...


3

There are two aspects: the ways system calls signal that an error occurred, and the way what error occurred is reported. Most system calls signal that an error occurred by returning -1, but this is not completely universal (for example, some system calls are always successful, e.g. getpid). If you know an error occurred, the error code is always in errno¹. ...


3

The current pending sector count is the number of sectors the disk is currently tracking that it has not been able to read. With luck, they'll be written, and then the disk will remap them. But until it reads them successfully or the computer tries to write to them, there's nothing it can do. This is a possible sign of more problems to come. You can read ...


3

When your PC has more than 4 GB of memory, but has also some devices that support only 32-bit addresses, any I/O from or to these devices must be mapped to somewhere in the low 4 GB range. Typically, a range of 64 MB is allocated for this. "Out of SW-IOMMU space" means that either you are doing so much I/O that you need more than 64 MB of buffers at the ...


3

This is exactly what trap ERR is for. Unfortunately, it has the same limitations and drawbacks as set -e. Like set -e, any command which returns non-zero in a conditional expression will to trigger the trap. Here is some example code: error=0 set_error() { (( error++ )) } trap set_error ERR ls askdjasdaj 2>/dev/null false false || true # false ...


3

As hinted at in this question, the answer is setting up a trap on the ERR signal - basically set -e corresponds to trap 'exit' ERR. So to e.g. log errors but continue execution, use trap 'logger -t myscriptname "Command $BASH_COMMAND exited with code $?"' ERR In bash, the variable $BASH_COMMAND contains the offending command. $? contains the exit code of ...


3

Please advice how to force the chmod command to give exit code 0 in spite of error chmod -f 777 file.txt || : This would execute :, i.e. the null command, if chmod fails. Since the null command does nothing but always succeeds, you would see an exit code of 0.


3

The simplest way is to store the response and compare it: $ response=$(curl -X POST -d@myfile.txt server-URL); $ if [ "Upload successful" == "${response}" ]; then … fi; I haven't tested that. The syntax might be off, but that's the idea. I'm sure there are more sophisticated ways of doing it such as checking curl's exit code or something. update curl ...


3

The difference between man and other commands like ls is that latter ones (those not complaining about non-existent directory) don't try to explicitly change there but already stay there. Man also does, but it additionally tries to explicitly change there, too. UNIX directories (as files) aren't deleted immediately when you call unlink(2) or rmdir(2) on ...


2

The basic rule should be to expose only necessary information. So here the basic security rules apply: Only as much access/information as needed Anything else should be forbidden/not readable As you can see from many questions asked here - comments will ask for further information, if needed. But it is up to you to obfiscate personal information about ...


2

First of all: The logs for apache are set in the httpd.conf file. And the logs for PHP (if any) are set in the php.ini file. For the case of PHP, you have to look at the php.ini file, and look for log_errors and error_log variables, that must have these values: log_errors = On error_log = /tmp/php_error.log the last value (/tmp/php_error.log) is just ...


2

Rsync doesn't have an option for this. I see two solutions. One is to parse rsync error messages; this isn't very robust. The other is to generate a list of unreadable files to filter. cd /source/directory exclude_file=$(mktemp) find . ! -readable -o -type d ! -executable | sed -e 's:^\./:/:' -e 's:[?*\\[]:\\1:g' >>"$exclude_file" rsync -rlptD ...



Only top voted, non community-wiki answers of a minimum length are eligible