Feeds:
Posts
Comments

Archive for the ‘Scripting’ Category

#!/bin/bash
##This is a script to simply and easily copy DVDs to your hard drive from the command line
#
#        Variable Declaration       
#
safemk () {
if [ ! -d $1 ];
  then mkdir $1;
  chmod +rw $1;
fi
}

MYHOME=/home/$USER
VIDEO=/home/$USER/video/dvds
#
#        Scripted Action           
#
           OPTIONS=”Home User-Specified Video/dvd QUIT”
           select opt in $OPTIONS; do
               if [ “$opt” = “Home” ]; then               
                DIRECTORY=$MYHOME
               elif [ “$opt” = “User-Specified” ]; then
                echo -e “Please enter the directory into which you would like to create your ISO: ”
        read  DIRECTORY
           elif [ “$opt” = “Video/dvd” ]; then
                 safemk video/dvds
         DIRECTORY=$VIDEO
           elif [ “$opt” = “QUIT” ]; then
         exit 0
               else
                clear
                echo “That wasn’t one of the options.”
               fi
               break
           done
    echo -e “Please enter the desired filename for your ISO (and please remember to precede ”
    echo -e “any SPACES or ‘ with a \) :”
    read FILENAME
    dd if=/dev/cdrom of=$DIRECTORY/”$FILENAME”.iso
    exit 0

Read Full Post »

for the GNU Compilers gcc and g++

Nice free book for GCC compiler for beginner

http://www.network-theory.co.uk/docs/gccintro/index.html

Read Full Post »

Looking around Ubuntu forum I came across backing up and restoring Ubuntu / Linux using tar.

The concept shown over is very simple and easy to understand. There are many commercial products such as Norton Ghost and Acronis to image backup Linux server but the following procedure makes task easier to implement.

1. Backing-up

Become a superuser

$ sudo su –

Change which ever directory you want you backup to end up but in the given example change it to / directory

cd /

Use the following command, this will make backup and compress it.

tar cvpzf mybackup.tgz --exclude=/proc --exclude=/lost+found \
--exclude=/mybackup.tgz --exclude=/mnt --exclude=/sys /
tar : The GNU version of the tar archiving utility
Options
c: create a new archive
v: verbosely list files processed
p: ignore umask when extracting files (the default for root)
preserve permissions
z: filter the archive through gzip
f: use archive file or device
Next come the exclude directories...
In the example we are not backing up everything since these directories aren't very useful to include.
Restoring:
Warning:

Be really careful while restoring your system as you might overwrite stuff that is important to you.Once again, change user to root to perform this job.One of the beautiful things of Linux is that This’ll work even on a running system; no need to screw around with boot-cd’s or anything. Of course, if you’ve rendered your system unbootable you might have no choice but to use a live-cd, but the results are the same. You can even remove every single file of a Linux system while it is running with one command. I’m not giving you that command though!

 tar xvpfz backup.tgz -C /
Options:
x: extract files from an archive
v: verbosely list files processed
p: preserve permissions
f: use archive file or device
z: filter the archive through gzip
C: change to directory DIR

WARNING: this will overwrite every single file on your partition with the one in the archive!

Create the following directory manually are the restore
mkdir proc
mkdir lost+found
mkdir mnt
mkdir sys
etc...

To restore Grub

http://www.ubuntuforums.org/showthre…t=grub+restore

Use the following script to run in the cron job

# generate backup filename with timestamp
timestamp=`date +%Y%m%d_%H%M%S`
file="backup"${timestamp}".tar.gz"

# generate backup tarball
tar -cvpzf "/PATH-GOES-HERE/$file" --exclude=/proc --exclude=/lost+found \
 --exclude=/media --exclude=/mnt --exclude=/sys --exclude=/tmp /

OR,
Use the following interactive backup
#!/bin/bash
# Instructions:
# Change the values of "STORAGE_MEDIA" and "ROOT_EXCLUDE_DIRS"
# Name this file "restore", make it "executable" and put it in "/bin"
# Invoke by: "sudo backup"
#
# Feel free to modify however you want.
#------------------------------------------------------------------------------------
#    CHANGE THE VALUES BELOW TO SUIT YOUR CONFIGURATION
#------------------------------------------------------------------------------------

ROOT="/*"
ROOT_EXCLUDE_DIRS="--exclude=/lost+found --exclude=/proc \
--exclude=/mnt --exclude=/sys --exclude=/backup*.tgz \
--exclude=/var/cache/apt/archives"
DEST_DIR=""

#-----------------------------------------------------------------
#             DO NOT CHANGE ANYTHING BELOW THIS LINE
#----------------------------------------------------------------
if [ "$USER" != "root" ]; then
    echo "You are not root user, use: sudo backup"
    exit
fi
:PRINTMSG
clear
echo "|-------------------------------------------------------------"
echo "|  IT'S RECOMMENDED TO RUN THIS SCRIPT BEFORE GNOME LOGIN  "
echo "|-------------------- Press CTRL+ALT+F1 at the GDM login"
echo "|-------------------------------------------------------------"
echo "|  BACKUP YOUR SYSTEM: "
OPTIONS="Backup Destination Exit"
LIST="1) Backup 2) Specify Destination 3) Exit" 

select opt in $OPTIONS; do
if [ "$opt" = "Exit" ]; then
    clear
    exit

elif [ "$opt" = "Backup" ]; then
    if [ ${#DEST_DIR} -gt 0 ]; then
        ROOT_EXCLUDE_DIRS=${ROOT_EXCLUDE_DIRS///backup*.tgz/$DEST_DIR}
    fi
    tar cvpfz $DEST_DIR/backup.`date +%d%m%y_%k%M`.tgz $ROOT $ROOT_EXCLUDE_DIRS
    echo "| BACKUP COMPLETE!"
    exit
elif [ "$opt" = "Destination" ]; then
    echo "| Actual Destination: $DEST_DIR/backup.`date +%d%m%y_%k%M`.tgz "
    echo "| For change it, just input some ABSOLUTE path "
    echo "| For leave it as it is, just input nothing an push Return"
    read NEW_DEST

    # if the NEW_DEST length is > 0 then we assign it
    if [ ${#NEW_DEST} -gt 0 ]; then
        DEST_DIR="$NEW_DEST"
    fi
    clear

    if [ ! -d $DEST_DIR ]; then
        echo "| WARNING: Specified Destination Dir: $DEST_DIR/ does not exist, yet"
    fi
    echo "|--------------------------------------------------------------"
    echo "|  BACKUP YOUR SYSTEM: "
    echo $LIST
else
    clear
    echo "| BAD OPTION! Select 1 or 2"
    echo "|--------------------------------------------------------------"
    echo "|  BACKUP YOUR SYSTEM: "
    echo $LIST
fi
done

Read Full Post »

Shell scripting

Variables

To separare variable from the rest of the text, use {}

 $ echo ${name}7

To remove a variable $name

 $ unset $name

To assign a result of a command to a variable

 $NAME=`whoami`
       or
 $NAME=$(whoami)

Standard I/O Redirection

To send the stdout and stderr to two separate files

 $ prog >file1 2>file2

To send both stdout and stderr to the same file

 $ prog >2&1 file1

To pipe stdout and stterr of prog1 to stdin of of prog2

 $ prog1 |& prog2

Arithmatic in shell scripts

To add $a and $b

 $ let c=$a+$b; echo $c
          or
 $ c=$(( a+b )) ; echo $c
          or
 $ echo $a+$b | bc
          or
 $ expr $a + $b

Assign the variable value to another variable

 i.e unmount.ksh
 #!/bin/ksh
 #usage $0 [ MYHOME | MYDATA | MYLOG ]
 UMOUNT=$1
 MYHOME="/u01/myhome"
 MYDATA="/u01/mydata"
 MYLOG="/u01/mylog"
  eval MNTDIR=\$(UMOUNT}
 /usr/sbin/umount $MNTDIR

Array Variables

In ksh, the syntax is

 set -A name value1 value2 ... valueN

In Bash

 name=(value1 ... valueN)

Array variables can be accessed as follows

 ${name[index]}

example:

 $ set -A address 404 'Broadway st' dublin ca

 $ echo ${address[0]}
   404

 $ echo ${address[1]}
   Broadway st

 $ echo ${address[2]}
   dublin

Using read to get user input for a variable value, also to prompt the user for input

 read user_name?" Please enter the user ID (Login Name) to add: "
 Please enter the user ID (Login Name) to add: 

Testing

Test command can perform tests on numeric and string data as well as on files. The test command can be used in explicit or implicit mode.

Test in explicit mode

 $ test "ABC" = "abc"

Test in implicit mode

 $ [ "ABC" = "abc" ]

Test result of command execution using “if” directly instead of using $?

  if ! diff $TMPFILE_old $TMPFILE_new > /dev/null ; then
  ........
  ........
  fi

Testing Numeric Values

 -e        Equality check
 -ne       Not equal 
 -lt       Less than 
 -gt       Greater than 
 -le       Less than or eqaul to 
 -ge       greater than or equal to 

Testing String Values

 -z string           True if string length is zero
 -n string           True if string lenth is non zero
 string1 = string2   True if string1 and string2 are equal
 string1 != string2  True if string1 is not equal to string2

Testing Files

 -d file          True if file is a directory
 -f file          True if file exists and not directory
 -s file          True if file is more than 0 bytes in length
 -r file          True if file is readable
 -w file          True if file is writable
 -x file          True if file is executable
 -L file          True if file is symbolic link
 file1 -nt file2  True if file is newer than file2

Loop

While loop

cat “filename” | while read data
do
…….
…….
done

case function

 case  $variable-name  in
                pattern1)   command
                            ...
                            command;;
                *)          command
                            ...
                            command;;
           esac

Breaking the loop

The break command discontinues the execution of loop immediately and transferes control to the command following the done keyword. You can pass a number n as an aargument to the break command. In that case, the break command jumps to the command after the nth occurrence of the done keyword.

Continue command

The continue skips the remaining part of the loop and transfer the control to the start of the loop for the next iteration.

Functions

To define function

 function_name ()
 {
 ......
 ......
 }

To process multiple options


Use getopts internal command to process mutiple options in a shell scripts

 while getopts ":ab:c" opt; do
    case $opt in 
	a  ) process option -a ;;
	b  ) process option -b 
	     $OPTARG is the option's argument ;;
	c  ) process option -c ;;
	\? ) print 'usage: bob [-a] [-b barg] [-c] args...'
	     return 1
    esac
 done
 shift $(($OPTIND - 1))

 normal processing of arguments...
  • Its first argument is a string containing all valid option letters. If an option requires an argument, a colon follows its letter in the string. An initial colon causes getopts not to print an error message when the user gives an invalid option.
  • Its second argument is the name of a variable that will hold each option letter (without any leading dash) as it is processed.
  • If an option takes an argument, the argument is stored in the variable OPTARG.
  • The variable OPTIND contains a number equal to the next command-line argument to be processed. After getopts is done, it equals the number of the first \”real\” argument.

Tips

In the bash shell, to enable interpretation of backslash escapes, use the -e option

 $ echo -e "this is a test line \n"

Read Full Post »

AWK Programming

awk is filter. awk performs the specified action on lines that match the pattern. Like ‘sed’, a regular expression pattern must be enclosed in a forward slashes. If no pattern is listed, the action is performed on every input line.

 awk 'pattern {action}' [files]

 $ awk '/^S/' phone.list
 $ awk '/^S/ {print $1}' phone.list

Variables


 $0 represents the whole current input record
 $1 represents the first field of the current input record
 $NF is number of fields for the current input record
 NR is number of Lines read

Operators


Relational Operators

 <  and >     Less than and Greater than
 <=  and >=   Less than or equl to AND  Greater than or equl to 
 ==  and !=   Equl to AND Not qual to 
 ~ and !~     Matches AND Doesn't match regular expressions

Boolean Operator:

 || 	Logical OR 
&& Logical AND
! Logical NOT

Assignment Operators

 ++ Add 1 to variable. 
 -- Subtract 1 from variable. 
 += Assign result of addition. 
 -= Assign result of subtraction. 
 *= Assign result of multiplication. 
 /= Assign result of division. 
 %= Assign result of modulo. 
 ^= Assign result of exponentiation. 
 **= Assign result of exponentiation.[6] 

Arithmetic Operator

 +  Addition
 - Subtraction
 * Multiplication
 / Division
 % Modulo Division (Remainder)
 = assignment

printf fuction: printf statement can be used to specify the width and alignment of output fields. Unlike print function, it does not give any new line charater at the end of the line. The contents of the field will ne right justif ied by default. You musr specify “-” to get left justification. Thus “%-20s” out puts a string left justif ied in a fiels 20 character wide.

Format Specifiers used in printf

 c 	ASCII Character 
 d 	Decimal Integer 
 e 	Floating point format 
 s	String 
 %	Literal %

Example of formatting:

 $ ls -l 
 -rwxrwxr-x    1 c1jkaila usa          543 Jan   30 15:28 autopath
 drwxr-xr-x    2 c1jkaila usa          512 Mar   19 15:14 bin
 drwxr-x---    2 c1jkaila usa          512 Mar   19 09:23 odbc

Without formating:

 $ ls -l | awk '{print $9, $2}'
 autopath 1
 bin 2
 odbc 2

With formating:

 $ ls -l | awk '{printf "%-25s%-5s\n", $9, $2}'
 autopath          1
 bin               2
 odbc              2
 sdata             2
 shareware         2
 tmp               2

To find a patern “Dec” in 6th field

 ls -l | awk '$6 == "Dec"'

To display the first and second fields of each line

 awk '{print $1, $2}' <filename>

To display all the lines containing string1 in file called file1

 awk '/string1/' file1

To display the first and 3rd fields of /etc/passwd file

 awk -F: '{print $1, $3}'

To display the first and last field of /etc/passwd file

 awk -F: '{print $1, $NF}'

To display the previous field to the last filed of any file

 awk '{print $(NF-1)}' file_name

To sum all the fields using awk command:

 ls -l | awk '{sum += $5} {print sum}'
 ls -l | awk '{sum += $5} END {print sum}'

To add few numbers and find out the average

 echo "test 10 20 30" | awk '{total = $2 + $3 + $4} {ave = total / 3} {print ave}'

To add the value of Unix variable $var1 to the beginning of each line in file1

 cat file1 | awk -v name=$var1 '{print name"  " $0}'

To replace DS with DSsmtp.comcast.net if the line has only single word ‘DS’

  awk '{if ($0~/^DS$/) {print "DSsmtp.comcast.net"} \
    else {print}}' /etc/mail/sendmail.cf 

This example is used in AIX

  awk '{if ($0!~/^\*/ && $0~/:$/ && $1~/^default:$/) {print $0"\n\tregistry = NIS"} \
     else if ($0!~/^\*/ && $0~/:$/ && $1!~/^default:$/) {print $0"\n\tregistry = files"} \
     else {print}}' /etc/security/user > /etc/security/user.new

To add # mark to the beginning of each line except for the lines starting with #, ftp, shell, and telnet:

  awk '{if ($1~/^#/ || $1~/^ftp/ \ 
  || $1~/^shell/ || $1~/^telnet/) \ 
  {print} else {print "#"$0}}' /etc/inetd.conf 

Running Unix commands directly using awk

 # ps -ef | grep syncd | grep -v grep | awk '{system("kill -9 " $2)}'

The above command find the process ID of sysncd and kills that process.

Some examples with sample output

 $ awk -F":" '{ print "username: " $1 "\t\tuid:" $3" }' /etc/passwd
 username: halt          uid:7
 username: operator      uid:11
 username: root          uid:0

Read Full Post »