Shashikant shah

Monday, 13 July 2015

ARRAY Details

$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'

echo ${Unix[1]}

$./arraymanip.sh
Red hat
====================
declare -a Unix=('Debian' 'Red hat')
echo {unix[@]}
#./t.sh
Debian Red hat
==================

declare -a shashi=('jk'' 'gol 'hello');

echo ${shashi[0]}

#./script.sh

jk
====================
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

echo ${#Unix[@]} #Number of elements in the array
echo ${#Unix}  #Number of characters in the first element of the array.i.e Debian
echo ${#unix [1]} # length of the element located at index.
echo ${unix[@]:1:3} # remove the sentense in one line.e.g:- remove sentense 1 & 3
echo ${unix[2]:1:2} # remove the word in sentance.
echo ${unix[@]/ubuntu/sco unix} # ubuntu ko replace karega.
echo ${#unix[*]} # last file name.

unset unix[1] #1 is not showing.
unset Unix    # unix array not showing
================
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}

$./arraymanip.sh
AIX

========================
diff_file
file1
file2
file3

#!/bin/bash
file=`cat "diff_file"`
for t in ${file[@]}
    do
echo $t
    done
echo "read file content!"

#./script.sh
file1
file2
file3

=======================

#!/bin/bash
# define file array
files=(/etc/*)

# find total number of files in an array
echo "Total files in array : ${#files[*]}"
total=${#files[*]}

# Print 1st file name
echo "First filename: ${files[0]}"
echo "Second filename: ${files[1]}"
echo "Third filename: ${files[1]}"

# total - 1 = last item (subscript) in an array
echo "Last filename: ${files[$(( $total-1 ))]}"

echo
echo "****************"
echo "*** For Loop ***"
echo "****************"

# Use for loop iterate through an array

# $f stores current value

for f in "${files[@]}"
do
    echo -n "$f "
done

echo
echo
echo "**************************"
echo "*** C Style For Loop ****"
echo "**************************"
echo
# Use c style for loop
# get total subscripts in an array
total=${#files[*]}
#
for (( i=0; i<=$(( $total -1 )); i++ ))
do
    echo -n "${files[$i]} "
done

echo

=======================close======================================
cat file
2012/05/21
2012/05/22

vim test.sh

OLDIFS=$IFS
IFS="/"
while read f1 f2 f3
do
    echo "Year is : $f1"
    echo "Month is : $f2"
    echo "Date is : $f3"
done < file
IFS=$OLDIFS

./test.sh
Year is : 2012
Month is : 05
Date is : 21

Year is : 2012
Month is : 05
Date is : 22

====================================
# bash -c 'set w x y z; IFS=":-;"; echo "$*"' 
 
# w:x:y:z

Sunday, 12 July 2015

script for data copy to remote server and date file matching script


1.Put to remote host password :-
# echo "echo redhat" > /root/shashi/passwd

 2.Create a data copy script :-
#!/bin/bash

export SSH_ASKPASS="/root/shashi/passwd"

setsid ssh -o StrictHostKeyChecking=no "root"@"192.168.10"

exit 1

OR

setsid scp -r "/root/shashi/*" "root"@"192.168.0.10:/root/shashi/data"
==========================
# chmod +x passwd
#chmod +x script.sh
#./script.sh
==========================================================
===========================================================

#!/bin/bash

Path=/tmp

d=$(date +%d --date="1 days ago")

file=$(ls -lh $Path | awk '{print $6 " " $7 " " $9}' | sed -n /$d/p)

count=`echo -n "$file" | wc -l`

    if [ 0 == "$count" ];then

echo "date is not Match with file"

else

echo "date is Match with file"

fi
-=========================================================
 How do I backup /var/www/html using rsync?

Run rsync over SSH using password authentication, passing the password on the command line:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/

======================================================================
export SSHPASS=password
sshpass -e ssh user@remotehost

Saturday, 11 July 2015

diff script in linux

#!/bin/bash
batch=/home/user/test/test1
prod=/home/user/test/test2

echo -n "Enter DC FTP-1 Prod path: "
    read ftpbatch

echo -n "Enter DC FTP-2 Prod  path: "
    read ftpprod

echo -n "Enter File name to campare : "
    read file

export A=$ftpbatch
export B=$ftpprod
export C=$file

E=$(rm -rf $batch/$C* | rm -rf $batch/*)
F=$(rm -rf $prod/$C* | rm -rf $prod/*)

D=`wget -P $batch ftp://$server1$A/$C 2>&1 /tmp/ftp_wget_logs`
G=$(echo $?)
if [ 0 == "$G"]; then
    echo "file has been download $batch/$C"
else
    echo "file is not found ftp://$server1$A/$C"
exit 1

T=`wget -P $batch ftp://$server1$B/$C 2>&1 /tmp/ftp_wget_dest_logs`
H=$(echo $?)
if [ 0 == "$H"]; then
    echo "file has been download $prod/$C"
else
    echo "file is not found ftp://$server1$B/$C"
exit 1

I=$(cd $batch && wc -l $C)
J=$(cd $prod && wc -l $C)
# DD=$(sed "s/ /\\ /g" /tmp/ab) world me space hai to.
#$(cd $batch && wc -l "$DD")

H=$(cd $batch && du -sh $C | awk '{print $1}')
H=$(cd $prod && du -sh $C | awk '{print $1}')
cmd1=$(diff -u --ignore-all-space $batch/"$C" $prod/$"$C") sare space remove
cmd=$(diff --changed-group-format='%>%<' --unchanged-group-format='' $batch/$C $prod/$C)
Z=$(diff --changed-group-format='%>%<' --unchanged-group-format='' $batch/$C $prod/$C | wc -l)

if [ 0 == "$Z" ];then
cat << EOF > /home/user/test/diff_logs
===================================
"File-Name: $C ,File-Size : $H, TL: $I"
"File-Name: $C ,File-Size : $G, TL: $J"
"DIff is not found in both files."
==================================
EOF
else
cat << EOF > /home/user/test/diff_logs
"File-Name: $C ,File-Size : $H, TL: $I"
"File-Name: $C ,File-Size : $G, TL: $J"
$cmd
=====================================
EOF
fi

=============== close=============================================
diff contune 
 ===============================================================
#!/bin/bash
batch=/home/user/test/test1
prod=/home/user/test/test2

A=$(ls -l $batch | awk '{print $9}' | sed 1d > /tmp/prod_ftp1)
B=$(ls -l $prod | awk '{print $9}' | sed 1d > /tmp/prod_ftp2)
C=`diff --changed-group-format='' --unchanged-group-format='%>' /tmp/prod_ftp1 /tmp/prod_ftp2`
D=`echo -n "$C" > /tmp/prod_ftp3`

total=${#files[*]}

for (( i=0; i<=$(( $total -1 )); i++ ))
do
    echo -n "${files[$i]"
done

Rename File in linux

We are find the file name shashi_kant and Rename to shashi-kant.

#!/bin/bash
FILE1=shashi_kant
PATH1=/home/shashi/R0_data

if [ 0 == 0 ]; then

A=$(ls -l $PATH1 | grep -i $FILE1 | awk {'print $9'} > /tmp/R0_shashi)
for file in `cat /tmp/R0_shashi`
do
B=$(echo $file | tr '[:lower:]' '[:upper:]')
C=$(mv   $PATH1/$file  $PATH1/$B)  ### CAPITAL
done
fi

if [ 0 == 0 ]; then

B=$(ls -l $PATH1 | grep -i $FILE1 | awk {'print $9'} > /tmp/R0_shashi1)
for file1 in `cat /tmp/R0_shashi1`
do
D=`mv   $PATH1/$file1  $PATH1/${file1/${file1:6:1}/-}`; ## rename file
done
fi