Tag Archives: cli

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

gpg for Mac

Lately I’ve been building a bunch of “Linux” command line tools for my Mac. As I focus on security, I decided GPG would be the next tool. You can download my GPG public key at KenjiYoshino.pub.

Download

  1. gpg-1.4.13.tar.gz
    • SHA-1 of gpg-1.4.13.tar.gz: 45901f228377c65b445104d7037ad26dde70fe7a
    • Signature: gpg-1.4.13tar.gz.sig
    • SHA-1 of the gpg executable: 361b9beec3667abdc01d30b0b5ac0b215b3d4d48
    • SHA-1 of the gpgv executable: 006c7ac41d63f1a1a7aa695428f42acd9f7a54e3
  2. Open the Terminal and navigate to the downloaded archive
  3. Extract the files by running tar xzf gpg-1.4.13.tar.gz
  4. Login with an account with Administrator privileges
  5. Copy /bin/gpg and /bin/gpgv to /usr/local/bin
  6. Copy /man/gpg.1 and /man/gpgv.1 to /usr/local/share/man/man1
  7. Make sure all users have execute/read access to these files

Compile

Note: You must have Xcode installed.

  1. Download the GPG 1.4.13 source from http://www.gnupg.org/download/
  2. Open a terminal window and browse to the downloaded archive
  3. Extract the archive using tar xzf gnupg-1.4.13.tar.gz
  4. Open the gnupg-1.4.13 directory
  5. Run ./configure
  6. Run make
  7. You will have the gpg and gpgv binaries in the /g10 directory and the man page in the /doc directory
  8. Copy /g10/gpg and /g10/gpgv to /usr/local/bin
  9. Copy /doc/gpg.1 and /doc/gpgv.1 to /usr/local/share/man/man1
  10. Make sure all users have execute/read access to these files

Showing VIM Location

When working with Linux, every VIM editor automatically shows the little status bar at the bottom of the page. It displays useful tidbits of information like line number, column number, and location (as a percent). For some reason, Apple didn’t think to enable this in MacOS’s VIM editor. Fortunately that’s easy to fix by following these steps:

  1. Open a Terminal window
  2. If you’re not at the root of your home directory run cd ~
  3. Run command vi .vimrc This will create or open the .vimrc file which are you personal VIM “preferences”
  4. Enter set ruler on an empty line
  5. Save and quit

wget for Mac

I’m working on a Google Chrome extension to allow users to check the URL they are being forwarded to when they click on shortened URLs. I wanted to make sure I’m parsing responses and forwards properly, so wget came to mind. I’ve used wget a few times on Linux, but I’m doing my development on my Mac, so I compiled wget for my Mac.

Download

  1. wget-1.14.tar.gz
    • SHA-1 of wget-1.14.tar.gz: fedb008d414b87e44962e5e17671230aebe88189
    • Signature: wget-1.14.tar.gz.sig
    • SHA-1 of the wget executable: 062e17849d46cecc3d1e6d4a42b88e7eba16d96a
  2. Open the Terminal and navigate to the downloaded archive
  3. Extract the files by running tar xzf wget-1.14.tar.gz
  4. Login with an account with Administrator privileges
  5. Copy /bin/wget to /usr/local/bin
  6. Copy /man/wget.1 to /usr/local/share/man/man1
  7. Make sure all users have execute/read access to these files

Compile

Note: You must have Xcode installed.

  1. Download the wget source (wget-1.14.tar.gz) from http://ftp.gnu.org/gnu/wget/
  2. Open a terminal window and browse to the downloaded archive
  3. Extract the archive using tar xzf wget-1.14.tar.gz
  4. Open the wget-1.14 directory
  5. Run ./configure --with-ssl=openssl
    If you have GNUTLS installed, you can omit “–with-ssl=openssl”
  6. Run make
  7. You will have the wget binary in the /src directory and the man page in the /doc directory
  8. Copy /src/wget to /usr/local/bin
  9. Copy /doc/wget.1 to /usr/local/share/man/man1
  10. Make sure all users have execute/read access to these files

7zip for Mac

With taxes approaching , I started revisiting the old question of how to securely transfer files between Mac and Windows.

My first thought was using zip and openSSL, but that requires the Windows user to have openSSL installed and use the command line. Maybe an encrypted zip, but I couldn’t figure out what encryption options the Mac command line supports and the Windows user needs WinZip to decrypt. I like 7zip and it works well for Windows and p7zip works well for Linux, but Mac distributions for p7zip don’t seem to be maintained for Mac. I decided to take things into my own hands. It turned out to be simpler than I expected.

  1. Install the Xcode (available through the Mac App Store) and the Xcode Command Line Tools
  2. Download the full p7zip source from sourceforge
  3. Extract the 7zip source
  4. Open Terminal, navigate to the extracted files
  5. Run make
  6. As an administrative user, copy <extracted files>/bin/7za to /usr/local/bin
  7. As an administrative user, copy <extracted files>/man1/7za.1 to /usr/local/share/man/man1/
  8. Run chmod +r 7za.1 (for some reason only the owner had read access)

You can download the binary I created at 7za-9.20.tar.gz. Extract this file using tar xzf 7za-9.20.tar.gz and then follow steps 6-9 above. The SHA1 hash of the tar.gz archive is 910eb7f91056ac66009373a542a45c1b1b82094d. The SHA1 hash of the 7za executable is e7b0b37be87354c7188401ea46dc65c6611647b5. The signature for the tar.gz archive is 7za-9.20.tar.gz.sig.

Note: On my older MacBook that has undergone a number of OS upgrades, /usr/local/share/ did not exist. Putting the man file in /usr/local/man/man1/ did not work. I had to create /usr/local/share/man/man1/.