方法一
array=(I am oldboy teacher welcome to oldboy training class)
for word in ${array[*]}
do
if [ ${#word} -le 6 ]
then
echo $word
fi
done
echo ===end
for((i=0;i<${#array[*]};i++))
do
if [ ${#array[i]} -le 6 ]
then
echo ${array[i]}
fi
done
方法二
#!/bin/bash
array=(I am oldboy teacher welcome to oldboy training class)
for i in ${array[*]}
do
if [ ${#i} -le 6 ]
then
echo $i
fi
done
方法三
#!/bin/bash
arr=(I am oldboy teacher welcome to oldboy training class)
for n in ${arr[*]}
do
if [ ${#n} -le 6 ]
then
echo $n
fi
done
方法四
number=(I am oldboy teacher welcome to oldboy training class)
for ((i=0;i<${#number[*]};i++))
do
if [ "${#number[$i]}" -le 6 ];then
echo ${number[$i]}
fi
done
方法一
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
arr=(
http://www.baidu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
usage(){
if [[ ! $1 =~ http://.* ]]
then
echo "Usage:$0 http://www.xx.com"
exit 1
fi
}
check_url(){
wget -q -o /dev/null --spider -T 15 --tries=2 $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
action "$1 is ok." /bin/true
else
action "$1 is no." /bin/false
fi
}
piliang(){
for url in ${arr[*]}
do
usage $url
check_url $url
done
}
main(){
while true
do
piliang
echo ====================
sleep 5
done
}
main
方法二
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
check=0
a=(
http://blog.oldboyedu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
https://www.qq.com
http://www.baidu.com
)
jian(){
for ((i=0;i<${#a[*]};i++))
do
wget -T 3 ${a[i]} &>/dev/null
if [ $? -eq 0 ]
then
action "${a[i]} is ok." /bin/true
else
action "${a[i]} is error." /bin/false
fi
done
((check++))
}
main(){
while true
do
jian
echo "=================check count:${check}================="
sleep 5
done
}
main
方法三
#!/bin/bash
. /etc/init.d/functions
check_count=0
url_list=(
http://www.baidu.com
http://www.qq.com
http://oldboy.blog.51cto.com
http://10.0.0.7
)
int() {
b=''
for ((i=0;$i<=100;i+=2))
do
printf "progress: [%-50s] %d%%\r" $b $i
sleep 0.1
b+='#'
done
echo
}
count_down() {
echo -n "请等待......"
sleep 1
int
}
check_url() {
count_down
for ((i=0;i<`echo ${#url_list[*]}`;i++))
do
curl ${url_list[$i]} &>/dev/null
if [ $? -eq 0 ]
then
action "${url_list[$i]} is OK" /bin/true
else
action "${url_list[$i]} is NO" /bin/false
fi
done
((check_count++))
}
main() {
while true
do
check_url
echo "等待在次测试......"
sleep 5
done
}
main
方法四(精简版)
#!/bin/sh
arr=(
http://www.baidu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
while true
do
for url in ${arr[*]}
do
wget -q -o /dev/null --spider -T 15 --tries=2 $url &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
echo "$url is ok."
else
echo "$url is no."
fi
done
sleep 5
done
文件重命名问题案例
方法一
for file1 in `ls *.html`
do
mv $file1 `echo $file1|awk -F "[_.]" '{print "oldgirl_"$2".HTML"}'`
done
方法二
Path=/oldboy
[ -d $Path ] || mkdir $Path
for n in {1..10}
do
random=`openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 2-11`
touch $Path/oldboy_${random}.html
done
方法三
#!/bin/sh
Dirname="/oldboy"
cd $Dirname||exit 1
for OldFileName in `ls`
do
NewFileName=$(echo $OldFileName|awk -F [_.] '{print "oldgirl_"$2".HTML"}')
mv $OldFileName ${NewFileName}
done
url监控案例
方法一
#!/bin/sh
arr=(
http://www.baidu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
while true
do
for url in ${arr[*]}
do
wget -q -o /dev/null --spider -T 15 --tries=2 $url &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
echo "$url is ok."
else
echo "$url is no."
fi
done
sleep 5
done
方法二
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
arr=(
http://www.baidu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
usage(){
if [[ ! $1 =~ http://.* ]]
then
echo "Usage:$0 http://www.xx.com"
exit 1
fi
}
check_url(){
wget -q -o /dev/null --spider -T 15 --tries=2 $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
action "$1 is ok." /bin/true
else
action "$1 is no." /bin/false
fi
}
piliang(){
for url in ${arr[*]}
do
usage $url
check_url $url
done
}
main(){
while true
do
piliang
echo ====================
sleep 5
done
}
main
加密与解密
#!/bin/sh
array=( #<==把待比较的字符串信息放到数组里。
21029299
00205d1c
a3da1677
1f6d12dd
890684b
)
Path=/tmp/md5.txt
funGetMd5() {
> $Path
for ((Num=0;Num<=32767;Num++))
do
{
Stat=$(echo $Num|md5sum)
echo "$Stat $Num" >> $Path #<==建立数字和md5sum后的内容对应关系。
}&
done
}
funFindMd5() {
word=$(echo "${array[@]}"|sed -r 's# |\n#|#g') #<==取出所有数组元素并用|分隔开。
grep --color=auto -E "$word" $Path #<==同时过滤包含所有不同字符串的内容。
}
funcMain(){
funGetMd5
funFindMd5
}
funcMain
求和案例
a=`awk '{print $10}' /root/access_2013_05_30.log` >/tmp/num.txt
sum=0
for n in $a
do
if [ $n == "-" ]
then
continue
fi
let sum=sum+n
done
echo $sum
批量创建用户案例
方法一
user="oldboy"
passfile="/tmp/user.log"
for num in `seq -w 10`
do
useradd $user$num
pass="`echo "test$RANDOM"|md5sum|cut -c3-11`"
echo "$pass"|passwd --stdin $user$num
echo -e "user:$user$num\tpasswd:$pass">>$passfile
done
echo ------------this is zwb trainning class contents-------------
cat $passfile
方法二
. /etc/init.d/functions
user="oldboy"
passfile="/tmp/user.log"
for num in `seq -w 11 15`
do
pass="`echo "test$RANDOM"|md5sum|cut -c3-11`"
useradd $user$num &>/dev/null &&\
echo "$pass"|passwd --stdin $user$num &>/dev/null &&\
echo -e "user:$user$num\tpasswd:$pass">>$passfile
if [ $? -eq 0 ]
then
action "$user$num is ok" /bin/true
else
action "$user$num is fail" /bin/false
fi
done
echo ---------------------------------
cat $passfile && >$passfile
方法三
. /etc/init.d/functions
user="xiaoting"
passfile="/tmp/user.log"
for num in `seq -w 10`
do
pass="`echo "test$RANDOM"|md5sum|cut -c3-11`"
useradd $user$sum &>/dev/null &&\
echo -e "$user${num}:$pass">>$passfile
if [ $? -eq 0 ]
then
action "$user$sum is ok" /bin/true
else
action "$user$sum is fail" /bin/false
fi
done
echo ------------------------------------------------
chpasswd < $passfile
cat $passfile && >$passfile
方法四
#将$()括号里面的内容传递给变量user
for user in $(echo oldboy{01..10}|xargs -n1)
do
#将密码传递给变量pass
pass=$(echo $RANDOM|md5sum|cut -c 1-8)
#创建用户
useradd $user
#给用户修改密码
echo "$pass"|passwd --stdin $user
#把创建的用户及密码追加重定向到文件里面
echo "$user:$pass" >> /tmp/user.txt
done
密码md5加密
for n in {0..32767}
do
echo -n "$n " >>/tmp/md5sum.txt
echo $n|md5sum >>/tmp/md5sum.txt
done
grep "4fe8bf20ed" /tmp/md5sum.txt
文件改名案例
方法一
Path=/oldboy
[ -d $Path ] || mkdir $Path
cd $Path
a=`ls *.html|wc -l` >/dev/null
for file in `ls`
do
if [ $a -ne 0 ]
then
echo `ls *.html|awk -F '_' '{print "mv",$0,$1"_oldgirl.HTML"}'|bash` >/dev/null
else
echo `ls *.HTML|awk -F '_' '{print "mv",$0,$1"_oldboy.html"}'|bash` >/dev/null
fi
done
方法二
[ d /oldboy ] && rm -rf /oldboy
[ d /oldboy ] || mkdir /oldboy
cd /oldboy
for n in `seq 10`
do
name=`openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 2-11`
touch ${name}_oldboy.html
done
密码破解
for n in {0..32767}
do
echo "`echo $n|md5sum` $n" >>/tmp/zhiwen.log
done
>/tmp/zhiwen.log
for n in {0..32767}
do
echo "`echo $n|md5sum` $n" >>/tmp/zhiwen.log
done
md5char="4fe8bf20ed"
while read line
do
if [ `echo $line|grep "$md5char"|wc -l` -eq 1 ]
then
echo $line
break
fi
done </tmp/zhiwen.log