获取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
#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
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
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
# 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
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