1+2+..+100求和
+--------------+
| for循环 |
+--------------+
sum=0
for((i=1;i<=100;i++))
do
sum=$((i+$sum))
done
echo $sum
+--------------+
| while循环 |
+--------------+
sum=0
i=1
while ((i<=100))
do
((sum+=i))
((i++))
done
echo $sum
+--------------+
| 数学公式 |
+--------------+
i=100
echo $((sum=i*(i+1)/2))
+--------------+
| awk |
+--------------+
seq 100 | awk '{i+=$1}END{print i}'
批量生成8位随机数文件
for((i=1;i<=10;i++))
do
touch `echo $RANDOM | md5sum | cut -c 1-8`.txt
done
批量修改文件名
ll
-rw-r--r-- 1 root root 0 Jul 31 06:57 01508427.txt
-rw-r--r-- 1 root root 0 Jul 31 06:57 177ad50a.txt
-rw-r--r-- 1 root root 0 Jul 31 06:57 764f1747.txt
-rw-r--r-- 1 root root 0 Jul 31 06:57 79205720.txt
-rw-r--r-- 1 root root 0 Jul 31 06:57 8d54c065.txt
-rw-r--r-- 1 root root 0 Jul 31 06:57 97026f8e.txt
+--------------+
| sed |
+--------------+
for i in `ls *.txt`
do
mv $i `echo $i | sed "s@txt@sh@g"`
done
+--------------+
| awk |
+--------------+
for i in `ls *.txt`
do
awk -F. '{print "mv "$0,$1".sh"}'
done
+--------------+
| rename |
+--------------+
for i in `ls *.txt`
do
rename ".txt" ".sh" $i;
done
关闭服务开机自启
chkconfig --list | grep 3:on | awk '{print $1}' | \
egrep -v "rsyslog | sshd | sysstst" | sed -r 's@(.*)@chkconfig \1 off@g' | bash
批量生成用户并设置随机数密码
for i in `seq 10`
do
RAND=`openssl rand -hex 10`
useradd test$i && \
echo $RAND | passwd --stdin test$i && \
echo -e "user: test$i \t passwd: $RAND" >> /tmp/user.log
done
判断网段IP在线情况
for i in `seq 254`
do
ping -c2 172.16.100.$i &> /dev/null
[ $? -eq 0 ] && echo "172.16.100.$i is up" >> /tmp/ip_up.log \
|| echo "172.16.100.$i is down" >> /tmp/ip_down.log
done
利用数组输出IP
array=(
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
)
for ip in ${array[*]}
do
echo $ip
done
-----------
for ((i=0;i<${#array[@]};i++))
do
echo ${array[i]}
done