Skip to content
Advertisement

Change exit code on a single line using Bash?

I am using the savscan command but this returns 3 instead of 1 when a malware is detected, and I need to get 1 if a malware is detected, I tried the following:

$ bash -c "savscan -f -archive infectedfile.exe && if [ $? -eq 3 ]; then exit 1 ; fi"
$ echo $?
$ 0 
$ bash -c "savscan -f -archive infectedfile.exe ; if [ $? -eq 3 ]; then exit 1 ; fi"
$ echo $?
$ 0 

but I still get the exit code 0, I also need to run everything in one line

Advertisement

Answer

Personally, I’d use a function wrapper for this:

savscan() {
  local retval
  command savscan "$@"; retval=$?
  (( retval == 3 )) && retval=1
  return "$retval"
}

savscan -f -archive infectedfile.exe

…as adding more rules about how to mutate your exit status is as simple as adding additional commands inspecting and modifying retval, as you see fit.

If you for some reason insist on defining and invoking this function in a single line, this could look like:

savscan() { local retval; command savscan "$@"; retval=$?; (( retval == 3 )) && retval=1; return "$retval"; }; savscan -f -archive infectedfile.exe
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement