1.求100以内偶数和
#!/bin/bash
let I=1
let SUM=0
while [ $I -le 100];do
if [ (($I%2))-eq 0 ]
then
SUM=$(($SUM+$I))
I=$(($I+1))
else
I=$(($I+1))
fi
done
echo "The sum is :$SUM"
2.求三个数中的最大值
#!/bin/bash
echo "please enter three number:"
read A B C
if [ $A -gt $B ]
then
max=$A
else max=$B
fi
if [ $C -gt $max ]
then
max=$C
fi
echo "The max is $max"
3.显示登录用户时间,用户名
#!/bin/bash
cat /etc/passwd|cut -d: -f1|grep '^$1\>' ||who |grep $1 &> /dev/null
if [ $? -eq 0 ];then
echo "the user is :$1"
TIME=`who|cut -d- -f2,3|cut -d' ' -f1,2`
COUNT=`who |grep $1 |cut -d' ' -f1|wc -l`
echo "$1 login counts are:$COUNT"
echo "$1 login time is:$TIME"
else
echo "please give me a current name:"
fi
4.ping 一个c类网
#!/bin/bash
cping (){
pingnet=`echo $1 | sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*\1/g'`
let I=1
while [ $I -le 254 ];do
ping -c 1 -W 2 $pingnet.$I &> /dev/null
[ $? -eq 0 ] && echo "$pingnet.$I online." || echo "$pingnet.$I offline."
let I++
done
}
read -p "A network:"MYNET
cping $MYNET
5.判断用户是否存在并显示默认shell
#!/bin/bash
read -p "please input your username:" A
if cut -d: -f1 /etc/passwd |grep "^$A$" &> /dev/null;then
B=`grep "$A:" /etc/passwd | cut -d: -f7`
echo "${A}'s shell is $B."
else
echo " $A not exist,please give me a correct username! "
fi
6.输入一个文件并统计有多少行
#!/bin/bash
let COUNTS=0
echo "Please enter a file:"FILE
read FILE
if [ -e $FILE -a -f $FILE ];then
while read LINE
do
echo $LINE
COUNTS=$(($COUNTS+1))
done < $FILE
echo "The total linesis $COUNTS."
else
echo "This FILE is wrong ,please enter a right FILE. "
fi
7.定义一个简单的函数,求两个数的大小
#!/bin/bash
max() {
[ $1 -gt $2 ] && echo "The max is $1."||echo "The max is $2."
}
max 1 2
转载于:https://blog.51cto.com/chenxizhuimeng/459296