Category Archives: Linux

BASH Exit Codes

I’ve been learning a lot about BASH lately and am working on re-writing my Base32 Decoder and HMAC scripts before releasing the full OTP script.

One of the topics that helps with signalling and control flow between BASH functions is exit codes. Understanding the results of calling exit and the exit code that is stored in $?.

#!/bin/bash

function myFunction {
   echo $1
   exit $1
}

# Call 1
var=$(myFunction 1)
echo "Exit 1: $?"

# Call 2
(myFunction 2)
echo "Exit 2: $?"

# Call 3
(( var += 1 ))
echo "Exit 3: $?"

# Call 4
(( 1 / 0 ))
echo "Exit 4: $?"

# Call 5
myFunction 5
echo 'The last line.'

Call 1 captures the stdout of myFunction in var, captures the exit code, then prints the exit code of myFunction,.

Call 2 prints directly to stdout, captures the exit code, and prints the exit code of myFunction.

Call 3, the exit code of the arithmetic operation is printed.

Call 4, shows that a failed arithmetic operation returns a non-zero exit code. Note: Most operations will succeed, even if invalid parameters are given.

Call 5 shows that when a directly called function (i.e. not in a subshell) exits, it causes the whole script or function to exit (i.e. the last line is never executed).

Running the script produces the following output:

Exit 1: 1
2
Exit 2: 2
Exit 3: 0
./test.sh: line 21: ((: 1 / 0 : division by 0 (error token is " ")
Exit 4: 1
5

Bypassing Basic Download Filters

The other day, I was trying to update the driver for my EVGA GeForce GTX460 Video Card, but the download kept stopping at 180MB out of 271MB. After re-starting the download a few times, realized that my ISP or firewall was flagging something malicious and interrupting the download. I figured I had 4 options:

  1. Download the driver on my server, and then download it to my PC using SFTP.
  2. Download it over a VPN. Being limited to 300kb/s bandwidth on the free ProXPN VPN didn’t seem like a good option either.
  3. Download it over public Wi-Fi. Just annoying.
  4. Download it over https.

Assuming it worked, I figured option 4 would be the easiest. The default download link too me to http://us.download.nvidia.com/Windows.... By changing the link to https://us.download.nvidia.com/Windows..., I received a certificate mismatch message, but the download worked when I overrode the warning. A closer look at the warning, shows the https certificate is for the domain name a248.e.akamai.net. This seems safe to me, since Akamai is a CDN, so it makes sense that NVIDIA could be using Akamai to distribute their drivers. I would be more cautious if the certificate was not issued to a CDN or NVIDIA itself. Remember to check that the installer was signed by the appropriate company (NVIDIA in this case).
Cert Error

I haven’t tried this on other downloads, but I think it should work on a fair percentage of them.