Scripts

1. Write all the output to the current date file :

operation >>  `date “+%Y-%m-%d”`.log

Options can be illustrated with an example bash script :

cat test.sh

#!/bin/bash
echo testing
echo option m :$(date +%m)
echo option M :$(date +%M)
echo option d :$(date +%d)
echo option D :$(date +%D)
echo option y :$(date +%y)
echo option Y :$(date +%Y)
echo option H :$(date +%H)
echo option h :$(date +%h)
echo option s :$(date +%s)
echo option S :$(date +%S)
echo option b :$(date +%b)
echo option B :$(date +%B)
root@test:/root/test# date
Thu Dec 11 12:53:07 IST 2014

root@test:/root/test# ./test.sh
testing
option m :12
option M :53
option d :11
option D :12/11/14
option y :14
option Y :2014
option H :12
option h :Dec
option s :1418282601
option S :21
option b :Dec
option B :December

2. Sample script to copy files from location /source to a folder with today’s date.

#!/bin/bash
date=$(date -I)
if [ ! -d "/BACKUP/$date" ]; then
mkdir /BACKUP/$date
fi
touch /BACKUP/$date/backup_log.log
cp -pr /source/* /BACKUP/$date 2>> /BACKUP/$date/backup_log.log

3. Sample script to make a MySQL dump hourly and daily

#!/bin/bash
Now_hourly=$(date +%d-%b-%H_%M)
Now_daily=$(date +%d-%b-daily)
if [ "$1" == "hourly" ]; then
/usr/bin/mysqldump --all-databases | gzip -c | cat > /backup_location/mysql_backup-$Now_hourly.sql.gz
elif [ "$1" == "daily" ]; then
/usr/bin/mysqldump --all-databases | gzip -c | cat > /backup_location/mysql_backup-$Now_daily.sql.gz
else
echo "Error. Enter hourly or daily";
fi

Note : Add the cron entry with argument daily and hourly to take corresponding ones.

0 * * * * script_name hourly
0 2 * * * script_name daily

First one will take backup hourly and second one every day at 02:00 AM

4. Sample script to take cPanel home directory backup :

#!/bin/bash
cd /home
for file in *; do
if [ -d $file ]; then
/bin/tar -zcf /backup_location/$file$(date +_%d-%H).tgz /home/$file/public_html
#echo $file
fi
done

5. Sample command to compare two files and show the missing ones :

grep -Fxv -f A B

This one will show all the values in B which is not in A. Order doesn’t matter and is “case sensitive”
eg :
[root@gopu test]# cat A
1
2
3
[root@gopu test]# cat B
2
33
4
[root@gopu test]# grep -Fxv -f A B
33
4
[root@gopu test]# grep -Fxv -f B A
1
3

PS: If you want to find out the common fields in two files :

comm -12 A B

[root@test]# cat A
A
B
C
D
[root@test]# cat B
C
D
E
F
[root@test]# comm -12 A B
C
D

6. Python script to join two excel sheet files.

We are merging two excel sheets having one common column ‘serial’ with values comes in random order.

For example sheet1 contains values as below:

Computer   Serial     Owner
Dell        1234       A
HP          2345       B
Huawei      3456       C
Cisco       4567       D
Sun         5678       E

Sheet2 contains values as below :

Serial  Warranty Status
2345    1/1/2010
4567    2/2/2012
1112    3/2/2015

Result should be like

Computer     Serial     Owner       Warranty Status
Dell         1234           A        
HP           2345           B        1/1/2010
Huawei       3456           C        
Cisco        4567           D        2/2/2012
Sun          5678           E        
             1112                   3/2/2015

We are here merging the sheets for the common data ‘Serial’. Program is as below :

import pandas as pd

source1_df = pd.read_excel('a.xlsx', sheetname='source1')
source2_df = pd.read_excel('a.xlsx', sheetname='source2')
joined_df = pd.merge(source1_df,source2_df,on='Serial',how='outer')
joined_df.to_excel('/home/user1/test/result.xlsx')

Enjoy 🙂

7. Sample script that shows all the network configurations

#!/bin/bash
tput setaf 2;
echo "This script shows all the network configurations where we are running the script.... G";tput sgr 0
tput setaf 3; export MYIPADDR=`ifconfig eth0 | grep -w inet | awk '{print $2}' | cut -d ":" -f 2`
export MYBRCAST=`ifconfig eth0 | grep -w inet | awk '{print $3}' | cut -d ":" -f 2`
export MYNETMAS=`ifconfig eth0 | grep -w inet | awk '{print $4}' | cut -d ":" -f 2`
export MYPREFIX=`ipcalc -p $MYIPADDR $MYNETMAS | cut -d "=" -f 2`
export MYNETWOR=`ipcalc -n $MYIPADDR $MYNETMAS | cut -d "=" -f 2`
echo "MYIPADDR=$MYIPADDR"
echo "Broadcast=$MYBRCAST"
echo "netmask=$MYNETMAS"
echo "network=$MYNETWOR"
echo "prefix=$MYPREFIX"
tput sgr 0

8. Sample script that renames files based on condition

for f in *.txt; do mv "$f" "`echo $f | sed s/source/destination/`"; done 

eg :
[root@test]# ll
total 0
-rw-r–r– 1 root root 0 Nov 3 14:25 hari.txt
-rw-r–r– 1 root root 0 Nov 3 14:25 sdffdharidslfj.txt
-rw-r–r– 1 root root 0 Nov 3 14:25 sdf-hari-sdlfj.txt
[root@test]# for f in *.txt; do mv “$f” “`echo $f | sed s/hari/gopu/`”; done
[root@test]# ll
total 0
-rw-r–r– 1 root root 0 Nov 3 14:25 gopu.txt
-rw-r–r– 1 root root 0 Nov 3 14:25 sdffdgopudslfj.txt
-rw-r–r– 1 root root 0 Nov 3 14:25 sdf-gopu-sdlfj.txt

EnJOY 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s