Open window

Think globally, act locally!!

A script to clean up the log files in /var/log July 19, 2009

Filed under: Shell scripts — Sheikh Jafar Tarique @ 4:03 am

#!/bin/bash

LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
E_XCD=86 # Can’t change directory?
E_NOTROOT=87 # Non-root exit error.

# Run as root, of course.
if [ "$UID" -ne "$ROOT_UID" ]
then
echo “Must be root to run this script.”
exit $E_NOTROOT
fi

if [ -n "$1" ]
# Test whether command-line argument is present (non-empty).
then
lines=$1
else
lines=$LINES # Default, if not specified on command-line.
fi

cd $LOG_DIR

if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo “Can’t change to $LOG_DIR.”
exit $E_XCD
fi # Doublecheck if in right directory before messing with log file.

# Far more efficient is:
#
# cd /var/log || {
# echo “Cannot change to necessary directory.” >&2
# exit $E_XCD;
# }

tail -n $lines messages > mesg.temp # Save last section of message log file.
mv mesg.temp messages # Becomes new log directory.

# cat /dev/null > messages
#* No longer needed, as the above method is safer.

cat /dev/null > wtmp # ‘: > wtmp’ and ‘> wtmp’ have the same effect.
echo “Logs cleaned up.”

exit 0
# A zero return value from the script upon exit indicates success

 

Lists only the unique lines and count the number of times it was repeated in the original file June 2, 2008

Filed under: Shell scripts — Sheikh Jafar Tarique @ 7:19 am
#!/bin/bash
      sort -u $1 |
      while read lineVal
      do
             matchCount=`grep -c $lineVal $1`
             echo -e "$lineVal\t$matchCount"
      done
 

Merge the content of 2files line by line& put the output in another file June 2, 2008

Filed under: Shell scripts — Sheikh Jafar Tarique @ 7:19 am
               #/bin/bash
                lineCount=`cat $1 | wc -l`
                for i in `seq 1 $lineCount`
                do
                      lineTime=`head -$i $1 | tail -1  | cut -d"," -f1`
                      lineValue=`head -$i $1 | tail -1  | cut -d"," -f2`
                      lineValue2=`head -$i $2 | tail -1  | cut -d"," -f2`
                      result=$(($lineValue + $lineValue2))
                      echo "$lineTime,$result" >> output.csv
                  done
 

A service is running or not June 2, 2008

Filed under: Shell scripts — Sheikh Jafar Tarique @ 7:15 am
 a=`ssh oracle@jupiter 'pgrep oracle'`
            for p in $a
            do
            echo $p >/tmp/value
            done
            b=`cut -c1-4 /tmp/value 2>/dev/null; rm -f /tmp/value`
            if [ $b!=0 ] 2>/dev/null; then
            echo "oracle is running"
            else
            echo "oracle is not running"
            fi
 

Host is alive or not June 2, 2008

Filed under: Shell scripts — Sheikh Jafar Tarique @ 7:15 am

Here is a simple shell programming which will check whether a host is alive or not

The code:

               #!/bin/bash
               a=<hostname>
               ping -c2 $a >/dev/null
               if [ $? = 0 ] 2>/dev/null; then
               echo "Host is alive"
               else
               ech0 "Host is not alive"
               fi