Shell scripting


Use this to format any media , including your HDD , pen drives , usb portables , memory cards and so on.Please note : Be careful while selecting the /dev/sd* in the list menu.
Tired this as there was a need to right click format on Ubuntu : Download here

   Inspired by Scott’s FAST database implemented as a batch file and hasan.diwan@gmail.com bash script , i tired a bit more on it , it’s as below :


#!/bin/bash


fad()
{
echo “$1″ >> ~/.db
echo “Updated at $@”
}


f()
{
grep “$1″ ~/.db
}


fuse()
{
echo “f keyword to search”
echo “f -add or f -a to add “
}


case “$1″ in


-a |-add |–add) fad “$2″
;;

“”) f “$*”
;;

*) fuse
;;


esac

For the input either use bash’s tab completion, which will automatically put in backslashes as appropriate, or the user should quote it.

Sample :

To add
$ f -a “hemanth.hm nerd +919449167054″
$


To search
$ f “nerd”
$ hemanth.hm nerd +919449167054

My sensiour thanks to  \amethyst and greycat , for teaching me best partices :)


To run the script as a command , place the script in /bin.



More graphical approach

                                                                                              

#! /bin/bash

fad()                        
{
echo “$1″ >> ~/.db
notify-send “Database Updated $@”
}

f()
{
res=$(grep “$1″ ~/.db)
echo “$res”
notify-send “$res”
echo “$res” | zenity –text-info
}

fuse()
{
echo “f keyword to search”
echo “f -add or f -a to add “
}

case “$1″ in

-a |-add |–add) fad “$2″
;;

“”) f “$*”
;;


*) fuse
;;

esac
Just  wonder  why not you tweet while coding from terminal , so tired this :
#! /bin/sh

user=”twitter-username”
password=”twitter-password”

echo “You want to tweet about ?”
read message 
curl -u  $user:$password -d “status=$message” “https://twitter.com/statuses/update.xml”

Rather disadvantage if your friend could read your script right ;)
So tired this :

__________________________________________________________________________________

#! /bin/bash
tweet()
{
usr=$(zenity –entry –text=”Username” –title=”Twitt”)
pwd=$(zenity –entry –text=”Password” –title=”Twitt” –hide-text)
msg=$(zenity –entry –text=”Tweet” –title=”Twitt”)

if [ "$usr" = "" ]; then
zenity –info –text=”UserName invalid”
tweet
elif [ "$pwd" = "" ]; then
zenity –info –text=”Invalid Password”
tweet
elif [ "$msg" = "" ]; then
msg=”www.h3manth.com”
fi
}

tweet
URL=”https://twitter.com/statuses/update.xml”
curl -u ${usr}:${pwd} -d status=”${msg}” ${URL} -k -s > /dev/null

if [[ $? = 0 ]];
then
zenity –error –text=”You couldn’t twitt , check if everything is fine ” –title=”Twitt”
exit 0
else
zenity –info –text=”$msg is twitted” –title=”Twitt”
exit 1
fi

__________________________________________________________________________________

Join hands :  twitty

I had not written , something colorful like this from a long time , have tried to tie up all the basics in a single code.
Say i name it as fun.sh , and execute it , lets see what happens :O)

#!/bin/bash

# Variable(s)
user=”";

# Function,can be defined without the function keyword !!
# Simple function to “read” user input

read_info()
{
echo “What is your name ? “
# read data from keyboard to user variable
read user
# Say hi to the user
echo “Hi $user”
}
ask()
{
echo “Which OS do you like ?”
echo “1) Linux”
echo “2) Windows”
echo “3) MAC”
echo “4) None”
echo “5) You wont tell me”

read case;

case $case in
1) echo “The best , Happy Hacking :);;
2) echo “Who needs Gates and Windows , when you have freedom “;;
3) echo “You need an Apple?”;;
4) echo“None!!!, What are you using now then!”;;
5) exit
esac

}

# Note the spaces in after if condition and within [ ] , if you are using
# if [ $# -eq 0 ] or better go for if test $# -eq 0
# $# gives number fo command line args

if test $# -eq 0
then
echo “Number of command line arguments is $# !!”
else
# loop till the end of arguments and echo themall using “for” loop

echo “There are $# number of agrs they are {using for loop} : “
for arg in $*
do
echo $arg
done

# We can also do this:
# for args in “$@”
# do
# echo ${args}
# done

#
# loop till the end of arguments and echo themall using “while” loop

echo “There are $# number of agrs they are {using while loop} : “
while [ -n "$1" ]
do
echo $1
shift
# Yes,shift can be used to parse through the command line array
done

fi

read_info
ask

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ ./fun 1 2 3

Output :

There are 3 number of agrs they are {using for loop} :
1
2
3
There are 3 number of agrs they are {using while loop} :
1
2
3
Waht is your name ?
hemanth
Hi hemanth
Which OS do you like ?
1) Linux
2) Windows
3) MAC
4) None
5) You wont tell me
1
The best , Happy Hacking :)

I had not written , something colorful like this from a long time , have tried to tie up all the basics in a single code.
Say i name it as fun.sh , and execute it , lets see what happens :O)

#!/bin/bash

# Variable(s)
user=”";

# Function,can be defined without the function keyword !!
# Simple function to “read” user input

read_info()
{
echo “What is your name ? “
# read data from keyboard to user variable
read user
# Say hi to the user
echo “Hi $user”
}
ask()
{
echo “Which OS do you like ?”
echo “1) Linux”
echo “2) Windows”
echo “3) MAC”
echo “4) None”
echo “5) You wont tell me”

read case;

case $case in
1) echo “The best , Happy Hacking :);;
2) echo “Who needs Gates and Windows , when you have freedom “;;
3) echo “You need an Apple?”;;
4) echo“None!!!, What are you using now then!”;;
5) exit
esac

}

# Note the spaces in after if condition and within [ ] , if you are using
# if [ $# -eq 0 ] or better go for if test $# -eq 0
# $# gives number fo command line args

if test $# -eq 0
then
echo “Number of command line arguments is $# !!”
else
# loop till the end of arguments and echo themall using “for” loop

echo “There are $# number of agrs they are {using for loop} : “
for arg in $*
do
echo $arg
done

# We can also do this:
# for args in “$@”
# do
# echo ${args}
# done

#
# loop till the end of arguments and echo themall using “while” loop

echo “There are $# number of agrs they are {using while loop} : “
while [ -n "$1" ]
do
echo $1
shift
# Yes,shift can be used to parse through the command line array
done

fi

read_info
ask

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ ./fun 1 2 3

Output :

There are 3 number of agrs they are {using for loop} :
1
2
3
There are 3 number of agrs they are {using while loop} :
1
2
3
Waht is your name ?
hemanth
Hi hemanth
Which OS do you like ?
1) Linux
2) Windows
3) MAC
4) None
5) You wont tell me
1
The best , Happy Hacking :)

Functions are easy to define in scripting :

function function_name
{

}

function –> keyword .
function_name—> valid identifier .

Example :

##variables
today=$(date +”%x %r “)

##functions

function todayIS
{

echo $today

}

Output will be : Monday 26 January 2009 10:04:59 PM IST {something like this}

It is simple easy and yet very powerful to use functions(methods) in your code :) .
Be careful in selecting the function and variable names , note that it wont conflict with the reserved keywords of the shell like PATH,HOME etc

Functions are easy to define in scripting :

function function_name
{

}

function –> keyword .
function_name—> valid identifier .

Example :

##variables
today=$(date +”%x %r “)

##functions

function todayIS
{

echo $today

}

Output will be : Monday 26 January 2009 10:04:59 PM IST {something like this}

It is simple easy and yet very powerful to use functions(methods) in your code :) .
Be careful in selecting the function and variable names , note that it wont conflict with the reserved keywords of the shell like PATH,HOME etc

The best way is to get any flavor of UNIX .
You can just do it in terminal or go for more attractive GUI like : geany

Alternatives for other OS :

1.cigwin : cygwin
2.mks : mks
3.winbash : winbash

Online best alternative : junix

Now , the simplest way to write a script in terminal :

$ cat > script_name
> type here
>
>
press Ctrl+D

then ,

$ chmod 777 script_name

$ ./script_name

Other methods will be disclosed in the next coming posts

Loopy loops :

while loop

Syntax :

while conditiondo........done

until loop 
until falsedo .......done

for loop
for variable in listdo.......done

for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

As we get different routes in the journey of life , we get different paths in the flow of our code , which to select depends on our logic , it may be right or wrong , but it depends on our conditions !!

if condition

if this is true then i would do that


Syntax 0:

 if condition       then       ...       ...    else         ...fi Syntax 1:
           if condition                then                ....                elif another_condition                    then                          ....                else                         ....            fi 


test command or [ expr ]

Am i a hero ?

If test returns zero then I am a hero , or else any non-zero value from the test clearly says I am not a hero.

Syntax:


test expression OR [ expression ]


Can be used on : Integer ,File types and Character strings .

Conditions :


eq equal to
ne not equal
gt greater than
lt lesser than
ge greater than or equal
le lesser than or equal


For strings we can use == and != , for equals and not equal to conditions.

For file we can use -[ s,f,d,w,r,x,] file , can be used to check ,non empty ,file,directory, wrx permissions respectivly.

Local operations like !,-a,-o i.e NOT,AND,OR can be used with expressions .

Case statement


Case in expression then go to the referred case number or the pattern

Syntax:



case expression in
pattern1) execute commands ;;
pattern2) execute commands ;;
...
esac


P.S : “;;” and ‘)’


Next Page »