本帖最后由 石头 于 2015-12-22 17:02 编辑
①编写一个名为iffile程序,它执行时判断/bin目录下date文件是否存在?
②编写一个名为greet的问候程序,它执行时能根据系统当前的时间向用户输出问候信息。设从半夜到中午为早晨,中午到下午六点为下午,下午六点到半夜为晚上。
③编写一个名为ifuser的程序,它执行时带用户名作为命令行参数,判断该用户是否已经在系统中登录,并给出相关信息。
④编写一个名为menu的程序,实现简单的弹出式菜单功能,用户能根据显示的菜单项从键盘选择执行对应的命令。
⑤编写一个名为chname的程序,将当前目录下所有的.txt文件更名为.doc文件。
⑥编写一个名为chuser的程序,执行中每隔5分钟检查指定的用户是否登录系统,用户名从命令行输入;如果指定的用户已经登录,则显示相关信息。
0
1.
#!/bin/bash
#
FI="/bin/data"
if `cat $FI &> /dev/null`;then
echo "$FI is exist"
else
echo "$FI no found!"
exit 88
fi
#!/bin/bash
#
FI="/bin/data"
if `cat $FI &> /dev/null`;then
echo "$FI is exist"
else
echo "$FI no found!"
exit 88
fi
0
2.
#!/bin/bash
#
HH=`date +%H`
echo "Now Time:" `date +%H:%M:%S`
case $HH in
00|01|02|03|04|05|06|07|08|09|10|11)
echo "Good Morning!"
;;
12|13|14|15|16|17)
echo "Good Afternoon!"
;;
18|19|20|21|22|23)
echo "Good Evening!"
;;
*)
;;
esac
#!/bin/bash
#
HH=`date +%H`
echo "Now Time:" `date +%H:%M:%S`
case $HH in
00|01|02|03|04|05|06|07|08|09|10|11)
echo "Good Morning!"
;;
12|13|14|15|16|17)
echo "Good Afternoon!"
;;
18|19|20|21|22|23)
echo "Good Evening!"
;;
*)
;;
esac
0
3.
#!/bin/bash
#
read -p "Please input the username" US
until who | grep $US &> /dev/null;do
echo $US "is not come"
sleep 10
done
echo $US "is loging ..."
#!/bin/bash
#
read -p "Please input the username" US
until who | grep $US &> /dev/null;do
echo $US "is not come"
sleep 10
done
echo $US "is loging ..."
0
4.
#!/bin/bash
#
declare Par
function message(){
echo "0.echo hello,world!"
echo "1.echo hi!"
echo "2.quit"
read -p "Please input parameter" Par
}
message
while [ $Par <> '2' ] ; do
case $Par in
0)
echo "hello,world"
;;
1)
echo "hi,world"
;;
2)
exit 88
;;
*)
echo "Unkown command"
;;
esac
sleep 2
message
done
#!/bin/bash
#
declare Par
function message(){
echo "0.echo hello,world!"
echo "1.echo hi!"
echo "2.quit"
read -p "Please input parameter" Par
}
message
while [ $Par <> '2' ] ; do
case $Par in
0)
echo "hello,world"
;;
1)
echo "hi,world"
;;
2)
exit 88
;;
*)
echo "Unkown command"
;;
esac
sleep 2
message
done
0
5.
#!/bin/bash
#
declare Dir
read -p "Please input directory" Dir
for i in `ls $Dir/*.txt` ;do
j=`echo $i|cut -d. -f1`".doc"
mv $i $j
done
#!/bin/bash
#
declare Dir
read -p "Please input directory" Dir
for i in `ls $Dir/*.txt` ;do
j=`echo $i|cut -d. -f1`".doc"
mv $i $j
done
0
6.
#!/bin/bash
#
read -p "Please input the username" US
until who | grep $US &> /dev/null;do
echo $US "is not come"
sleep 300
done
echo $US "is loging ..."
who|grep $US
#!/bin/bash
#
read -p "Please input the username" US
until who | grep $US &> /dev/null;do
echo $US "is not come"
sleep 300
done
echo $US "is loging ..."
who|grep $US
0
- #!/bin/bash
- file=/bin/date
- if [ -f $file ];then
- echo "file exist!"
- else
- echo "no file!,please check!"
- fi
0
- #!/bin/bash
- nowtime=`date +"%H"`
- if [[ 0 -le $nowtime && $nowtime -lt 12 ]];then
- echo "good morning!"
- elif [[ 12 -le $nowtime && $nowtime -lt 18 ]]; then
- echo "good afternoon!"
- elif [[ 18 -le $nowtime && $nowtime -ge 23 ]]; then
- echo "good night!"
- fi
0
- #!/bin/bash
- loginuser=`who|awk '{print $1}'`
- result=`echo $loginuser|grep $1`
- if [[ -n $result ]];then
- echo "$1 is here!"
- else
- echo "$1 is not here!"
- fi
0
2.#! /bin/sh -
hour=`date|cut –c 12-13`
if test “$hour” –ge 0 –a “$hour” –le 11; then
echo “Good morning!”
elif test “$hour” –ge 12 –a “$hour” –le 17; then
echo “Good afternoon!”
else
echo “Good evening!”
fi
? 说明:
第一个有效语句是将命令执行的结果赋给hour变量,所以用反向单引
号。
用cut命令从date命令的输出中切割出“小时”信息;这里-c 10-11
选项表示只切割10到11列。
这个程序使用了if连用格式,也可以使用if完整格式的嵌套形式。
②根据系统当前的时间向用户输出问候信息
hour=`date|cut –c 12-13`
if test “$hour” –ge 0 –a “$hour” –le 11; then
echo “Good morning!”
elif test “$hour” –ge 12 –a “$hour” –le 17; then
echo “Good afternoon!”
else
echo “Good evening!”
fi
? 说明:
第一个有效语句是将命令执行的结果赋给hour变量,所以用反向单引
号。
用cut命令从date命令的输出中切割出“小时”信息;这里-c 10-11
选项表示只切割10到11列。
这个程序使用了if连用格式,也可以使用if完整格式的嵌套形式。
②根据系统当前的时间向用户输出问候信息
0
3. #!/bin/bash
echo 'type in the user name:'
read user
if grep $user /etc/passwd > /tmp/null && who | grep $user
# 测试条件是由&&连接的2个命令
# grep $user /etc/passwd > /tmp/null ,从/etc/passed文件中寻找$user字段,并重定向输出到文件/tmp/null
# who | grep $user ,who命令列出当前系统已经登录的用户,管道|链接,从其中寻找是否有$user字符串
then
echo "$user has logged in the system."
cp /tmp/null ~/me.tmp
rm /tmp/null
else
echo "$user has not logged in the system."
fi
echo 'type in the user name:'
read user
if grep $user /etc/passwd > /tmp/null && who | grep $user
# 测试条件是由&&连接的2个命令
# grep $user /etc/passwd > /tmp/null ,从/etc/passed文件中寻找$user字段,并重定向输出到文件/tmp/null
# who | grep $user ,who命令列出当前系统已经登录的用户,管道|链接,从其中寻找是否有$user字符串
then
echo "$user has logged in the system."
cp /tmp/null ~/me.tmp
rm /tmp/null
else
echo "$user has not logged in the system."
fi
0
4. #! /bin/sh
clear
echo "-------------MENU---------"
echo
echo "1.Find files modified in last 24 hours"
echo "2. The free disk space"
echo "3. Space consumed by this user"
echo "4.Exit"
echo
echo -n " Select:"
read choice
case $choice in
1)find $HOME -mtime -1 -print;;
2)df;;
3)du -s $HOME;;
4)exit;;
*)echo "Invalid option"
esac
clear
echo "-------------MENU---------"
echo
echo "1.Find files modified in last 24 hours"
echo "2. The free disk space"
echo "3. Space consumed by this user"
echo "4.Exit"
echo
echo -n " Select:"
read choice
case $choice in
1)find $HOME -mtime -1 -print;;
2)df;;
3)du -s $HOME;;
4)exit;;
*)echo "Invalid option"
esac
0
5. #!/bin/sh
for file in *.txt
do
leftname=`basename $file .txt`
mv $file $leftname.doc
done
说明:
(1)在Linux系统中不支持mv *.txt *.doc这样的更名命令形式
,如果需要将文件成批地更名最好编写一个shell脚本文件。
(2)在for语句的参数列表中使用了“*”通配符。
(3(在程序中用到basename命令,该命令从随后的文件名剥去指
定的后缀。
⑤将当前目录下所有的.txt文件更名
0
6.方法一:
#!/bin/bash
#
read -p "Enter a user name: " userName
先判别用户名是否存在
while ! id $userName &> /dev/null; do
read -p "Enter a user name again: " userName
done
who | grep "^$userName" &> /dev/null
retVal=$?
while [ $retVal -ne 0 ]; do
sleep 300
who | grep "^$userName" &> /dev/null
retVal=$?
done
echo "$userName is on."
0
1.#!/bin/bash
file=/bin/date
if [ -f $file ]
then
echo "$file cun zai"
else
echo "wen jian bu cun zai"
fi
file=/bin/date
if [ -f $file ]
then
echo "$file cun zai"
else
echo "wen jian bu cun zai"
fi
0
2. #!/bin/bash
time=`date +%T |awk -F ':' '{print $1}'`
if [[ $time -ge 0 && $time -lt 12 ]]
then
echo "good morning"
elif [[ $time -ge 12 && $time -lt 18 ]]
then
echo "good afternoon"
else
echo "good night"
fi
time=`date +%T |awk -F ':' '{print $1}'`
if [[ $time -ge 0 && $time -lt 12 ]]
then
echo "good morning"
elif [[ $time -ge 12 && $time -lt 18 ]]
then
echo "good afternoon"
else
echo "good night"
fi
0
3.#!/bin/bash
if [ $# -le 0 ]
then
echo "zhi xing shi qing shu ru yonghu"
exit 0
fi
if [ $# -ge 1 ]
then
for i in $@
do
us=`who |grep $i`
if [ $? == 0 ]
then
echo "$us"
else
echo " $i wei deng lu "
fi
done
fi
if [ $# -le 0 ]
then
echo "zhi xing shi qing shu ru yonghu"
exit 0
fi
if [ $# -ge 1 ]
then
for i in $@
do
us=`who |grep $i`
if [ $? == 0 ]
then
echo "$us"
else
echo " $i wei deng lu "
fi
done
fi
编辑回复