数值运算shell脚本
这次的shell案例比较简单,但有其特点。
#!/bin/sh # scriptbc - Wrapper for 'bc' that returns the result of a calculation. if [ $1 = "-p" ] ; then precision=$2 shift 2 else precision=2 # default fi bc -q << EOF scale=$precision $* quit EOF exit 0
脚本特点:
1) 脚本整体简洁明了,但功能强大,可以实现带自定义数值运算。
2)scale=$precision 是传给bc的参数,用于显示小数的位数
3)<< EOF
scale=$precision $* quit
EOF
用来给脚本传递值,类似于在bash下手动依次输入的效果。
4) precision=$2 ;shift 2 这行有意思,有了这个就可以实现脚本参数的功能,
shift 2 的意思是将输入的参数左移2位,即$1,$2先清空,$3赋值给$1,$4赋值给$2。
shell脚本echo输出不换行功能增强实例
这是第8/101个脚本Thereareasmanywaystosolvethisquirkyechoproblemastherearepagesinthisbook.Oneofmyfavoritesisverysuccinct:functionechon{echo"$*"|awk'{printf"%s"$0}'}Youmayprefertoavoidtheoverheadi
判断输入的日期是否正确的shell脚本
今儿个讲得是判断输入的日期是否正确,有利用到我们之前03这个例子中的函数下面是代码#!/bin/sh#valid-date--Validatesadate,takingintoaccountleapyearrules.exceedsDaysIn
linux shell实现判断输入的数字是否为合理的浮点数
这个shell是来判断输入的数字是否为合理的浮点数实现代码如下:#!/bin/sh#validfloat--Testswhetheranumberisavalidfloating-pointvalue.#Notethatthisscriptcannotacceptscientific(1.3