30道linux操作系统shell程序综合设计题

上次分享了常见的21个shell命令,这次看看掌握咋样了呗

题目1

步骤 1: 使用 vi 编辑文件 shell 脚本文件 exe1.sh
#!/bin/bash
var=”hello”
echo $var
echo ”The program $0 is now running ”
echo ”The first parameter was $1”
echo ”The second parameter was $2”
echo ”The parameter list was $*”
echo ”The user’s home directory is $HOME”
echo ”please enter a new word”
read var
echo $var
exit 0
步骤 2 :对执行文件赋予执行权限
步骤 3 :执行文件

 题目2

步骤 1: 使用 vi 编辑文件 shell 脚本文件 sum.sh
#!/bin/bash
gsum=0
function sumn()
{ sum=0
for((i=1;i<=$1;i++))
do
let sum=$sum+$i
done
gsum=$sum
return $sum
}
sumn 10
echo “sum=$?”
步骤 2 :对执行文件赋予执行权限
步骤 3 :执行文件

 题目3

步骤 1: 使用 vi 编辑文件 sum.c
#include<stdio.h>
int func(int n)
{
int sum=0,i;
for(i=0;i<n;i++)
{
sum+=i;
}
return sum;
}
main()
{
int i;
long result=0;
for(i=1;i<=100;i++)
{
result+=i;
}
printf("result[1-100]=%d\n",result);
printf("result[1-100]=%d\n",func(100));
}
步骤 2 :编译文件并生成可执行文件
步骤 3 :如果有错误,再次使用 vi 修改 sum.c 的内容;然后再次编译。
直至编译时不报任何错误
步骤 4 :执行文件

题目4

步骤 1: 使用 vi 编辑文件 application.c
#include <stdio.h>
#define N 10
void greeting (char * name);
int main(void)
{
char name[N];
printf("Your Name, Please:");
scanf("%s", name)
greeting(name);
return 0; }
void greeting (char * name)
{ printf("Hello %s! \r\n", name); }
步骤 2 :编译文件并生成可执行文件
步骤 3 :如果有错误,再次使用 vi 修改 sum.c 的内容;然后再次编译。
直至编译时不报任何错误
步骤 4 :执行文件

题目5

步骤 1: 新建一个文件夹(文件夹的名字为自己姓名的拼音字母),例
如姓名为张三,则新建文件夹名字为 zhangsan
步骤 2: 进入新建文件夹中(例如 zhangsan ),使用 vi 编辑文件 linux.c
并保存
#include <stdio.h>
int main(void)
{
printf("I Like Linux! \r\n");
return 0;
}
步骤 3 :退出新建文件夹(例如 zhangsan ),使用压缩命令将新建文
件夹打包压缩为 zhangsan.tar.gz

题目6

步骤 1: 新建一个文件夹(文件夹的名字为姓名的拼音字母),例
如姓名为张三,则新建文件夹名字为 zhangsan
步骤 2: 进入新建文件夹中(例如 zhangsan ),使用 vi 编辑文件 linux.c
并保存
#include <stdio.h>
int main(void)
{
printf("I Like Linux! \r\n");
return 0;
}
步骤 3 :在新建文件夹(例如 zhangsan )中再次新建一个文件夹,取
名为 test ,将 linux.c 程序拷贝到 test 文件夹中

题目7

步骤 1: 新建一个文件夹(文件夹的名字为姓名的拼音字母),例
如姓名为张三,则新建文件夹名字为 zhangsan
步骤 2: 进入新建文件夹中(例如 zhangsan ),使用 vi 编辑文件 linux.c
并保存
#include <stdio.h>
int main(void)
{
printf("I Like Linux! \r\n");
return 0;
}
步骤 3 :在新建文件夹(例如 zhangsan )中将 linux.c 程序更名为 test.c

题目8

步骤 1: 使用 vi 编辑文件 traffic.sh 程序要求依次满足以下要求:
a. 使得首先程序输出 “ The shell program was written by zhangsan
b. 使得程序输出“ current signal is:
c. 程序等待键盘输入信号(输入的信号包括“ red
,
green
,
yellow ”)
d. 如果输入的信号
为“ red ”,显示“ stopped
为“ green ”,显示“ pass!
为“ yellow ”,显示“ wait
步骤 2 :对执行文件赋予执行权限
步骤 3 :执行文件
#! /bin/bash
# traffic signal
echo "current signal is:"
read signal
case $signal in
red)
echo 'stopped!';;
green)
echo 'pass!';;
yellow)
echo 'wait!';;
esac

题目9

步骤 1: 使用 vi 编辑文件 test.c
#include <stdio.h>
main ( )
{
int i, j, k;
for (i=1; i<=6; i++)
{
for (j=1; j<=6; j++)
{
if (i>=j)
{
k=i+j;
printf (“%d + %d = %d \t”, i, j,k);
}
}
printf (“\n”);
}
}
步骤 2 :编译文件并生成可执行文件;
步骤 3 :如果有错误,再次使 用 vi 修改对应程序的内容;
然后再次编译。直至编译时不报任何错 误;
步骤 4 :执行文件

题目10

步骤 1: 使用 vi 编辑文件 shell 脚本文件 sum.sh
#!/bin/bash
#an example script of while
clear
loop=0
while [ $loop –le 10 ]
do
let loop=$loop+1
echo "the loop current value is: $loop"
done
步骤 2 :对文件赋予执行权限
步骤 3 :执行文件

题目11

步骤 1: 使用 vi 编辑文件 shell 脚本文件 case.sh
#!/bin/bash
#an example script of case
clear
echo "Are you like Linux?"
read like_it
case $like_it in
y|Y|yes|Yes) echo "Linux is a good friend of us."
;;
n|N|no|No) echo "Try it, and you will like it."
;;
*) echo "you answer is: $like_it"
;;
esac
步骤 2 :对执行文件赋予执行权限
步骤 3 :执行文件

题目12

步骤 1. 采用普通用户建立一个空文件 , 文件名为 exam.sh ,并查看其
权限。
步骤 2. 采用文字设定法,修改 exam.sh 的文件权限,即,拥有者增
加执行权限,同组用户减去写权限,其他用户去掉所有权限,并查看
修改结果。
步骤 3. 采用数字设定方法,设定 exam.sh 的文件权限 即,文件拥有
者有读 r ,写 w 和执行 x 的权限,同组用户拥有读 r 和执行 x 的权限,
而其它用户只有读 r 的权限,并查看修改结果

题目13

步骤 1. 建立姓名全拼小写(以下用 zhangsan 代替)的文件夹。
步骤 2. 在此文件中生成 file1.txt 文件,内容“ my name is zhangsan ;
生成 file2.txt 文件 , 内容 “ this is file2 ; 生成 file3 文件 , 内容“ this
is file3 .
步骤 3. 合并 file1 file2 file3 文件为 file4.txt 文件,显示 file4 中的
内容 .
步骤 4. 用“ find ”命令在用户主目录中查找 file4.txt 文件

 题目14

步骤 1. 用超级用户 root 创建一个新用户 , 用户名为自己的姓名全拼小
( 以下以 lisi 为例 ) ,设置其主目录为 /home/lisi,
步骤 2. 通过 /etc/passwd 查看用户是否创立成功。
步骤 3. 用超级用户给 lisi 设置密码 , 并切换到用户 lisi ,然后切换回超
级用户
步骤 4. 更改用户 lisi 的用户名为姓名全拼小写 + 学号前两位(以下以
lisi08 为例),并通过 /etc/passwd 查看是否生效。
步骤 5. 删除用户 lisi08 及其主目录

题目15

把下面一段文字用 vi 编辑器输入到文本文件 bj3 中:
Time: PM 3:30 2012.5.30
Place:Computer Hall 408
Lecture:History of Computer Science
把第 1 行到第 3 行连续复制 3 遍。
在第一行前面添加标题“Meeting Notes”
把最后一行的“History”改成“Future”。
在最后一行后面添加上 2021年6 月月历。

题目16

用 vi 编辑器新建文件文件 gd3,输入下面一行语句:
The vi editor was developed at the Univesity of California,Berkeley.
把该行语句复制 10 行。
把最后一行中出现的所有大写字母均改为相应的小写字母。
在第一行前面插入标题,内容是“vi history”。
在最后一行附加上当前日期和时间。

题目17

编写 Shell 程序,使其执行时在屏幕上输出整数 1~50,但其中位于 20 和 30 之
间的偶数不输出。
for ((i=1;i<=50;i++))
do
if ((i>=20 && i<=30 && i%2==0))
then continue
fi
echo $i
done

题目18

编写 Shell 程序,在屏幕上输出 10 行,第一行输出 1 个“*”号,第二行输出 3
个“*”号,第二行输出 5 个“*”号,依此类推。
for ((i=1;i<=10;i++))
do
for ((j=1;j<=2*i-1;j++))
do
echo -n "*"
done
echo
done

题目19

编写 Shell 程序,输入一批正整数(以 0 为结束标志),请统计其中奇数的个数,
并把统计结果输出。
sum=0
echo "please enter the interger,ended by 0"
read num
while((num!=0))
do
if((num%2==1))
then ((sum=sum+1))
fi
read num
done
echo "the number of ji shu is $sum"

题目20

编写 Shell 程序,求 1*1+2*2+3*3+…….+50*50 的总和。
sum=0
for ((i=1;i<=50;i++))
do
((ji=i*i))
((sum=sum+ji))
done
echo $sum

题目21

编写 Shell 程序,列出等差数列 1,3,5,7,9,…的前 20 项及其总和。(上机)
n=1
s=0
for ((i=1;i<=20;i++))
do
echo
$n
((s=s+n))
((n=n+2))
done
echo
"sum=$s"

题目22

编写 Shell 程序,把 100 分转换为 5 级计分制,从键盘输入百分值,根据情况,
显示具体的等级,其中 90-100 之间对应 A,80-89 之间对应 B,70-79 之间对应
C,60-69 之间对应 D,60 以下对应 E。
echo "please input x="
read
x
if(($x<=100 && $x>=90))
then
echo "A"
else
if(($x>=80 && $x<=89))
then
echo "B"
else
if(($x>=70 && $x<=79))
then
echo "C"
else
if(($x>=60 && $x<=69))
then
echo "D"
else
echo
"E"
fi
fi
fi
fi

题目23

编写 Shell 程序,在屏幕上输出如下格式的九九表。
1*1=1
2*1=2 2*2=4
……………….
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
((b=i*j))
echo -n "$i*$j=$b "
done
echo
done

题目24

编写 Shell 程序,首先输出提示信息"please input x",然后从键盘读入一个整
数,接着判断该数是否大于 11,如果是则输出 2012 年 12 月份的月历,否则输
出 2013 年 11 月份的月历。
echo "please input x"
read x
if ((x>=11))
then
cal 12 2012
else
cal 11 2013
fi

题目25

编写 Shell 程序,首先显示一行信息:“input x and y”,然后从键盘读入两个
整数,接着显示“the sum of two input is ”,然后把两个数的和输出到屏
幕上,最后输出系统当前日期和时间信息。
echo "input x and y"
read x y
echo "the sum of two input is"
((s=x+y))
echo $s
date

 题目26

编写 Shell 程序 test5,使之从命令行接受数字,输出最大值,例如,键入 test5
1 2 3,则屏幕显示:the largest number is : 3。
n=$#
if (($#<1))
then echo "error number"
exit
else
max=$1
while ((n>=1))
do
if (($1>=max))
then
max=$1
fi
shift
((n=n-1))
done
echo " the largest number
is :$max"
fi

题目27

编写程序 test5,使之从命令行接受数字,输出最小值,例如,键入 test5 1 2 3
屏幕显示:the smallest number is : 1。
n=$#
if (($#<1))
then echo "error number"
exit
else
min=$1
while ((n>=1))
do
if (($1<=min))
then
min=$1
fi
shift
((n=n-1))
done
echo " the smallest number is :
$min"
fi

题目28

编写 Shell 程序,求 1!+ 2!+ 3!+….+ 10!的值。
s=0
echo "please input number:"
read x
for ((i=1;i<=x;i++))
do
t=1
for ((j=1;j<=i;j++))
do
((t=t*j))
done
((s=s+t))
done
echo "the sum is $s"
PAGE
\* MERGEFORMAT8
PAGE
PAGE 8

题目29

步骤 1: 使用 vi 编辑文件 hello.c ,实现
#include <stdio.h>
main( )
{
char name[20];
printf("Please input your name:");
scanf("%s", name); // 输入姓名
printf("Welcome %s!\n",name); // 输出 welcome 姓名
return 0;
}
步骤 2 :编译文件并生成可执行文件
步骤 3 :如果有错误,再次使用 vi 修改 hello.c 的内容;然后再次编
译。直至编译时不报任何错误
步骤 4 :执行文件

题目30

 

步骤 1: 使用 vi 编辑文件 shell 脚本文件 hello.sh
#!/bin/bash
#a Simple shell Script Example
#a Function
function say_hello()
{
echo "Enter Your Name,Please. :"
read name
echo "Hello $name"
}
echo "Programme Starts Here....."
say_hello
echo "Programme Ends."
步骤 2 :对执行文件赋予执行权限
步骤 3 :执行文件
评论 52
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

通信汪的美好生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值