SHELL Usage four (for statement)

1, SHELL statement Programming For the real case
1) Fixed For statement syntax:
I the For in (expression)
 do 
action 1 
DONE

 

2) For cases of operating a statement:
#!/bin/bash
for NUM in 1 2 3 4
do
echo “$NUM) www.jd.com”
done

 

3) For operating statement Case II:
#!/bin/bash
for NUM in $(echo {1..4})
do
echo “$NUM) www.jd.com”
done

 

4) For operating the three-sentence cases:
#!/bin/bash
COMMAND="$*"for IP in 127.0.0.{1..10}
do
echo -e "\033[32mThe $IP Exec Command:\033[0m"
ssh -l root $IP "$COMMAND"
scp -r jfedu.txt [email protected]$IP:/data/backup/
done

 

5) For the case of operating a four statements:
#!/bin/bash
for LOG in `ls access_2019*`
do
echo $LOG
tar -czvf ${LOG}.tar.gz $LOG
if [ ! -d /data/backup/`date +%F` ];then
mkdir -p /data/backup/`date +%F`
fi
mv ${LOG}.tar.gz /data/backup/`date +%F`
rm -rf $LOG
done

 

6) For operating statement Case Five:
#!/bin/bash
for LOG in `ls /data/backup/2019-07-06/*`
doecho $LOG
tar -xzf $LOG -C /data/logs/linux_web/
done

 

7) For printing cycle wait 10 seconds Tips:
for ((j=0;j<=10;j++))
do
echo -ne "\033[32m-\033[0m"
sleep 1
done
echo

 

2, SHELL statement programming While the real case
1) fixed While statement syntax:
While (expression | condition)
 do 
action 1 
DONE

 

(1) Print BAT Company Website cycle, read or read instruction for reading row variables:
#!/bin/bash
#By author jfedu.net 2017
while read LINE
do
echo $LINEdone <jfedu.txt

 

Jfedu.txt which reads:
www.baidu.com
www.taobao.com
www.qq.com
(2) While output per infinite Hello World:
#!/bin/bash
#By author jfedu.net 2017
while sleep 1
do
echo -e "\033[32mHello World.\033[0m"
done

 

(3) the print cycle number from 1 to 100, arithmetic logic means for expr:
#!/bin/bash
#By author jfedu.net 2017
i=0
while ((i<=100))
do
echo $i
i=`expr $i + 1`
done

 

(4) While the total cycle required 1-100:! # / Bin / bash
#By author jfedu.net 2017
#auto sum 1 100
j=0
i=1
while ((i<=100))
do
j=`expr $i + $j`
((i++))
done
echo $j

 

(5) While loop to read the file line by line:
#!/bin/bash
#By author jfedu.net 2017
while read line
do
echo $line;
done < /etc/hosts

 

(6) While loop 100 determines if the input number is greater than (similar correctness IP), is less than a
Straight prompt input, until it is correct:
#!/bin/bash
while truedo
read -p "Please input A Number:" INPUT
if [ "$INPUT" -gt 100 ];then
echo $INPUT
break;
fi
done

 

(7) is determined every 5 seconds cycles / etc / passwd whether illegal modification:
#!/bin/bash
#Check File to change.
#By author jfedu.net 2017
FILES="/etc/passwd"
while true
do
echo "The Time is `date +%F-%T`"
OLD=`md5sum $FILES|cut -d" " -f 1`
sleep 5
NEW=`md5sum $FILES|cut -d" " -f 1`
if [[ $OLD != $NEW ]];then
echo "The $FILES has been modified."
fi
done

 

(8) per 10 seconds is determined whether the user login jfedu:
#!/bin/bash
while true
do
USER=$*
echo -e "\033[32m`date`\033[0m"
for i in $USER
do
NUM=$(who|grep $i|wc -l)
if [ $NUM -ne 0 ];then
echo "The $i is Login Linux System."
fi
done
echo "--------------------"
sleep 10
done

 

3, SHELL programming combat MYSQL database backup script
 backup script backup any single library;
#!/bin/bash
#2019年7月6日15:54:03
#auto backup mysql database.
#by author www.jfedu.net####################
SQL_USR="backup"
SQL_PWD="backup"
SQL_DB="$1"
SQL_DIR="/data/backup/$(date +%F)"
SQL_CMD="/usr/bin/mysqldump"
if [ $# -eq 0 -o "$1" == "help" ];then
echo -e "\033[32m----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0
jfedu1|www|jfedu2|help}\033[0m"
exit 1
fi
if [ ! -d $SQL_DIR ];then
mkdir -p $SQL_DIR
fi
$SQL_CMD -u$SQL_USR -p$SQL_PWD $SQL_DB >
$SQL_DIR/${SQL_DB}.sql
if [ $? -eq 0 ];then
echo -e "\033[32mThe MYSQL $SQL_DB backup
success.\033[0m"
echo "----------------"
echo "ls -l $SQL_DIR/"ls -l $SQL_DIR/
else
rm -rf $SQL_DIR/${SQL_DB}.sql
echo -e "\033[32mThe MYSQL $SQL_DB backup
Failed.\033[0m"
exit
fi

 

 backup scripts to achieve any number of backup;
#!/bin/bash
#2019年7月6日15:54:03
#auto backup mysql database.
#by author www.jfedu.net
####################
SQL_USR="backup"
SQL_PWD="backup"
SQL_DB="$*"
SQL_DIR="/data/backup/$(date +%F)"
SQL_CMD="/usr/bin/mysqldump"
if [ $# -eq 0 -o "$1" == "help" ];then
echo -e "\033[32m----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0
jfedu1|www|jfedu2|help}\033[0m"exit 1
fi
if [ ! -d $SQL_DIR ];then
mkdir -p $SQL_DIR
fi
for SQL_DB in `echo $SQL_DB`
do
$SQL_CMD -u$SQL_USR -p$SQL_PWD $SQL_DB >
$SQL_DIR/${SQL_DB}.sql
if [ $? -eq 0 ];then
echo -e "\033[32mThe MYSQL $SQL_DB backup
success.\033[0m"
echo "----------------"
echo "ls -l $SQL_DIR/"
ls -l $SQL_DIR/
else
rm -rf $SQL_DIR/${SQL_DB}.sql
echo -e "\033[32mThe MYSQL $SQL_DB backup
Failed.\033[0m"
exit
fi
done

 

 backup script backup of all databases;
#!/bin/bash
#2019年7月6日15:54:03
#auto backup mysql database.
#by author www.jfedu.net
#########################
SQL_USR="backup"
SQL_PWD="backup"
SQL_DB="$*"
SQL_DIR="/data/backup/$(date +%F)"
SQL_CMD="/usr/bin/mysqldump"
if [ $# -eq 0 -o "$1" == "help" ];then
echo -e "\033[32m----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0
jfedu1|www|jfedu2|help}\033[0m"
exit 1
fi
if [ ! -d $SQL_DIR ];then
mkdir -p $SQL_DIR
fi
for SQL_DB in `echo $SQL_DB`do
if [ "$SQL_DB" == "all" ];then
$SQL_CMD -u$SQL_USR -p$SQL_PWD
--all-databases > $SQL_DIR/${SQL_DB}.sql
if [ $? -eq 0 ];then
echo -e "\033[32mThe MYSQL $SQL_DB
backup success.\033[0m"
echo "----------------"
echo "ls -l $SQL_DIR/"
ls -l $SQL_DIR/
continue
else
rm -rf $SQL_DIR/${SQL_DB}.sql
echo -e "\033[32mThe MYSQL $SQL_DB
backup Failed.\033[0m"
exit
fi
fi
$SQL_CMD -u$SQL_USR -p$SQL_PWD $SQL_DB >
$SQL_DIR/${SQL_DB}.sql
if [ $? -eq 0 ];then
echo -e "\033[32mThe MYSQL $SQL_DB backupsuccess.\033[0m"
echo "----------------"
echo "ls -l $SQL_DIR/"
ls -l $SQL_DIR/
else
rm -rf $SQL_DIR/${SQL_DB}.sql
echo -e "\033[32mThe MYSQL $SQL_DB backup
Failed.\033[0m"
exit
fi
done

 

4, SHELL programming combat Linux systems IP modify the script
 dynamic IP can be modified as a static IP;
#!/bin/bash
#2019年7月6日16:51:27
#auto change server ip.
#by author www.jfedu.net
####################
ETH_IPADDR="$1"
GATEWAY="10.10.10.1"
DNS_SERVER="8.8.8.8"ETH_NAME="ifcfg-ens33"
NETMASK="255.255.255.0"
ETH_BAK="/data/backup/`date +%F`"
ETH_DIR="/etc/sysconfig/network-scripts"
if [ $# -eq 0 ];then
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0
10.10.10.111|1.1.1.1|help}\033[0m"
exit 1
fi
if [ ! -d $ETH_BAK ];then
mkdir -p $ETH_BAK
fi
cd $ETH_DIR
cp $ETH_NAME $ETH_BAK
grep "dhcp" $ETH_NAME
if [ $? -eq 0 ];then
echo "TYPE=\"Ethernet\"
BOOTPROTO=\"static\"
NAME=\"ens33\"
DEVICE=\"ens33\"
ONBOOT=\"yes\"">$ETH_NAMEecho "IPADDR=$ETH_IPADDR" >>$ETH_NAME
echo "NETMASK=$NETMASK" >>$ETH_NAME
echo "GATEWAY=$GATEWAY" >>$ETH_NAME
echo "DNS1=$DNS_SERVER" >>fiETH_NAME $

 

 itself can achieve a static IP, modify other static IP;
#!/bin/bash
#2019年7月6日16:51:27
#auto change server ip.
#by author www.jfedu.net
####################
ETH_IPADDR="$1"
GATEWAY="10.10.10.1"
DNS_SERVER="8.8.8.8"
ETH_NAME="ifcfg-ens33"
NETMASK="255.255.255.0"
ETH_BAK="/data/backup/`date +%F`"
ETH_DIR="/etc/sysconfig/network-scripts"
if [ $# -eq 0 ];then
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0
10.10.10.111|1.1.1.1|help}\033[0m"exit 1
fi
if [ ! -d $ETH_BAK ];then
mkdir -p $ETH_BAK
fi
cd $ETH_DIR
cp $ETH_NAME $ETH_BAK
echo "TYPE=\"Ethernet\"
BOOTPROTO=\"static\"
NAME=\"ens33\"
DEVICE=\"ens33\"
ONBOOT=\"yes\"">$ETH_NAME
echo "IPADDR=$ETH_IPADDR" >>$ETH_NAME
echo "NETMASK=$NETMASK" >>$ETH_NAME
echo "GATEWAY=$GATEWAY" >>$ETH_NAME
echo "DNS1=$DNS_SERVER" >>$ETH_NAME

 

Analyzing  IP user input is correct;
#!/bin/bash
#2019年7月6日16:51:27
#auto change server ip.
#by author www.jfedu.net
####################GATEWAY="10.10.10.1"
DNS_SERVER="8.8.8.8"
ETH_NAME="ifcfg-ens33"
NETMASK="255.255.255.0"
ETH_BAK="/data/backup/`date +%F`"
ETH_DIR="/etc/sysconfig/network-scripts"
if [ ! -d $ETH_BAK ];-mkdirthen
p $ETH_BAK
fi
while true
do
read -p "Please Input Server IPaddr: " ETH_IPADDR
if [ ! -z $ETH_IPADDR ];then
IP1=`echo $ETH_IPADDR|cut -d "." -f 1`
IP2=`echo $ETH_IPADDR|cut -d "." -f 2`
IP3=`echo $ETH_IPADDR|cut -d "." -f 3`
IP4=`echo $ETH_IPADDR|cut -d "." -f 4`
if [ $IP1 -ge 1 -a $IP1 -le 255 -a $IP2 -ge 0 -a $IP2 -le 255 -a $IP3 -ge 0
-a $IP3 -le 255 -a $IP4 -ge 0 -a $IP1 -le 255 ];then
ETH_IPADDR=$ETH_IPADDR
break
fifi
done
cd $ETH_DIR
cp $ETH_NAME $ETH_BAK
echo "TYPE=\"Ethernet\"
BOOTPROTO=\"static\"
NAME=\"ens33\"
DEVICE=\"ens33\"
ONBOOT=\"yes\"">$ETH_NAME
echo "IPADDR=$ETH_IPADDR" >>$ETH_NAME
echo "NETMASK=$NETMASK" >>$ETH_NAME
echo "GATEWAY=$GATEWAY" >>$ETH_NAME
echo "DNS1=$DNS_SERVER" >>$ETH_NAME
if [ $? -eq 0 ];then
echo
echo "cat $ETH_DIR/$ETH_NAME"
cat $ETH_DIR/$ETH_NAME
echo "----------------"
echo -e "\033[32mThe Server IPaddr config success.\033[0m"
else
echo
echo -e "\033[32mThe Server IPaddr config Failed.\033[0m"exit
fi

 

Guess you like

Origin www.cnblogs.com/nsh123/p/12157301.html