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

Saturday, July 19, 2008

Special character ; at bashprompt

; is used as a command separator and to terminate a case statement at bash

#!/bin/bash

# This script is to explain the usage of the ; as a special character in bash.
# Sijo George
#--------------------------START-------------------------------
echo "Hello world,"; echo "How are you?"; # Execute two commands in a line using a ; delimiter
case $1 in
[a-z] ) echo " The input is an alphabet " ;; # Terminator in the case option
[0-9] ) echo " The input is a number" ;;
* ) echo " The input is neither an alpabet or a number ";;
esac

Thursday, July 17, 2008

Usage of Special character '#' at bash

Read through the script below. This better explains the usage

#!/bin/bash
# This script is to explain the special char #
# Sijo George sijo.george@gmail.com
#------------------START------------------------#
#This is a comment
echo "This is #not a comment"
echo 'This also # is not a comment'
echo But ...This is a #comment
echo However This \# is not a comment
echo "Parameter sub is not a comment"
echo ${PATH}
echo ${PATH#*:};# Cuts of the shortest string of pattern after the # from the front
echo ${PATH##*:};# Cuts of the longest string of pattern after the # from the front
echo "Base conversion is not a comment "
echo $((2#1010)) # Convers binary 1010 to decimal
echo $((16#ABCD)) # Converts hex ABCD to decimal
string="this is a string"
echo ${#string} # Gives the number of characters in the string
array=(1 test a 10)
echo ${#array} # Gives the length of first element of the array
echo ${#array[*]} # Gives the number of elements in the array
echo ${#array[@]} # Gives the number of elements in the array
echo ${#} # Gives the number of command line parameters.
echo ${#*} #Gives the number of command line parameters.
echo ${#@} #Gives the number of command line parameters.

Thursday, February 21, 2008

Keyboard shortcuts at bash prompt

Alt + < - Move to the first line in the history
Alt + > - Move to the last line in the history
Alt + ? - Show current completion list
Alt + * - Insert all possible completions
Alt + / - Attempt to complete filename
Alt + . - Yank last argument to previous command
Alt + b - Move backward
Alt + c - Capitalize the word
Alt + d - Delete word
Alt + f - Move forward
Alt + l - Make word lowercase
Alt + n - Search the history forwards non-incremental
Alt + p - Search the history backwards non-incremental
Alt + r - Recall command
Alt + t - Move words around
Alt + u - Make word uppercase
Alt + backspace - Delete backward from cursor
Ctrl + a - Jump to the start of the line
Ctrl + b - Move back a char
Ctrl + c - Terminate the command
Ctrl + d - Delete from under the cursor
Ctrl + e - Jump to the end of the line
Ctrl + f - Move forward a char
Ctrl + k - Delete to EOL
Ctrl + l - Clear the screen
Ctrl + r - Search the history backwards
Ctrl + R - Search the history backwards with multi occurrence
Ctrl + u - Delete backward from cursor
Ctrl + xx - Move between EOL and current cursor position
Ctrl + x @ - Show possible hostname completions
Ctrl + z - Suspend/ Stop the command

Fun with bash - Addition at bash prompt

Addition at bash prompt can be done in many ways. I have managed to collate some of them in the script below. The script below counts from 1 to 14 in 14 different ways. Its fun isn't it. It shows how powerful and feature-rich this simple scripting language is.

#!/bin/bash
n=1;
echo -n "$n, "
let "n = $n + 1"
echo -n "$n, "
: $((n = $n + 1))
echo -n "$n, "
(( n = n + 1 ))
echo -n "$n, "
n=$(($n + 1))
echo -n "$n, "
: $[ n = $n + 1 ]
echo -n "$n, "
n=$[ $n + 1 ]
echo -n "$n, "
let "n++"
echo -n "$n, "
(( n++ ))
echo -n "$n, "
: $(( n++ ))
echo -n "$n, "
: $[ n++ ]
echo -n "$n, "
n=`echo "$n + 1" | bc`
echo -n "$n, "
n=`echo "$n + 1" | bc -l`
echo -n "$n, "
n=`expr $n + 1`
echo "$n"