bash 脚本exercise
1 #!/bin/env sh
2
3 declare -i max=0
4 declare -i min=32765
5 declare -a zz
6
7 for x in `seq 0 1 9`;do
8 zz[x]=$RANDOM
9 [ ${zz[x]} -gt $max ] && max=${zz[x]}
10 [ ${zz[x]} -lt $min ] && min=${zz[x]}
11 done
12
13 echo ${zz[@]}
14 echo $max,$min
1 #!/bin/env sh
2
3 declare -a bb
4 declare -i sum=0
5
6 bb=(/var/log/*.log)
7
8 for x in `seq 0 2 $[${#bb[*]}-1]`;do
9 let sum+=`cat ${bb[x]}|wc --lines`
10 done
11
12 echo $sum
1 #!/bin/env sh
2
3 declare -a pp
4 declare -i sum
5
6 pp=(/var/log/*.log)
7
8 for b in `seq 0 1 $[${#pp[*]}-1]`;do
9 if [ $[b%2] -ne 0 ];then
10 continue
11 fi
12
13 let sum+=`wc --lines ${pp[b]}|awk '{print $1}'`
14 done
15
16 echo ${pp[*]},$sum
${bb[*]:5:3}
数组切片${array[@]:offset:number}
数组亦支持反向索引
echo ${array[-1]}
echo ${array[-2]}
追加元素
array[${#array[*]}]="variable"
删除数组元素
unset array[index]
关联数组
declare -A array_name
显示最后一个磁盘信息
1 #!/bin/env sh
2
3 disks=$(fdisk -l /dev/[sh]d[a-z] | grep -o '^Disk /dev/[sh]d[a-z]' | wc --lines)
4
5 if [[ $disks -eq 1 ]];then
6 fdisk -l /dev/[sh]da
7 else
8 fdisk -l $(fdisk -l /dev/[sh]d[a-z] |grep -o '^Disk /dev/[sh]d[a-z]'|tail -1|cut -d' ' -f2)
9 fi
determine root:
#!/bin/env sh
function isRoot(){
if [ `echo $UID` -ne 0 ];then
return 5
else
return 0
fi
}
isRoot
if [ $? -ne 0 ];then
echo "Must be root to execute this script"
exit 5
else
echo "Welcom root"
exit 0
fi
nmap-ncat:
Usage: ncat [options] [hostname] [port]
#!/bin/env sh
#cat ip.txt|while read line;do
while read line
do
nc -zw 2 $line > /dev/null 2>&1
if [ $? -eq 0 ];then
echo -e "\033[32;7m$line:success\033[0m"
else
echo -e "\033[31;7m$line:fail\033[0m"
fi
done < $1
~
192.168.31.101 22
192.168.31.102 22
jd.com 443
jd.com 80