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

简介: 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
donefor ((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 :执行文件


相关文章
|
4天前
|
存储 程序员
操作系统(10)----从写程序到程序运行
操作系统(10)----从写程序到程序运行
10 1
|
6天前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
14 2
|
6天前
|
Linux 开发工具 C语言
【操作系统】实验四 增加Linux系统调用
【操作系统】实验四 增加Linux系统调用
15 1
|
6天前
|
NoSQL Ubuntu Linux
【操作系统】实验三 编译 Linux 内核
【操作系统】实验三 编译 Linux 内核
11 1
|
6天前
|
安全 Linux 网络安全
【操作系统】实验一 Linux操作系统安装
【操作系统】实验一 Linux操作系统安装
16 3
|
6天前
|
存储 Linux 程序员
【操作系统原理】—— Linux内存管理
【操作系统原理】—— Linux内存管理
9 0
|
6天前
|
存储 Shell Linux
操作系统实战(一)(linux+C语言)
本篇文章重点在于利用linux系统的完成操作系统的实验,巩固课堂知识
|
6天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
12 0
|
7月前
|
Unix Shell Linux
|
6天前
|
Shell Linux C语言
Linux中执行Shell的函数(popen,system,exec)介绍:分享一些常用的执行Shell的函数及其相关编程技巧和经验
Linux中执行Shell的函数(popen,system,exec)介绍:分享一些常用的执行Shell的函数及其相关编程技巧和经验
36 0