I have performed some of these questions (for example, How to set a variable at the output of a command in Bash? ), However, the accepted answers seem to be inoperative for me. I was not sure if I should cancel another question or post my own duplicate, so I apologize if I am wrong here.
I want to get the status of the output and output from several commands in the script that I am collecting. Here is an example of what I used:
cmd_output=$(rm $file) exit_status=$? if [ "${exit_status}" -eq 0 ] then log "Successfully removed the original" ${TAB_LEVEL} else fail "Failed to remove the original, the output was: \n ${cmd_output}" fi
Log and Failure Features:
# Usage: fail "Failure message" function fail { echo "FATAL ERROR: $1" >> "${LOG_DIR}/${LOG_FILE}" exit 1 } # Usage: log "Log message" 3 Where the tab-level is 3. function log { if (("${2}" > 0)) then eval "printf ' %.0s' {1..$2}" >> "${LOG_DIR}/${LOG_FILE}" fi echo "$1" >> "${LOG_DIR}/${LOG_FILE}" return 0 }
In the above example, I use the $ (cmd) format, but I also tried using backlinks.
In my log file, everything that I see when a crash occurs:
FATAL ERROR: Failed to delete the original, the output was: \ n
In addition, the output of unsuccessful commands ends on the screen, as usual. Is there a general reason why my cmd_output variables remain empty?
command-line bash shell ubuntu
Chris o'kelly
source share