I’ve updated my BASH Base32 Decoder to be more efficient by using exit codes and bitwise operators (I read the bash man page).
Author Archives: Kenji Yoshino
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
BASH OpenSSL HMAC
I just posted a BASH script that works around the OpenSSL 0.9.8 limitation of using printable ASCII keys for HMAC by moving the key processing into BASH and only utilizing OpenSSL for the hash functions.
BASH Base32 Decoder
I just published a Base32 decoder written in BASH.
This is a piece of a project I’m working on to implement a command line authenticator script as a backup for my Google Authenticator app.
Box Direct Linking
According to Box Help, direct linking to files is only supported for paid accounts; however, I found that it is possible to link directly to a file with a free account. Just create your own direct URL using the following format https://app.box.com/index.php?rm=box_download_shared_file&shared_name=<SHARE_NAME>&file_id=<FILE_ID>
<SHARE_NAME>
is the random alphanumeric string that is part of a share link. For example https://app.box.com/s/sn5mfs7esv0w35ybeoa5
. This standard share link takes you to a page where you can view an online preview of the file and click a download button to get the actual file.
<FILE_ID>
is the numeric identifier for the file which appears at the end of the URL when editing the file. For example https://app.box.com/files/0/f/0/1/f_33678058764
The direct link for this example would be https://app.box.com/index.php?rm=box_download_shared_file&shared_name=sn5mfs7esv0w35ybeoa5&file_id=f_33678058764
Since this isn’t officially supported, Box may always update the service to prevent this.