1、 写一个脚本:
1)设定变量FILE的的值为/etc/passwd
2) 使用循环读取文件/etc/passwd 的第2,4,6,10,13,15行,并显示其内容;
3)把这些行保存至/tmp/passwd文件中
#!/bin/bash
#
FILE='/etc/passwd'
for LINE in 2 3 6 10 13 15; do
head -$LINE $FILE | tail -1 | tee -a /tmp/mypasswd
done
unset FILE
2、写一个脚本:
1)设定变量FILE的值为/etc/passwd;
2) 依次向/etc/passwd中的每个用户问好;
3)统计一共有多少个用户;
#!/bin/bashou
#
FILE='/etc/passwd'
LINES=`wc -1 $FILE | cut -d' ' -f1`, your shell:`head -$LINE 4FILE | tail -l | cut -d: -f7`"
done
3、求一百以内偶数的和;
#!//bin/bash
#
declare -i SUM=0
for I in `seq 2 2 100`; do
let SUM+=$I
done
echo $SUM
4、求一百以内所有能被3整除的正整数和
#!/bin/bash
#
declare -i SUM=0
for I in {1..100}; do
M=$[$I%3]
if [ $M -eq 0 ]; then
SUM+=$I
fi
done
echo "The sum is:$SUM."
unset SUM
5、计算100以内所有奇数的和以及所有偶数的和
#!/bin/bash
#
declare -i EVENSUM=0
declare -i ODDSUM=0
for I in {1..100}; do
if [ $[$I%2 ] -eq 1 ]; then
let ODDSUM+=$I
else
let EVENSUM+=$I
fi
done
echo "The oddsum is: $ODDSUM."
echo "The evensum is: $EVENSUM."
6、写一个脚本,测试172.16.0.0/16网络内的所有主机是否在线;
#!/bin/bash
#
HOST=172.16.
for N in {0..254};do
for I in {1..254}; do
if ping -c 1 -W 1 $HOST$I &> /dev/null; then
echo "$HOST$N.$I is online"
else
echo "$HOST$N.$I is offline"
fi
done
done
unset HOST