1212布置的作业

1212布置的作业


1)写一个脚本 显示某用户登录了多少次,登录时间 判断用户是否存在
#!/bin/bash
read -p "please inpute a username:" A

if grep "^$A" /etc/passwd &>/dev/unll
then

who|grep "^$A\>"| wc -l;
who|grep "^$A\>"| cut -d ' ' -f14;

else
echo "pelase give me a correct name, try again! "
fi




#!/bin/bash
grep "^\<$1\>" /etc/passwd &>/dev/null && echo $1 && (who |grep "^$1"|wc -l ) && (who |cut -d" " -f12 )

grep "^\<$1\>" /etc/passwd &>/dev/null || echo "please input a right username,try again! "



2)100内偶数之和
#!/bin/bash
declare I=1
declare SUM=0
while [ $I -le 100 ];do
if [ $(($I%2)) -eq 0 ]
then
let SUM+=$I
fi
let I++
done
echo "The sum is: $SUM"





3)三个数,最大数是谁?
#!/bin/bash
if [ $1 -ge $2 ];then

if [$3 -ge $1 ];then
echo "$3 is the max"
else
echo "$1 is the max"
fi
elif [ $2 -ge $3 ];then
echo "$2 is the max"
else
echo "$3 is the max"

fi






echo -n "please enter three number"
read A B C
if [ $A -ge $B ]
then
max=$A
else
max=$B
fi
if [ $C -ge $max ]
then
max=$C
fi
echo "The max is $max"










4)ping C类网 ping B类网



#!/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 10 ];do
ping -c1 -W1 $PINGNET.$I &>/dev/null
[ $? -eq 0 ] && echo "$PINGNET.$I is online" || echo " $PINGNET.$I is offline "
let I++
done

}

bping () {
BPINGNET=`echo $1 |sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/g'`
let J=0
while [ $J -le 1 ];do
cping $BPINGNET.$J
let J++
done
}

read -p "inpute a ip:" IP







5)提示用户通过键盘输入一个用户名,来判断这个用户是否存在,如果存在就显示一下用户默认的shell

#!/bin/bash

read -p "please input your username:" A

if grep "^$A\>" /etc/passwd &> /dev/null ;then
echo "the user exist"
echo "his shell is:"
grep "^$A\>" /etc/passwd |cut -d: -f7
else
echo " The user not exist,please give me a correct username! "
fi






#!/bin/bash

read -p "Please input a user:" USER
grep --color=auto "^\<$USER\>" /etc/passwd &>/dev/null && echo "$USER" && grep "^\<$USER\>" /etc/passwd |cut -d: -f7
grep "^\<$USER\>" /etc/passwd &>/dev/null || echo "The $USER dosen't exist!!"




6)要求用户输入一个文件名,判断如果此文件是个普通文本文件,显示共有多少行
#!/bin/bash
let COUNTS=0
echo "Please enter a file:"
read FILE

if [ -e $FILE -a -f $FILE ];then
while read LINE
do

let COUNTS++
done < $FILE
echo "There are $COUNTS lines in $FILE."
else
echo "$FILE no exit"
fi






7)写个脚本 通过查找“/etc/sysconfignetwork-scripts/ifcfg-eth0”
如果BOOTPROTO=dhcp,bootp, 改为 static
(BOOTPROTO的值可以是动态的dhcp和bootp;也可以是静态的static和none)

修改
IPADDR=

NETMASK=

最后service network restart,使修改生效



IFFILE='/etc/sysconfig/network-scripts/ifcfg-eth0'
read -p "IPaddr:" MYIP
read -p "Netmask:" MYMASK

if grep -E "^BOOTPROTO=(dhcp)|(bootp)$" $IFFILE &> /deb/null ;then
sed -i "s/^BOOTPROTO=.*/BOOTPROTO=static/g" $IFFILE
grep "^IPADDR=.*" $IFFILE &> /dev/null && sed -i "s/IPADDR=.*/IPADDR=$MYIP/g" $IFFILE || sed -i "/BOOTPROTO.*/a\IPADDR=$MYIP" $IFFILE
grep "^NETMASK=.*" $IFFILE &> /dev/null && sed -i "s/NETMASK=.*/NETMASK=$MYMASK/g" $IFFILE || sed -i "/BOOTPROTO.*/a\NETMASK=$MYMASK" $IFFILE

fi




技巧:bash -x 脚本名字
用来调试脚本,能看到每一行的执行情况



你可能感兴趣的:(linux,职场,作业,休闲)