Warning the script in this blog post are not guaranteed to work, they are meant only to educate and to show what can be done with Bash. I am not responsible for any data lost or damage caused.

If you run linux, or if you’re going to. You’re going to need to know about Bash.

Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the “Bourne-Again SHell”, a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh, which appeared in the Seventh Edition Bell Labs Research version of Unix
    Bash manual from gnu.org

Scripts Directory

When you create your own scripts, you need a place to store them, and this location needs to be included into the PATH.
So that when you can access your scripts from anywhere on the command line.

The usual thing to do, is to create a “bin” or “scripts” directory in your own folder.

  $ mkdir ~/scripts

And then to add that directory to the PATH variable.

  $ export PATH=$PATH:~/scripts

By putting ~/scripts in your PATH variable, you enable the system to run the executable scripts in ~/scripts from anywhere on you system.

The PATH variable will only last while this session is open, so you will need to change it everytime a new session starts. We can do this by adding the “export …” line to our bash configuration file at ~/bashrc

As an example, add the following script to ~/scripts/helloworld

#!/bin/bash

echo Hello, World!

To execute, it needs to have executable permission. You can give it this by running this command for every script that you put into the ~/scripts folder.

  $ chmod +x ~/scripts/helloworld

This will ensure that the script will be found and that it will be executable.

  $ helloworld
  Hello, World!

Bash Scripts to Encrypt and Decrypt files

Encrypting files are cool. You will need to have gnupg, srm, ncurses and tar installed. Enough Said.

Encrypt Script

Place the following into ~/scripts/encrypt:
  #!/bin/bash

  tput sc
  while [ -s $pass ] || [ $pass '!=' $pass2 ]; do 
   read -s -p "Enter Passphrase: " pass;
   tput rc;
   read -s -p "Re-Enter Passphrase: " pass2;
   tput el1;
   tput rc;
  done;

  for file in $@; do
   tar -cf - $file | gpg -c --passphrase $pass > $file.gpg;
   srm $file;
  done

Then run the script:

  $ encrypt file-to-be-encrypted

The file will be replaced with a .gpg file. Remember to store you passwords in a safe place

Decrypt Script

To decrypt the .gpg file you will need the following script at ~/scripts/decrypt.

  #!/bin/bash
  #~/scripts/decrypt

  tput sc;
  while [ -s $pass ]; do 
   read -s -p "Enter Passphrase: " pass;
   tput el1;
   tput rc;
  done;

  for file in $@; do
    tar -xf <(cat $file | gpg -d --passphrase $pass);
    if [ $? -eq 0 ]; then 
      srm $file;
    else
      echo Failed to Decrypt;
      echo [password might be wrong];
    fi;
  done;

Streaming Audio and Webcam feed over SSH

Sometimes you would like to see whats going on in your office, or at home. Streaming the data over ssh is very simplistic

Video feed

The webcam was easy…

 ssh desktop -Y mplayer tv://device=/dev/video

Audio feed

This might or might not work, depending on your setup.

/dev/audio1 is whichever device manages your microphone. It might be /dev/dsp.

/dev/audio is the speaker device on which you would like to hear the sound.

  ssh desktop 'dd bs=1 if=/dev/audio1' > /dev/audio  

Switching between keyboad-layouts

I use both Qwerty and Dvorak layouts on my keyboard. To easily switch, I’ve made this script.

  #!/bin/bash
  # ~/scripts/keyboard-toggle
  if $( setxkbmap -print | grep dvorak > /dev/null );then
    setxkbmap us;
  else
    setxkbmap dvorak;
  fi;

Then I added a hotkey to my window manager to run “keyboard-toggle”. /

Serializing .jpg file names to md5 file names;

I have a folder where I dump all the funny pictures I find on the web. The following script allows me to store a great amount of images in one folder without causing file name conflicts.

Warning Original file names will be lost using this method.

  #!/bin/bash
  # ~/scripts/serialize

  cnt=${#};
  a=0;
  while [ $a -lt $cnt ]; do
    a=$((a+1));
    file=${!a};
    length=${#file}
    # proceed if file is jpg
    if [ "${file: -4}" == ".jpg" ]; then
      md5name=`md5sum $file | cut -d ' ' -f 1`;
      file_basename=`basename $file .jpg`
      if [[ $md5name != $file_basename ]]; then
        echo Renaming $file to $md5name.jpg
        mv $file $md5name.jpg;
      fi;
    else
      echo $file not image, skipping
    fi;
  done;