LINUX shell 脚本

news/2025/3/12 11:53:36/

第一部分:使用vi/vim进行shell基础程序设计

1、编写脚本实现:从键盘输入两个数,然后比较两个数大小,并将结果输出。

 #! /bin/bash
read -p "input two numbers:" n1 n2
max=$n1
min=$n2
if ((n2>max));thenmax=$n2min=$n1
fi
echo "$max>$min"

2、编写脚本计算两个整数的平方和。例:执行xx.sh 3 4时,输出结果为25

#!/bin/bashread -p "input two numbers:" n1 n2echo "The sum of the squares of $n1 and $n2 is" `expr $n1 \* $n1 + $n2 \* $n2`   
expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。

3、编写一个脚本,使用函数来判断从键盘输入的数是否为整数,若不是则重新输入。

#!/bin/bashjugment(){n=0while :doread  pexpr $p + 0 &> /dev/null
#Linux 中的/dev/null是一个空设备文件。这将丢弃写入它的任何内容,并将在读取时返回EOF。
#$?     上个命令的退出状态,或函数的返回值。if [ $? -eq 0 ]
#若输入不为整数,则返回值小于0thenecho "The number you entered is $p"echon=$pbreakelseecho "The $p you entered is not an integer"echo -ne "Please enter an integer:"echocontinuefidone}echo  "Enter an integer:"jugment
#执行函数jugmentnu=$n

注:

1$?     上个命令的退出状态,或函数的返回值。

4、编写一个脚本,实现一个简单的加减乘除运算的计算器。要求:在两个操作数之间进行,并通过命令行菜单向用户提示输入变量和运算符。

#!/bin/bashecho " ----------------------------------------"echo "|!The dai Calculator is at your service!|"echo " ----------------------------------------"echopanduan(){n=0while :doread  pexpr $p + 0 &> /dev/nullif [ $? -eq 0 ]thenecho "The number you entered is:"echo $pn=$pbreakelseecho "The $p you entered is not an integer"echo "Please enter an integer:"continuefidone}echo  "Please enter the first integer:"panduannu1=$necho  "Please enter the second integer:"panduannu2=$necho  " ------------------"
echo "|     1.add        |"
echo "|     2.subtract   |"
echo "|     3.multiply   |" 
echo "|     4.divide     |"
echo  " ------------------"while :doread -p "Please enter the algorithm you want to execute:" acase $a in"1")sum=`expr $nu1 + $nu2`echo "$nu1+$nu2=$sum"break;;"2")jian=`expr $nu1 - $nu2`echo "$nu1-$nu2=$jian"break;;"3")chen=`expr $nu1 \* $nu2`echo "$nu1*$nu2=$chen"break;;"4")chu=`expr $nu1 / $nu2`echo "$nu1/$nu2=$chu"break;;*)echo "Please chose 1、2、3、4"echo "Please select again"esacdone

5、编写脚本实现猜价格游戏。具体过程:脚本一开始运行就自动生成一个0-100之间的数来表示实际价格,然后提示用户输入猜测的价格,然后和实际价格进行比较。若高了,就提示用户猜低一点;否则,就提示用户猜高一点。以此类推,一直到用户最终猜中。在程序运行过程中还要统计用户猜测的次数,并在最后回显。程序还能根据猜测次数给用户打分,满分100,大致规则是猜的次数越少分数越高,比如只用1次就猜中就是100分。具体打分规则自己来定。

#!/bin/bashtimes=0time=10luck=$[$RANDOM%100]#$RANDOM 是0~32767s=100score=0while truedoread -p "Enter the number you guess(0-100):" acklet times++if [ $luck -eq $ack ] && [ $times -le $time ]thenecho "You guessed it, the correct answer is:$luck,You used the $times opportunity"echo "Your score is:"`expr $s - $times \* 10`breakelif [ $luck -gt $ack ]  && [ $times -le $time ]thenecho "Your $times guess is small. Your number is:$ack"elif [ $luck -lt $ack ]  && [ $times -le $time ]thenecho "You guessed big, you have used $times the chance"elseecho "Sorry, we've run out of times"echo "The right answer is:$luck"echo "Your score is:0"breakfidone

6、编写脚本,提示用户输入一个小于100的整数,并计算从1到该数之间的所有整数的和。

 #!/bin/bashread -p "Please enter an integer less than 100:" numif [ $num -eq 1 ];thenecho "And is equal to the: $num"sum=0elif [ $num -gt 1 ] && [ $num -lt 100 ];thenfor ((i=1; i<=$num; i++))dolet sum=$sum+$idoneecho "The sum from 1 to $num is $sum"elseecho "Input error!"fi

7、判断一个数是不是完数。打印出1-1000之间的完数。完数就是约数的和等于自身2倍的数。(6,28,496)

这款代码速度太慢。

#! /bin/bashfor((i=1;i<=1000;i++))dosum=0for((j=i;j>1;j--))dolet temp=i%jlet temp1=i/jif [ $temp -eq 0 ];thenlet sum+=temp1fidoneif [ $sum -eq $i ];thenecho $ifidone

这款代码特别快。

#!/bin/bashfor (( i=1; $[(2**i-1)*(2**(i-1))]<1000;i++ ))doif [ `factor $i $[2**i-1] |awk 'NF==2' |wc -l` -eq 2 ];thenecho $[(2**i-1)*(2**(i-1))]fidone

注:

  1. AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。

8、绘制一个由*”组成的菱形,菱形两个对角线上“*”的个数由用户输入。

#! /bin/bashread -p "Please enter the size of the diamond:" gfor ((i=1;i<=$g;i++))dofor ((j=$g;j>$i;j--))doecho -n " "donefor ((p=1;p<=$i;p++))doecho -n "* "doneecho ""donefor ((k=$g-1;k>=1;k--))dofor ((m=$g;m>$k;m--))doecho -n " "donefor ((l=1;l<=$k;l++))doecho -n "* "doneecho ""done


http://www.ppmy.cn/news/587123.html

相关文章

Linux: shell脚本

一、概述 sh &#xff08;shell command language&#xff09;是一种满足POSIX标准定义的脚本语言&#xff0c;有诸多具体实现&#xff08;dash&#xff0c;ksh88等&#xff09;。 bash是sh的一种实现(GNU Bourne-Again Shell)&#xff0c;并且增加了很多扩展定义。例如&…

脚本 结束 看护 linux,linux看护进程脚本

1. 看护进程脚本。工作原理是用shell不停的去查询进程&#xff0c;如果发现进程不存在则启动进程。如果用gnome启动&#xff0c;要在图形界面启动脚本。 start.sh Js代码 #! /bin/bash #program directory path PRO_PATH"/opt/bin/" #program name PRO_NAME"Rec…

shell脚本之find

shell脚本之find ​ 实时查找工具&#xff0c;通过遍历指定路径下的文件系统完成文件查找。 ​ find命令的工作方式如下&#xff1a;沿着文件层次结构向下遍历&#xff0c;匹配符合条件的文件&#xff0c;执行响应的操作。 语法&#xff1a;find [options]… [查找条件][处理…

Python用户管理系统,宠物管理系统

用户管理系统 surface """ #三引号是Python的注释符号&#xff0c;但也可以作为字符串输出 **************************************** 用户管理系统 **************************************** 1、注册新用户 2、用户登录 3、用户注销 4、用户信息显示 5、退…

linux shell脚本

在写深度学习程序的时候&#xff0c;有时候变量太多或者需要自定义一些变量的时候我们一般讲运行命令写在shell脚本里&#xff0c;命名为train.sh等 下面介绍了shell的基本命令和mmlab等中经常使用的shell操作 Shell运行 下面写一个简单的shell程序 #!/bin/bash echo "…

使用ADMT实现域用户和组的迁移

在windows server 2008 的AD维护工作中&#xff0c;我们可能需要将当前域的用户账户和组转移到另外一个域中&#xff08;这个动作我们称之为迁移&#xff09;。 在雅利安星际疫苗接种公司工作的windows 网络管理员号称不重疫苗也能顶的灵灵狗同志&#xff0c;正在为一件棘手的工…

星爷才是王道

期待已久的《大内密探灵灵狗》&#xff0c;想了好久才想出来的“稀烂”一词用作评价……看得出来&#xff0c;王晶是很想拍出某种感觉来&#xff0c;可是造作的痕迹过重&#xff0c;而收到的效果却甚微。古天乐是我很喜欢的喜剧演员&#xff0c;在稀烂的剧本下的表演也乏善可陈…

Linux watchdog配置

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、watchdog是什么&#xff1f;1.硬件看门狗2.软件看门狗 二、使用步骤1.硬件看门狗2.软件看门狗 总结 前言 好久没写文章了&#xff0c;最近遇到一个蛋疼的问…