Linux第一次试验:Shell Script编程应用实例

前言

为了帮助同学们完成痛苦的实验课程设计,本作者将其作出的实验结果及代码贴至CSDN中,供同学们学习参考。如有不足或描述不完善之处,敬请各位指出,欢迎各位的斧正!

一、实验目的

综合Linux常用命令和vi编辑器的使用,熟练掌握shell脚本编程。

二、实验工具与设备

已安装Linux系统的计算机或虚拟机。

三、实验预备知识

Linux的Shell种类众多,常见的有:Bourne Shell(/usr/bin/sh或/bin/sh)、Bourne Again Shell(/bin/bash)、C Shell(/usr/bin/csh)、K Shell(/usr/bin/ksh)、Shell for Root(/sbin/sh)等等。不同的Shell语言的语法有所不同,所以不能交换使用。每种Shell都有其特色之处,基本上,掌握其中任何一种就足够了。在本文中,我们关注的重点是Bash,也就是Bourne Again Shell,由于易用和免费,Bash在日常工作中被广泛使用;同时,Bash也是大多数Linux系统默认的Shell。在一般情况下,人们并不区分 Bourne Shell和Bourne Again Shell,所以,在下面的文字中,我们可以看到#!/bin/sh,它同样也可以改为#!/bin/bash
利用vi等文本编辑器编写Shell脚本的格式是固定的,如下:

#!/bin/sh
#comments
Your commands go here

首行中的符号#!告诉系统其后路径所指定的程序即是解释此脚本文件的Shell程序。如果首行没有这句话,在执行脚本文件的时候,将会出现错误。后续的部分就是主程序,Shell脚本像高级语言一样,也有变量赋值,也有控制语句。除第 一行外,以#开头的行就是注释行,直到此行的结束。如果一行未完成,可以在行尾加上",这个符号表明下一行与此行会合并为同一行。
编辑完毕,将脚本存盘为filename.sh,文件名后缀sh表明这是一个Bash脚本文件。执行脚本的时候,要先将脚本文件的属性改为可执行的:

chmod +x filename.sh

执行脚本的方法是:

./filename.sh

下面我们从经典的“hello world”入手,看一看最简单的Shell脚本的模样。

#!/bin/sh
#print hello world in the console window
a = "hello world"
echo $a

Shell Script是一种弱类型语言,使用变量的时候无需首先声明其类型。新的变量会在本地数据区分配内存进行存储,这个变量归当前的Shell所有,任何子进 程都不能访问本地变量。这些变量与环境变量不同,环境变量被存储在另一内存区,叫做用户环境区,这块内存中的变量可以被子进程访问。变量赋值的方式是:

variable_name = variable_value

如果对一个已经有值的变量赋值,新值将取代旧值。取值的时候要在变量名前加$$variable_name可以在引号中使用,这一点和其他高级语言是明显不同的。如果出现混淆的情况,可以使用花括号来区分,例如:

echo "Hi, $as"

就不会输出“Hi, hello worlds”,而是输出“Hi,”。这是因为Shell把$as当成一个变量,而$as未被赋值,其值为空。正确的方法是:

echo "Hi, ${a}s"

单引号中的变量不会进行变量替换操作。
关于变量,还需要知道几个与其相关的Linux命令。
env用于显示用户环境区中的变量及其取值;set用于显示本地数据区和用户环境区中的变量及其取值;unset用于删除指定变量当前的取值,该值将被指定为NULL;export命令用于将本地数据区中的变量转移到用户环境区。

四、 实验内容和步骤

  1. 编写一个脚本,求斐波那契数列的前10项及总和。
num1=1
num2=1
echo -n “$num1+$num2”
sum=2
for((i=1;i<=8;i++))
do
tmp=$(expr $num1 + $num2)
echo -n “+$tmp((num1=num2))
((num2=tmp))
sum=$(expr $sum + $tmp)
done
echo “=$sum
  1. 编写一个脚本,求一个数的逆序。
echo -n please input num:
read num
echo -n The num is 
while [ $num -gt 0 ]
do
sd=$(($num % 10))
echo -n "$sd"
num=$(($num/10))
done
echo
  1. 设计一个Shell程序,在用户主目录下建立一个userdata目录,在此目录下再建立5个目录,即user1~user5,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。(注意:最后删除自己所建立的目录,恢复原样,以便后面同学做此实验)
mkdir -m 754 /home/userdata
i=1
temp=/home/userdata/user
while[ $i -le 5 ]
do
mkdir -m 754 $temp$i
let”i=i+1”#
done
  1. 编程实现简单计算器。要求 用户输入一个表达式并输入结果,程序会判断用户输入的结果是否正确,并给出提示。直到 用户输入‘q’时,才退出执行。
    参考程序:
#!/bin/bash
#fileName:first
#note:This is the first title
#function:input an expretion and the answer, I will tell you right or wrong
#         You can try it as many times as you like.
echo Hello!  @_@ 
echo Welcom to the calculate testing!
echo You can input an expretion such as 2*2 or 3+1, and input the answer
echo I will tell you whether you are right or wrong.
echo You can input 'q' to exit.
echo "Now let's begin!"
number1=0;
while [ "$number1" != "q" ] 
do 
	echo Input the first number:
	read number1

    echo Input the operator type:
    read type
    echo Input the second number:
    read number2 
    echo Input the answer:
    read yourAnswer

    case $type in
        +) myAnswer=`expr $number1 + $number2`;;
        -) myAnswer=`expr $number1 - $number2`;;  
        \*)myAnswer=`expr $number1 \* $number2`;;
        /) 	
		if [ $number2 -eq 0 ]
		then
			echo "Sorry! :-("
			echo "0 cannot be the divisor"
			continue 
		else
			myAnswer=`expr $number1 / $number2`
		fi
	;;	
	*) echo Error!;;
    esac
    if [ $myAnswer -eq $yourAnswer ]
    then 
        echo ":-)" Congratulations!
        echo Your are right!
        echo Input 'q' to exit or try again!
    else
        echo ":-(" Sorry!
        echo You are wrong!
	echo "The right answer is:"
        echo "$number1 $type $number2 = $myAnswer"
        echo Input 'q' to exit or try again!    
    fi
   # echo Input the first number:
   # read number1
done

五、实验代码及步骤截图

  1. 程序1:
num1=1
num2=1
echo -n "$num1+$num2"  	#output a[1]+a[2] aka 1+1
sum=2
for((i=1;i<=8;i++))		#loop until a[10] is calculated
do
tmp=$(expr $num1 + $num2) 	#tmp=a[i]=a[i-2]+a[i-1]
echo -n "+$tmp"		#output a[i]
((num1=num2))			#exchange
((num2=tmp))
sum=$(expr $sum + $tmp) 		#sum=Sigma(a[i])
done
echo "=$sum" 			#output sum

输出结果:
在这里插入图片描述

  1. 程序2:
echo -n please input num:
read num			#read number
echo -n The num is " "	
while [ $num -gt 0 ] 	#loop until number=0
do				
sd=$(($num % 10))			#get the ones in digit
echo -n "$sd"
num=$(($num/10))			#number=number/10
done				#loop
Echo

输出结果:
在这里插入图片描述

  1. 程序3
mkdir -m 754 /home/userdata	
#make new directory on file /home/userdata with owner:rwx group user:rx other user:r
i=1
temp=/home/userdata/user
while [ $i -le 5 ]		#loop until i=5
do
mkdir -m 754 $temp$i		#make new directory=/home/userdata/useri
let "i=i+1" 			#i++
Done

输出结果:
在这里插入图片描述
之后使用rm -rf /home/userdata删除文件夹

  1. 程序4
#!/bin/bash
#fileName:shall
#note:This is the first title
#function:input an expretion and answer, I will tell you right or wrong 
#You can try it as many times as you like.
echo Hello! @_@
echo Welcome to the calculate testing!
echo You can input an expretion such as 2*2 or 3+1, and input the answer
echo I will tell you whether you are right or wrong
echo You can input 'q' to exit.
echo "Now let's begin!"
number1=0;
str='\*'
echo Input the first number:
	read number1				#read number1 first, q included
while [ "$number1" != 'q' ]			#loop until number=q
do
	ch=true					#set ch as check *or \*
	echo Input the operator type:
	read type
	echo Input the seconde number:
	read number2
	echo Input the answer:
	read yourAnswer
	case $type in
	+) 	myAnswer=`expr $number1 + $number2`;;	#use expr to calculate
	-) 	myAnswer=`expr $number1 - $number2`;;
	'\*') 	myAnswer=`expr $number1 \* $number2`;;
	'*' ) 	echo 'ERROR!' 
		echo '* is wrong, \* is right'
		ch=false						
		#use * to multiplication is wrong
		myAnswer=`expr $number1 \* $number2`
		;;
	/)
	if [ $number2 -eq 0 ]
	then
		echo 'Sorry! :-('
		echo '0 cannot be the divisor'
		continue
	else
		myAnswer=`expr $number1 / $number2`
	fi;;
	
	esac						#escape case 
	if [[ $myAnswer == $yourAnswer && $ch == true ]] #if alright
	then
		echo You are right!
		echo Input 'q' to exit or try again!
	else								#if answer is wrong or use *
		echo ':-(' Sorry!
		echo You are wrong!
		echo The right answer is
		if [[ $type == '*' ]]
		then
			type='\*'
		fi
		echo "$number1 $type $number2 = $myAnswer"	
		echo 'input q to exit or try again!'
	fi
unset number1
unset number2
unset myAnswer
unset yourAnswer
echo Input the first number:
read number1
Done

输出结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

六、实验总结

1.写出实验报告。
2.分析实验程序逻辑,并给定代码注释,分析实验结果及实验过程中遇到的问题。
实验过程遇到的问题:在执行最后一个试验时,关于“”和“*”的正则表达式问题严重影响到了程序的正常运行,令其在判断正确的expr语句中的乘号时出现了较大的问题。在经过翻阅CSDN网站中关于Linux的正则表达式后找到了关于“”和“*”正则表达式的处理方式,从而解决了实验遇到的困难。

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Knight_V_Schumacher

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

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

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

打赏作者

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

抵扣说明:

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

余额充值