从网上收集而来,自己学习的同时也可以作为一个备份以供随时参考:
if … then语句
if [ test_command ] then commands fi
if … then … else语句
if [ test_command ] then commands else commands fi
if … then … elif … (else)语句
if [ test_command ] then commands elif [ test_command ] then commands ... ... else (optional) commands fi
for … in语句
for 变量 in 变量列表 do commands done
while语句
while 条件为真 do commands done
until语句
until 条件为真 do commands done
case语句
case $variable in match_1) commands_to_execute_for_1 ;; match_2) commands_to_execute_for_2 ;; match_3) commands_to_execute_for_3 ;; ... ... *) (可选 - any other value) commands_to_execute_for_no_match ;; esac
Shell循环
#数字段形式
for i in {1..10}
do
echo $i
done
#详细列出(字符且项数不多)
for File in 1 2 3 4 5
do
echo $File
done
#对存在的文件进行循环
for shname in `ls *.sh`
do
name=`echo “$shname” | awk -F. ‘{print $1}’`
echo $name
done
#查找循环(ls数据量太大的时候也可以用这种方法)
for shname in `find . -type f -name “*.sh”`
do
name=`echo “$shname” | awk -F/ ‘{print $2}’`
echo $name
done
#(())语法循环–有点像C语法,但记得双括号
for((i=1;i<100;i++))
do
if((i%3==0))
then
echo $i
continue
fi
done
#seq形式 起始从1开始
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
#while循环注意为方括号[],且注意空格
min=1
max=100
while [ $min -le $max ]
do
echo $min
min=`expr $min + 1`
done
#双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+1))
i=1
while(($i<100))
do
if(($i%4==0))
then
echo $i
fi
i=$(($i+1))
done
整体示例如下:
#!/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
《“SHELL脚本中常用的控制结构 & Shell循环”》 有 1 条评论
Shell脚本编程总结及速查手册
https://ghui.me/post/2016/06/shell-handbook/
http://www.code123.cc/2648.html
https://github.com/qinjx/30min_guides/blob/master/shell.md