实 验 报 告
课程名称: linux教程 实验项目: shell编程 专业班级: 网络工程xxx 指导教师: xxx
姓 名: xxxx 学 号: xxx 成 绩: 日 期 xxxx
1
2
第二篇:实验4 shell 程序设计 实验报告
实验4 shell 程序设计
1.采用shell编程语言(类似C语言),完成
(1)请用C语言编写程序,将1-100之间能被3或7整除的数显示出来.
实验结果:
student@linux-suse:~> ./homework1.sh
3 6 7 9 12 14 15 18 21 24 27 28 30 33 35 36 39 42 45 48 49 51 54 56 57 60 63 66 69 70 72 75 77 78 81 84 87 90 91 93 96 98 99 end!
程序代码:
for ((i = 1; i <= 100; i++))
do
j=$[$i % 3]
if test $j -eq 0
then
fi
done
echo "end!" echo -n $i echo -n " " j=$[$i % 7] if test $j -eq 0 then fi echo -n $i echo -n " " else
(2)请用C语言编写程序,从键盘输入年号,屏幕显示是不是闰年,若是闰年则显不“YES!”否则显示“NO!”。
实验结果:
student@linux-suse:~> ./homework2.sh
清输入年份
2012
yes!
student@linux-suse:~> ./homework2.sh
清输入年份
1995
no!
程序代码:
echo "清输入年份
read year
i=$[$year % 4]
if test $i -eq 0
then
fi echo "yes!" j=$[$year % 100] if test $j -eq 0 then fi echo "yes!" k=$[$year % 400] if test $k -eq 0 then fi echo "yes!" echo "no!" else else else
(3)请用C语言编写程序,将从键盘录入的10个任意数,由低向高排序后显示。
实验结果:
student@linux-suse:~> ./homework3.sh
输入数字
9
输入数字
5
输入数字
8
输入数字
6
输入数字
3
输入数字
2
输入数字
1
输入数字
4
输入数字
7
输入数字
0 1 2 3 4 5 6 7 8 9
--------------------end--------------------
程序代码:
for ((i=0;i<10;i++))
do
echo "输入数字"
read values[i]
done
for ((i=0;i<10;i++))
do
lowest=$i
for ((j=i+1;j<10;j++))
do
if test ${values[j]} -le ${values[$lowest]}
then
lowest=$j
fi
done
temp=${values[i]}
values[i]=${values[lowest]}
values[lowest]=$temp
done
for ((i=0;i<10;i++))
do
echo -ne "${values[$i]}\t"
done
echo -e "\n\n--------------------end--------------------\n"
2编写一个shell脚本文件,文件内包含10个shell命令,然后执行这个脚本文件,察看这些命令自动执行情况(批处理形式)
实验结果:
student@linux-suse:~> ./task2.sh
Tue Jun 5 17:14:31 CST 2012
student
/home/student
hello world! Please input a number
5
数字是 5
奇数
student@linux-suse:~> ./task2.sh
Tue Jun 5 17:14:37 CST 2012
student
/home/student
hello world! Please input a number
6
数字是 6
偶数
程序代码:
date
logname
pwd
echo "hello world! Please input a number"
read number
echo "数字是 $number"
x=$[$number % 2]
if test $x -eq 0
then echo "偶数"
else echo "奇数"
fi
3编写一个shell脚本文件,并用shell编程语言编写一个菜单程序,分别调用作业1中的三个小题。
例如:
shell脚本文件执行后,先在屏幕显示菜单如下:
Please input vour select(0-4):
1.Disply 1.1
2.Disply 1.2
3.Disply 1.3
0.EXit
Select:
当你键入你的选择后,系统就执行相应的程序,完成其指定功能并重新返回到主菜单,若用户键入“0”,则退出该程序。
实验结果:
student@linux-suse:~> ./task3.sh
Please input your select (0-3):
1. Disply 1.1
2. Disply 1.2
3. Disply 1.3
0. Exit
Select:
1
3 6 7 9 12 14 15 18 21 24 27 28 30 33 35 36 39 42 45 48 49 51 54 56 57 60 63 66 69 70 72 75 77 78 81 84 87 90 91 93 96 98 99 end!
student@linux-suse:~> ./task3.sh
Please input your select (0-3):
1. Disply 1.1
2. Disply 1.2
3. Disply 1.3
0. Exit
Select:
2
清输入年份
5214
no!
student@linux-suse:~> ./task3.sh
Please input your select (0-3):
1. Disply 1.1
2. Disply 1.2
3. Disply 1.3
0. Exit
Select:
3
输入数字
4
输入数字
5
输入数字
9
输入数字
8
输入数字
7
输入数字
6
输入数字
3
输入数字
2
输入数字
输入数字
1
0 1 2 3 4 5 6 7 8 9
--------------------end--------------------
student@linux-suse:~> ./task3.sh
Please input your select (0-3):
1. Disply 1.1
2. Disply 1.2
3. Disply 1.3
0. Exit
Select:
程序代码:
echo "Please input your select (0-3):"
echo "1. Disply 1.1"
echo "2. Disply 1.2"
echo "3. Disply 1.3"
echo "0. Exit"
echo "Select:"
read select
case $select in
"0")
break
;;
"1")
./homework1.sh
;; ./homework2.sh ;; ./homework3.sh ;; "2") "3") esac