全部博文(489)
分类:
2011-12-22 22:04:05
原文地址:shell编程基础(2) 作者:luozhiyong131
1. 编写一个shell脚本以下面的格式显示HOME、PATH、SHLVL、LOGNAME变量的值:
HOME =
PATH =
SHLVL =
LOGNAME =
#!/bin/bash
echo "HOME = $HOME"
echo "PATH = $PATH"
echo "SHLVL = $SHLVL"
echo "LOGNAME = $LOGNAME"
2. 创建一个shell脚本,该脚本接收10个数,并显示最大数。
#!/bin/bash
i=1
read num1
while [ $i -le 9 ]
do
read num2
if [ $num1 -gt $num2 ]; then
num=$num1
else
num=$num2
fi
num1=$num
i=`expr $i + 1`
done
echo -n "max num :"
echo $num
3. 编写一个shell脚本,它从用户那里接收一个文件名和目录名。该脚本应判断接收的文件名和目录名是否存在,如果存在,就必须将文件拷贝到指定的目录下。当不能满足上面条件时要显示错误消息。
#!/bin/bash
echo "$1"
if [ -e $1 ]; then
echo "please input way"
read way
cp -rf $1 $way
echo "Done"
else
echo "false"
Fi
4. 改变登录提示符为:enter your command >
PS1="enter your command >"
5. 编写一个脚本,接收一个数字,并显示从1 到该数字之间的所有数字的平方值,格式如下:
1 square =
2 square =
…..
n square =
#!/ein/bash
read num
i=1
while [ $i -le $num ]
do
echo "$i square = $(($i*$i))"
i=`expr $i + 1`
Done
6. 使用find命令,查看在linux服务器上定义文件dmesg所花的时间
7. 编写一个shell脚本,它能够显示如下主菜单:
************** Main Menu *******************
1) File commands
2) Directory commands
3) List the users currently logged in
当选择第一个选项时,应显示如下文件子菜单:
Which file command would you like help on?
1) cp
2) mv
3) rm
please enter your choice:
当选择主菜单第二个选项时,应显示下面的目录子菜单:
Which directory command would you like help on?
1) mkdir
2) rmdir
3) ls
please enter your choice:
当选择了一个文件或者目录子菜单的选项时,shell脚本应显示该命令的求助,当主菜单的第三个选项被选择时,应显示当前登录的用户列表。
#!/bin/bash
echo "************** Main Menu ******************* "
echo "1) File commands"
echo "2) Directory commands"
echo "3) List the users currently logged in"
read choise
case $choise in
1)
echo "1) cp"
echo "2) mv"
echo "3) rm"
echo "please enter your choice:"
;;
2)
echo "Which directory command would you like help on?"
echo "1) mkdir"
echo "2) rmdir"
echo "3) ls"
echo "please enter your choice"
;;
3)
w ;;
esac
8. 编写一个shell脚本,应能显示以下序列的前10个数字:
0,1,1,2,3,5,8,13………
(Fibonacci序列)
#!/bin/bash
echo -n "0, 1"
num1=0
num2=1
i=1
while [ $i -lt 10 ]
do
num=$(($num1 + $num2))
echo -n ", $num"
num1=$num2
num2=$num
i=`expr $i + 1`
done
echo