Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sunday, 13 April 2014

Command line fun!



If you use the terminal a lot there are a few cool applications you can use to Jazz things up. Here are some of my favourites.

Steam Locomotive (sl)

  • Probably my favourite to get a cool steam locomotive ti run across the screen (shown at top of post)
  • Install with
 $ sudo apt-get install -y sl
  • Run with
$ sl
  • Or to make it 'Fly'

$ sl -F

As a practical joke you could set an alias for 'ls' on someone’s machine :)

 

Cmatrix

  • I think this is really coolto have running :)
  • Install
$ sudo apt-get install -y cmatrix

  • Run 
$ cmatrix


Oneko




  • This is pretty random but worth trying at least once
  •  It changes your cursor into mouse and creates a cat
  • The cat then starts chasing around your mouse cursor
  • Install
$ sudo apt-get install -y oneko

  • Syntax to see the magic happen!
$ oneko

Fortune



  • Fortune give 'fortune cookies' of wisdom when they are run
  • Install the 'classic' version
  $ sudo apt-get install -y fortunes
  • Run
$ fortune
Although the moon is smaller than the earth, it is farther away.

  •  If you search apt there are lots of other versions (including different languages)
  • System admins will like 
  $ sudo apt-get install fortunes-bofh-excuses
  • You can include these words of wisdom in your .bashrc

 

Cowsay



  • A configurable speaking/thinking cow!
  • Install

$ sudo apt-get install -y cowsay

  • Run 

$ cowsay "Enter any text"

  • There are other animals you can use listed at /usr/share/cowsay/cows
  • You can select a different cowfile with '-f'
$ cowsay -f ghostbusters.cow "Moo"


  •  The man file for cowsay is very detailed on the available options

Cowsay with Fortune




  • Its a great combination to use cowsay with fortune and is very simple
  • Examples
$ fortune | cowsay
$ fortune | cowsay -f ghostbusters.cow

  • You can use these in your .bashrc file
  • An option is to script it so the cowfile will be random

Figlet



  • Gives a cool 1980's look for text
  • Install
$ sudo apt-get install -y figlet
  • Example
$ figlet vancouver


Redirect Bash Errors and Output to /dev/null



Running scripts in the background or for cron jobs can product output that you would like to redirect. The standard way to redirect errors and output in Linux is to /dev/null.

Command line Bash Script Usage

  • To run a script and redirect errors and output

$ ExampleScript.sh > /dev/null 2>&1

Cron Bash Script Usage

  • Run a cron job every 30 minutes and redirect error and output from script

*/30 * * * * ~/bin/CheckServerStatus.sh > /dev/null 2>&1

What is the 2>&1 syntax?

  • Stdin is 0
  • Stdout is 1
  • Stderr is 2
  • If 2>1 was defined to redirect stderr to stdout, it would be interpreted as redirect stderr to a file named 1 
  •  Using & defines what follows is a file descriptor and not a filename
  •  Leads to : 2>&1

What is /dev/null?

  • Virtual file on Linux
  • Data written to /dev/null is discarded
  • Known sometimes as the "Black hole" 

Shuf - generate random permutations on Linux



Its always great to find a new Linux command that I have never heard of but is really useful. The command 'shuf' comes in to this category. It should be installed by default on most Linux systems as its part of the GNU Coreutils.

I wanted to generate random MAC address's from a text file which was simple with :

$ shuf -n 2 MACList | sort | uniq
00:A0:C8:14:C2:22
00:A0:C9:14:C8:29


Examples of using 'Shuf'

  • Generate 10 random numbers between 1 and 999

$ shuf -i 1-999 -n 10
182
588
696
841
574
204
173
641
958
289

  • Output results to a file
$ shuf -i 1-999 -n 10 -o results.txt
$ head -n 1 results.txt
749


  • Sort results and ensure numbers are unique

$ shuf -i 1-999 -n 10 | sort | uniq
101
142
298
396
508
564
793
870
923
95

  • Generate random words from Linux dictionary
$ shuf -n 2 /usr/share/dict/words
universe's
phosphorus


























Useful bashrc setup for command line efficiency



I find the following config items in my .bashrc file to use any system efficiency when I set it up.

Include home bin within PATH


  • I create a bin directory in the home directory to add in all my executable programs

# Include bin dir to PATH
export PATH=/home/cwishaw/bin:$PATH


Password Generator

 

  • I find it useful to be able to generate random passwords quickly :

GeneratePwd(){ < /dev/urandom tr -dc A-Z-a-z-0-9 | head -c${1:-12};echo;}
  • This will generate a random password of 12 chacacters :
$ GeneratePwd
JTRKjlXyAIGS

 

Extract files quickly


  • The following command will extract the most common file types without having to memorize all the syntax
Extract () {
   if [ -f $1 ] ; then
       case $1 in
           *.tar.bz2)   tar xvjf $1    ;;
           *.tar.gz)    tar xvzf $1    ;;
           *.bz2)       bunzip2 $1     ;;
           *.rar)       unrar x $1       ;;
           *.gz)        gunzip $1      ;;
           *.tar)       tar xvf $1     ;;
           *.tbz2)      tar xvjf $1    ;;
           *.tgz)       tar xvzf $1    ;;
           *.zip)       unzip $1       ;;
           *.Z)         uncompress $1  ;;
           *.7z)        7z x $1        ;;
           *)           echo "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
 }



Move up directories


  • The following command enabled you to quickly move up directories

Up(){
  local d=""
  limit=$1
  for ((i=1 ; i <= limit ; i++))
    do
      d=$d/..
    done
  d=$(echo $d | sed 's/^\///')
  if [ -z "$d" ]; then
    d=..
  fi
  cd $d
}


Include bash_aliases if exists

 

  • I prefer to manage all my aliases in a separate file as its easier to maintain.

if [ -f ~/.bash_aliases ];
 then
        . ~/.bash_aliases
fi


  •  The bash_aliases can have all your alias definitions of the format :

alias pacman='sudo pacman'
alias vi='vim'




Remove comments from a file on Linux with sed


You can quickly remove comments from a file on Linux using sed. For example if the comment starts with #.

$ sed '/^\#/d' filewithcomments.txt > filewithcomments.tmp
$ mv filewithcomments.tmp filewithcomments.txt
$ rm filewithcomments.tmp

Quickly view config files on Linux with no comments



There are many occasions when I am configuring new software on Linux and the configuration files are full of comments. While these have admittedly useful information what I want is to quickly view only the configuration that is currently set. Then I can see what I need to change or add in the file.

The solution is to use grep with regular expression ^[^#] which matches the first character of any line when that character is not equal to #. Lines without characters do no match so are also not displayed.

$ grep ^[^#] <path to file>

Example

$ grep ^[^#] /etc/resolv.conf

modern.ie bash: ./IE11.Win8.1.For.LinuxVirtualBox.part1.sfx: No such file or directory on Linux




While I love using Linux software UI testing in browsers does mean having to sometimes test using IE. I found out that helpfully Microsoft make available for testing only Virtualbox VM's to allow browser testing via the modern.ie site . Great, this is just what i need!  After downloading the VM's the instructions are pretty straight forward but instead of extracting the files I got this :

$ ./IE11.Win8.1.For.LinuxVirtualBox.part1.sfx

bash: ./IE11.Win8.1.For.LinuxVirtualBox.part1.sfx: No such file or directory

I was using Arch Linux and even though the MD5 sums were fine I downloaded the files again with the same result. I also tried Ubuntu, Crunchbang (#!) Linux with the same result. After a lot of trial and error I worked out that it needs some 32 bit libraries to extract the files correctly!

My solution was to use a 64 bit CentOS 6.x VM and installed :

# yum -y install glibc.i686 libstdc++.i686

Now you can extract the file as normal!

Hope this helps someone as it was a pain for me to work out.