Detecting program errors in bash scripts?

I am trying to write my first semi-advanced bash script that will accept the input as a file name related to the video avi, send it in ffmpegfor conversion to mp4(keeping the original name), and then pass it to MP4Box.

Below is what I am doing ...

#!/usr/bin/bash
ffmpeg -i $0 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre hq -crf 22 -threads 0 ($0).mp4
MP4Box -inter 500 ($0).mp4
  • Is there some kind of / catch attempt that I can make for the first call of the program to make sure that the MP4Box is getting healthy input?
  • Should I even worry about the error? Should I instead rely on the programs themselves to do this for me?
+3
source share
5 answers

&&. ( )

first-cmd && second-cmd

second-cmd , first-cmd true.

, , ||

bad-cmd || echo "failed"

:

good-cmd && echo "succeeded" || echo "failed"

bad-cmd || {
    recovery-cmd || 
    echo "recovery failed && 
    echo "recovered"
} 

, $?, .

/dev/null .

bad-cmd 2>/dev/null               # send error message to the bit bucket
var=$(good-cmd)                   # capture stdout
var=$(bad-cmd 2>&1)               # capture stdout and stderr
var=$(bad-cmd 2>&1 >/dev/null)    # capture only stderr

, .

: stderr ?

+6

, . , $?. , , . RC = $?. 0 . . , , . , . :

ffmpeg <your args>
RC=$?
if [ "${RC}" -ne "0" ]; then
    # Do something to handle the error.
else
    # Everything was ok.
fi
+5

. , . , , - , , , , - , : http://tldp.org/LDP/abs/html/exit-status.html

lskdf      # Unrecognized command.
echo $?    # Non-zero exit status returned because command failed to execute.
+1

-e set -e script. script, - .

, , script, || true .

+1

If ffmpeg is a good citizen, it will return exit code 0 on success and some other value on failure.

"$?" is a special bash construct that reports the return code of the last process executed. After the line ffmpeg you can check "$?". against non-zero values ​​and error output.

0
source

Source: https://habr.com/ru/post/1724848/


All Articles