各种shell脚本学习

获取pid并关闭它

#! /bin/bash
#filename=(`ls`)
#for var in ${filename[@]};do
#echo $var
#done
var=(`ps -aux | grep /usr/sbin/cupsd`)
#a=($echo($var))
echo ${var[1]}
#OLD_IFS="$IFS" 
#IFS=" " 
#arr=($var) 
#IFS="$OLD_IFS" 
#for s in ${arr[@]} 
#do 
#    echo "$s" 
#done
kill -9 ${var[1]}

遍历数组找到特定字符串

var=(`netcfg`)
len=${#var[@]}
#echo $len
i=0
for s in ${var[@]} 
do
#echo $s
case $s in
'ppp0')
ppp=${var[i+1]}
#echo $ppp
;;
'ccmni0')
ccmni0=${var[i+1]}
#echo $ccmni0
;;
'ccmni1')
ccmni1=${var[i+1]}
#echo $ccmni1
;;
'ccmni2')
ccmni2=${var[i+1]}
#echo $ccmni2
;;
esac
#使数据自加
let i+=1
done

读文件

BAKIFS=$IFS                    # save current input field separator
IFS=$(echo -en "\n\b")         #   and change it.

exec 3<&0                      # save current stdin
exec 0<"$FILE"                 #   and change it to read from file.

while read -r line ; do        # read every line from stdin (currently file).
  processLine $line            #   and process it.
done

exec 0<&3                      # restore previous stdin.
IFS=$BAKIFS                    #   and IFS.
That file handle belongs to the process, it's not a system-wide resource. For example, every process gets its own standard input (file handle 0) without conflicting with other processes. So, if you're talking about programs running separate from that one, no. Of course, within the process, you couldn't (for example) have another line reading from the "real" stdin within the while loop since it would get the next line from the file instead).

### Main script stars here ###
# Store file name
file=/data/tgt/conf
#file=$1
i=0
while IFS=: read -r name url 
do
case $name in
"domain")
if [ $i == 0 ]; then
iptables -N tgt_FORWARD
iptables -I FORWARD -j tgt_FORWARD
let i+=1
fi
iptables -A tgt_FORWARD -m string --string $url --algo bm -j DROP
;;
"ip")
iptables -A tgt_FORWARD -d $url -j DROP
;;
"withdraw")
#iptables -D FORWARD 1
echo withdraw
iptables -F tgt_FORWARD
exit 0
;;
esac
done < "$file"
#test if null
#[[ -n $name ]] && echo $name $url 
if [[ -n $name ]]; then
echo lastline
case $name in
"domain")
if [ i == 0 ]; then
iptables -N tgt_FORWARD
iptables -I FORWARD -j tgt_FORWARD
let i+=1
fi
iptables -A tgt_FORWARD -m string --string $url --algo bm -j DROP
;;
"ip")
iptables -A tgt_FORWARD -d $url -j DROP
;;
"withdraw")
iptables -D FORWARD 1
iptables -F tgt_FORWARD
exit 0
;;
esac
fi
exit 0

循环、break

#! /system/bin/sh
while true
do
find_ppp=0
i=0
var=(`netcfg`)
for s in ${var[@]} 
do
if [ $s == "ppp0" ]
then 
ppp_status=${var[i+1]}
find_ppp=1
#echo $s $ppp_status
break
fi
#echo $s
let i+=1
done
if [ $find_ppp -eq 1 ] && [ "$ppp_status" == "UP" ]; then
#echo UP
break
elif [ $find_ppp -eq 0 ]; then
#echo not find ppp0
pppd /dev/ttyMT1 921600 xonxoff defaultroute usepeerdns nocrtscts noauth nodetach debug 192.168.10.2:192.168.10.1 &
sleep 5
else
sleep 5
fi
done
while read myline
do
    $myline
done < /data/tgt/conf


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值