shell脚本对于sed和awk等使用

#! /bin/bash
# ./bf.sh /tmp XX.XX.XXX.XXX

SUCCESS=0
LockFile="bflock.lock"
errno_gsh=0
bf_all=1
bf_ip=""
if [ "$#" -ne "1" ]
then
        if [ "$#" -eq "2" ]
        then
                echo "$2"
                bf_ip=`echo $2 | awk --posix '/[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/{print $0}'`
                if [ -z "$bf_ip" ]
                then
                        echo "cs2 is 2 but ip is err $2"
                        exit 1
                fi
                bf_all=0
        else
                echo "wrong cs2  $#"
                exit 1
        fi
fi
bfdir=$1

if [ -f $LockFile ]
then
        touch $LockFile
fi

# Create the lock file
if ! lockfile -r 1 $LockFile #2>/dev/null
then
        echo "fail to lock $LockFile"
        rm -f $LockFile
        exit 2;
fi

echo "lock success"

if [ ! -d $bfdir ]
then
        mkdir -p $bfdir
        if [ ! -d $bfdir ]
        then
                echo "$bfdir can not create"
                rm -f $LockFile
                exit 3
        fi
fi

if [ -f /XXXXX/XXXX ]
then
        /XXXXX/XXXX -list > finddisk.txt
fi

if [ -f XXXX_mount_name.txt ]
then
        rm -rf XXXX_mount_name.txt
fi
touch XXXX_mount_name.txt

if [ -f hostdiskname.txt ]
then
        rm -rf hostdiskname.txt
fi
touch hostdiskname.txt

if [ -f listdevmountname.txt ]
then
        rm -rf listdevmountname.txt
fi
touch listdevmountname.txt

cat finddisk.txt | while read line
do
        first=`echo $line | awk '{print $1}'`
        if [ -z $first ]
        then
               continue
        fi   ##### this is used for up the empty line speed
        mount_name=`echo $line | awk '{ print $NF }'`

        if [ -z $mount_name ]
        then
                continue
        fi

        if [ "$bf_all" -eq "1" ]
        then
                tsta=`echo $mount_name | egrep '//iptv'`
        else
                XXX_x=`cat finddisk.txt | egrep "$bf_ip" | awk '{ print $(NF-1)}' | egrep "XXX" `
                tsta=`echo $line | egrep "$XXX_x"`
        fi
        echo "tsta is $tsta" >> listdevmountname.txt
        if [ -n "$tsta" ]
        then
                disk_name=`echo $line | awk '{print $1}'`
                XXX_no=`echo $line | awk '{print $4}'`
                dev_no=`echo $line | awk '{print $5}'`
                echo "mount_name ok is  $disk_name $mount_name $old_mount_name" >> listdevmountname.txt
                echo "$disk_name   $flr_no  $dev_no" >> hostdiskname.txt
                if [ "$mount_name" != "$old_mount_name" ]
                then
                        echo "$disk_name    $mount_name   $XXX_no  $dev_no" >> XXXX_mount_name.txt
                        old_mount_name=$mount_name
                fi
        fi
done

echo "df" > cmd.txt
echo "quit" >> cmd.txt

/XXXXX/etool < cmd.txt  > etooldf.txt
cat etooldf.txt | while read line
do
        mount_path=`echo $line | awk '{print $5}'`
        if [ "$mount_path" == "/" ]
        then
                mount_size=`echo $line | awk '{print $2}' | sed 's/[A-Z]/ /g'`
                echo "mountsize $mount_size G"
                needasize=`cat XXXX_mount_name.txt | wc -l`
                 if [ "$needasize" -eq "0" ]
                 then
                  echo "please check dev,maybe down "
                        exit 6
                fi
                needsize=$(( $needasize * 5 ))
                echo "needsize $needsize G"
                if [ $mount_size -le $needsize ]
                then
                        echo "need size > mount size no enough space"
                        rm -f $LockFile
                        exit 4
                fi
        fi
       
        mount_path=`echo $mount_path | awk '{print substr($1,1,5)}'`
        if [ "$mount_path" == "/XXXX" ]
        then
                echo "find XXXX not stop"
                exit 5
        fi
done
if [ "$?" -ne "0" ]
then
        rm -f $LockFile
        exit 5
fi

cat hostdiskname.txt | while read line
do
        disk_name=`echo $line | awk '{ print $1 }'`
        dd if=$disk_name skip=0 of=$disk_name seek=640 bs=1K count=240
        if [ "$?" -ne "0" ]
        then
                echo "dd hm hostdist disk_name $disk_name line $line err"
                rm -f $LockFile
                exit 5
                fi
done


cat XXXX_mount_name.txt | while read line
do
        disk_name=`echo $line | awk '{print $1}'`
        mount_name=`echo $line | awk '{print $2}'`
        mount_cl=`echo $mount_name | sed 's/////_/g'`
                echo "dd if=$disk_name of=$bfdir/$mount_cl.XXXX_bak bs=1M count=5120"
               dd if=$disk_name of=$bfdir/$mount_cl.XXXX_bak bs=1M count=5120
                if [ "$?" -ne "0" ]
                then
                        echo "dd hostdist $disk_name err"
                        rm -f $LockFile
                        exit 5
                fi
done
if [ "$?" -ne "0" ]
then
        rm -f $LockFile
        exit 5
fi

cat hostdiskname.txt | while read line
do
        disk_name=`echo $line | awk '{print $1}'`
        XXX_no=`echo $line | awk '{print $2}'`
        dev_no=`echo $line | awk '{print $3}'`
        #diskname_cl=`echo $line | sed 's/////_/g'`
        echo "dd if=$disk_name of=$bfdir/$XXX_no"_"$dev_no.XXXX_bak bs=1M count=1"
       dd if=$disk_name of=$bfdir/$XXX_no"_"$dev_no.XXXX_bak bs=1M count=1
        if [ "$?" -ne "0" ]
        then
                echo "dd hostdist $line err"
                rm -f $LockFile
                exit 5
        fi
done

rm -f $LockFile
exit $SUCCESS

 

 

******************************************************************

for arg in `/sbin/ifconfig | egrep 'inet addr' | awk '{ print $2 }' | awk -F: '{ print $2 }'`
do
 echo "arg is $arg"
 fscheckdir=`cat checktwoflrresult.txt | egrep "$arg" | awk '{ print $NF }' | sed -n 1p`
 
 
 fschecknum=`cat checktwoflrresult.txt | egrep "$arg" | awk '{ if(NF == "8" || NF == 5) {print $(NF-4)} else if(NF == 9 || NF ==6) {print $(NF-5)} }' | sed -n 1p`
 
 if [ -n "$fscheckdir" ]
 then
  echo "fscheckdir is $fscheckdir"
  echo "fschecknum is $fschecknum"
  break
 fi
done

 

*************************************************************************

dir=$1

if [ "$#" -ne "1" ]
then
#dir=`pwd`

dir="/XXXX"
fi

 

echo "ls  $dir" | ./xxxxx  > get_dir.txt
fileline=`cat get_dir.txt | wc -l`

 

cat get_dir.txt | while read line
do
        dirname=`echo $line | egrep "DIR" | awk '{ print $NF }'`
        if [ ${#dirname} -ge 1 ]    #${#变量名}:得到字符串长度
        then
                 echo "$dirname" >> dirname.txt
        fi
done

 

 

cat dirname.txt | while read line
do
        linkname=`echo $line`
        echo "ls $dir/$linkname/0" | ./XXXXX | grep "[LNK]" > linkname.txt  
        cat linkname.txt | while read line
        do  
         sed "s//[LNK/]/$modify_dir$zifu$linkname$zifu$shuzi/g" linkname.txt > link.txt
         sed "s/X////g" link.txt >> lin.txt
        done
done

 

你可能感兴趣的:(shell,脚本,UP,Path,disk)