Shell(二)入门到复杂 脚本实例(计算器)

Shell(一) 入门到复杂 自己做的各种脚本实例与解释

shell文件开头
shell文件必须以下面的行开始(必须方在文件的第一行):
#!/bin/sh
符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。
当编辑好脚本时,如果要执行该脚本,还必须使其可执行。
要使脚本可执行:
运行chmod +x filename这样才能用./filename 来运行


注释
在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。


变量
在shell编程中,所有的变量都由字符串组成,并且您不需要对变量进行声明,直接赋值就可以,应用变量的话,用$+变量名的形式。


要赋值给一个变量,您可以这样写:
a="hello world"
现在打印变量a的内容:
echo "A is:"
echo $a

有时候变量名很容易与其他文字混淆,比如:
num=2
echo "this is the $numnd"

这并不会打印出"this is the 2nd",而仅仅打印"this is the ",因为shell会去搜索变量numnd的值,
但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量:
num=2
echo "this is the ${num}nd"
这将打印: this is the 2nd

环境变量
.
由export关键字处理过的变量叫做环境变量。我们不对环境变量进行讨论,因为通常情况下仅仅在登录
脚本中使用环境变量。

这些不是系统命令,但是他们真的很重要。

管道 (|) 将一个命令的输出作为另外一个命令的输入。
grep "hello" file.txt | wc -l
file.txt中搜索包含有”hello”的行并计算其行数
在这里grep命令的输出作为wc命令的输入。当然您可以使用多个命令。
重定向 将命令的结果输出到文件,而不是标准输出(屏幕)。
> 写入文件并覆盖旧文件
>> 加到文件的尾部,保留旧文件内容。
反短斜线
使用 反短斜线 (" ` ")可以将一个命令的输出作为另外一个命令的一个命令行参数。

数值变量:

expr 变量1 运算符 变量2 [运算符 变量3]

+加法运算

-减法运算

\*乘法运算

%求模运算 相除后的余数

预定义变量是由bash程序预先定义好的一类特殊变量,用户只能使用而不能创建新的预定义变量,也不能直接为预定义变量赋值。常见预定义变量如下:

$#表示命令行中位置参数的个数

$*表示所有位置参数的内容

$?表示前一条命令执行后的返回状态,返回为0表示执行正确,任何非0表示运行出现异常

$0表示当前执行的脚本或程序名称


计算器脚本实例:

154330484.png

154331623.png

154331317.png

154332485.png

154332896.png

154333799.png

154333748.png

154334294.png

154334731.png

154335289.png


代码:(因为刚学 做的可能不怎么好这是本人自己做的,下面第二种方法附上老师做的,虽然功能都一样!但是代码量明显比我小很多!)

#!/bin/bash
#计算器
echo "----------------------"
echo "-   加法运算请输入1  -"
echo "-   减法运算请输入2 -"
echo "-  乘法运算请输入3  -"
echo "-   除法运算请输入4  -"
echo "-       -技术空创作     -"
echo "----------------------"
read -p "请选择你想要进行的运算" int
case "$int" in
1)
 while :
 do
 read -p "请输入第一个整数"  a
    expr $a+0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第一个数是$a"
       break
else
       echo "您输入的$a不是整数请重新输入"
fi
done
while :
do
 read -p "请输入第二个整数"  b
expr $b +0 &> /dev/null
if [ $? -eq 0 ]
 then
echo "第二个数是$b"
       break
else
       echo "您输入的$b不是整数请重新输入"
fi
done
sum=`expr $a + $b`
 echo "$a + $b = $sum"
;;
2)
 while :
 do
 read -p "请输入第一个整数"  c
    expr $c+0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第一个数是$c"
       break
else
       echo "您输入的$c不是整数请重新输入"
fi
done
while :
do
 read -p "请输入第二个整数"  d
expr $d+0 &> /dev/null
if [ $? -eq 0 ]
then
       echo "第二个数是$d"
       break
else
       echo "您输入的$d不是整数请重新输入"
fi
done
jian=`expr $c - $d`
 echo "$c - $d = $jian"
;;
3)
 while :
do
 read -p "请输入第一个整数"  e
    expr $e+0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第一个数是$e"
       break
else
       echo "您输入的$e不是整数请重新输入"
fi
done
while :
do
 read -p "请输入第二个整数"  f
expr $f +0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第二个数是$f"
       break
else
       echo "您输入的$f不是整数请重新输入"
fi
done
chen=`expr $e \* $f`
 echo "$e \* $f = $chen"
;;
4)
 while :
 do
 read -p "请输入第一个整数"  q
    expr $q+0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第一个数是$q"
       break
else
       echo "您输入的$q不是整数请重新输入"
fi
done
while :
do
read -p "请输入第二个整数"  w
expr $w+0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第二个数是$w"
  break
else
       echo "您输入的$w不是整数请重新输入"
fi
done
chu=`expr $q / $w`
 echo "$q / $w = $chu"
;;
esac


------------------------------------------------------------------------------------------------------

160603680.png

160603887.png

160604310.png

160604925.png

#!/bin/bash
echo " ----------------------------------"
echo "|这是一个简单的整数计算器,作者YUAN|"
echo " ----------------------------------"
echo
while :
do
read -p  "请输入一个整数: " nu
expr $nu + 0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第一个数是$nu"
       echo
       break
else
       echo "您输入的$nu不是整数请重新输入"
fi
done

while :
do
read -p  "请输入二个整数: " nu2
expr $nu2 + 0 &> /dev/null
if [ $? -eq 0 ]
 then
       echo "第二个数是$nu2"
       break
else
       echo "您输入的$nu2不是整数请重新输"
fi
done
echo  "------------------"
echo "|  1.加法          |"
echo "|  2.减法          |"
echo "|  3.乘法         |"
echo "|  4.除法         |"
echo  "------------------"
read -p "请输入您想执行的算法:" me
case $me in
       "1")
sum=`expr $nu + $nu2`
echo "$nu+$nu2=$sum"
;;
       "2")
jian=`expr $nu - $nu2`
echo "$nu-$nu2=$jian"
;;
       "3")
chen=`expr $nu \* $nu2`
echo "$nu*$nu2=$chen"
;;
       "4")
chu=`expr $nu / $nu2`
echo "$nu/$nu2=$chu"
esac

老师的代码很精简比我的代码少多了,不过刚开始都这样!慢慢来慢慢学!创意也是一种学习的方法


最后附上个自己做的猜价格小游戏:

161200139.png

161200644.png



脚本如下:

#!/bin/bash
#猜价格游戏
i=10
jiage=`expr $RANDOM % $i`
times=0
echo "商品实际价格在0-9之间,猜猜看是多少?"
while true
do
  read -p "请输入你猜测的价格数目:" INT
  let times++
  if [ $INT -eq $jiage ] ; then
      echo "恭喜你答对了,实际价格是 $jiage"
      echo " 你总共猜测了$times 次"
      exit 0
  elif [ $INT -gt $jiage ] ; then
      echo "太高了"
      else
      echo "太低了"
fi
done






Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值