因为在编写shell的时候for循环是个比较重要的语法结构,经常会碰到所以想及时做个总结/备忘,方便以后查阅:
x.循环读取文件每一个非空行的内容
GITDIR="$HOME/git_s"
mkdir -p $GITDIR
cd $GITDIR
while read p; do
if [ -n "$p" ]; then
echo $p
git clone $p
fi
done < $HOME/github_links.txt
0.最简单/常用的通配符方式
for i in *.txt;do
mkdir ${i%.txt}; #取txt文件名来创建目录
( cd ${i%.txt} && wget -i ../$i --no-check-certificate );
done
1.简单的数字+1的形式
for((i=1; i<30; i++));do dt=$(date +%Y%m%d -d " $i days ago") echo $dt done
2.数组形式
COUNTRY=('cn' 'hk' 'tw')
for c in ${COUNTRY[@]}
do
wget "http://www.ipdeny.com/ipblocks/data/countries/$c.zone"
country_file=$c.zone
IPS=$($EGREP -v "^#|^$" $country_file)
for ip in $IPS
do
echo "blocking $ip"
iptables -A INPUT -s $ip -j DROP
done
done
3.嵌套循环
dirs=(papers tips tools news web pentesting database binary '%e8%bf%90%e7%bb%b4%e5%ae%89%e5%85%a8')
for dir in ${dirs[@]}
do
for i in $(seq 1 5)
do
if [ "$i" == 1 ]
then
echo http://drops.wooyun.org/category/$dir
else
echo http://drops.wooyun.org/category/$dir/page/$i
fi
done
done
echo '###################################################'
dirs=(papers tips tools news web pentesting database binary '%e8%bf%90%e7%bb%b4%e5%ae%89%e5%85%a8')
dir_num=${#dirs[@]}
for((i=0;i<$dir_num;i++))
do
for j in $(seq 1 5)
do
if [ "$j" == 1 ]
then
echo http://drops.wooyun.org/category/${dirs[i]}
else
echo http://drops.wooyun.org/category/${dirs[i]}/page/$j
fi
done
done
暂时就差不这些,以后有别的tips再及时补充。
求一个shell循环_以获取03年至今的每一天日期
如题:
date=`date +%Y%m%d -d ” 1 days ago”`
把里面的1变为一个变量,由1-800循环,这样就实现了一个期间日期的循环显示,大概就是这样一个意思。
k=1
while((k++<=800));do
echo `date +%Y%m%d -d " $k days ago"`
done
-----
for((i=1;i<800;i++));do
echo $i
dt=$(date +%Y%m%d -d ' -'$i' day')
echo $dt
done
经过测试之后OK!
#!/bin/bash
for((i=0; i<10; i++));do
echo $(expr $i * 4)
done
for i in $(seq 10);do
echo $i
done
i=1
while(($i<10));do
echo $i
i=`expr $i + 1`
done
for i in {1..10};do
echo $i
done
for i in {01..10};do
echo $i
done
for i in {a..z};do
echo $i
done
for i in $(ls);do
echo $i
done
for i in `ls`;do
echo $i
done
for i in f1 f2 f3;do
echo $i
done
cat /tmp/drops.txt | while read url title
do
title=$(echo $title | tr ' </' '_')
curl -s $url | sed -n '/<div id="content">/,/entry-tags/p' >/tmp/tmp.html
cat /tmp/top.html /tmp/tmp.html /tmp/bottom.html >/tmp/$title.html
done
while read url title
do
title=$(echo $title | tr ' </' '_')
curl -s $url | sed -n '/<div id="content">/,/entry-tags/p' >/tmp/tmp.html
cat /tmp/top.html /tmp/tmp.html /tmp/bottom.html >/tmp/$title.html
done < /tmp/drops.txt
####
dir=(papers tips tools news web pentesting database binary)
dir_num=${#dir[@]}
for((i=0;i<$dir_num;i++))
do
for j in $(seq 1 15)
do
if [ "$j" == 1 ]
then
echo http://drops.wooyun.org/category/${dir[i]}
else
echo http://drops.wooyun.org/category/${dir[i]}/page/$j
fi
done
done
####
arr=("a" "b" "c")
echo "arr is (${arr[@]})"
arr=(a b c)
for i in ${arr[@]}
do
echo $i
done
echo "参数,$*表示脚本输入的所有参数:"
for i in $* ; do
echo $i
done
EGREP=/bin/egrep
COUNTRY=('cn' 'hk' 'tw')
for c in ${COUNTRY[@]}
do
wget "http://www.ipdeny.com/ipblocks/data/countries/$c.zone"
country_file=$c.zone
IPS=$($EGREP -v "^#|^$" $country_file)
for ip in $IPS
do
echo "blocking $ip"
iptables -A INPUT -s $ip -j DROP
done
done
# SHELL数组详解
# http://ixyzero.com/blog/archives/178.html
$ A=(a b c def)
$ echo ${A[@]} #取全部元素
a b c def
$ echo ${A[0]} #取第一个元素
a
$ echo ${#A[@]} #取得数组元素的个数
4
$ echo ${#A[3]} #取得第4个元素的长度
3
Bash中for/while的无限循环
搜索关键字:linux bash while infinite loop
参考解答:
- http://unix.stackexchange.com/questions/97059/why-does-while-0-go-into-infinite-loop
- http://www.cyberciti.biz/faq/bash-infinite-loop/
#!/bin/bash while :; do echo "Press [CTRL+C] to stop.." sleep 1 done while true; do echo "Press [CTRL+C] to stop.." sleep 1 done while (( 1 )); do echo "Press [CTRL+C] to stop.." sleep 1 done for (( ; ; )); do echo "Pres CTRL+C to stop..." sleep 1 done
=EOF=
《 “用shell编写for/while循环” 》 有 3 条评论
如何用Bash循环读取文件中每一行的内容?
`filename=’peptides.txt’
while read p; do
echo $p
done < $filename
`
或从文件描述符中进行读取:
`filename='peptides.txt'
exec 4<$filename
while read -u4 p ; do
echo $p
done
`
http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
shell下同时读取多个文件的方法
http://yaronspace.cn/blog/archives/1473
http://www.linuxjournal.com/content/reading-multiple-files-bash
BASH Shell: For Loop File Names With Spaces #Using old good find command to process file names with spaces
https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
Bash script error with strings with paths that have spaces and wildcards
https://unix.stackexchange.com/questions/156534/bash-script-error-with-strings-with-paths-that-have-spaces-and-wildcards/607424#607424
Bash script to cd to directory with spaces in pathname
https://stackoverflow.com/questions/589149/bash-script-to-cd-to-directory-with-spaces-in-pathname