Home > Back-end >  convert text to number in case of an error in Bash
convert text to number in case of an error in Bash

Time:03-29

I am running this command which usually returns a number. In cases of error, this parses into text which later on can cause error. How can I prevent this ?

folder_get_time=$((time  /opt/alluxio/bin/alluxio fs copyToLocal "$SRC" "$COPY_TO_LOCAL" )2>&1 >/dev/null )

CodePudding user response:

If you can't reliably trap the error

if folder_get_time=$((time /opt/alluxio/bin/alluxio fs copyToLocal "$SRC" "$COPY_TO_LOCAL" )2>&1 >/dev/null )
then
    echo "success" >&2
else
    rc=$?
    echo "fail" >&2
    exit $rc
fi

maybe check for a non-numeric output.

case $folder_get_time in
  '' | *[!0-9]*) echo "fail" >&2; exit 127 ;;
esac
  • Related