1.linux系统下shell脚本用case语句编写四则运算 2.linux系统下shell脚本输入数字串。进行反序输出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.linux系统下shell脚本用case语句编写四则运算 2.linux系统下shell脚本输入数字串。进行反序输出相关的知识,希望对你有一定的参考价值。
1.linux系统下shell脚本用case语句编写四则运算 2.linux系统下shell脚本输入数字串。进行反序输出
1.四则运算就是加减乘除,输入莫一项运算符,进行该运算。
[root@liuxiting testdir]# cat calculator.sh
#!/bin/bash
echo "usage: 1+3 <Enter> ,q <Enter> is quit"
while [ 1 ]
do
read -p "->>" str 1>>/dev/null
a=`echo $str |awk -F '+|-|*|/' 'print $1'`
if [ $a == q ]
then
break
fi
b=`echo $str |awk -F '+|-|*|/' 'print $2'`
o=`echo $str |grep -o "[[:punct:]]" | grep -v "\."`
case $o in
+) awk 'BEGINprintf " =%.2f\n",'$a'+'$b'';;
-) awk 'BEGINprintf " =%.2f\n",'$a'-'$b'';;
\*) awk 'BEGINprintf " =%.2f\n",'$a'*'$b'';;
/)if [ $b -eq 0 ]
then
echo 0 Can NOT be denominator!
continue
fi
awk 'BEGINprintf " =%.2f\n",'$a'/'$b'';;
#^) awk 'BEGINprintf " =%.2f\n",'$a'**'$b'';;
*) echo error;;
esac
done
刚刚写的倒序输出:
[root@liuxiting testdir]# cat daoXuShuChu.sh
#!/bin/bash
echo "usage: 123456 <Enter>, q <Enter> is quit"
while [ 1 ]
do
echo -n "Pleasw enter number : "
read n
if [ $n == 'q' ]
then
break
fi
sd=0
rev=""
on=$n
echo "$n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
rev=$( echo $rev$sd)
done
echo "$on in a reverse order $rev"
done 参考技术A echo "123+234"|bc
直接用系统的bc帮你运算好,直接给你一个结果
需要将结果复制给变量
直接
vvv=·echo "123+234"|bc·
Linux Shell脚本编程case条件语句
1,判断一个数字是否则在1,2,3之中.
#!/bin/bash read -p "pls input a number:" n case "$n" in 1) echo "变量是1" ;; 2) echo "变量是2" ;; 3) echo "变量是3" ;; *) echo "pls input a number between 1 and 3" exit; esac
2,多级if语句改写
#!/bin/bash read -p "pls input a number:" n if [ $n -eq 1 ]; then echo "$n是变量1" elif [ $n -eq 2 ]; then echo "$n是变量2" elif [ $n -eq 3 ]; then echo "$n是变量3" else echo "pls input a number between 1 and 3" fi
3,if..else嵌套,实现
#!/bin/bash read -p "pls input a number:" n if [ $n -eq 1 ]; then echo 1 else if [ $n -eq 2 ]; then echo 2 elif [ $n -eq 3 ]; then echo 3 else echo "pls input a number [1-3]" fi fi
4,判断 分数等级
#!/bin/bash read -p "pls input score to test level:" score if [ $score -ge 90 ]; then echo "优秀" elif [ $score -ge 80 ]; then echo "良好" elif [ $score -ge 70 ]; then echo "中等" elif [ $score -ge 60 ]; then echo "及格" else echo "不及格" fi
5,给文字加颜色
#!/bin/bash RED_COLOR=‘\e[1;31m‘ GREEN_COLOR=‘\e[1;32m‘ YELLOW_COLOR=‘\e[1;33m‘ BLUE_COLOR=‘\e[1;34m‘ RESET_COLOR=‘\e[0m‘ echo ‘ 1, 悟空 2, 八戒 3, 唐僧 4, 白龙马 ‘ read -p "pls input a number:" n case $n in 1) echo -e "${RED_COLOR}悟空${RESET_COLOR}" ;; 2) echo -e "${GREEN_COLOR}八戒${RESET_COLOR}" ;; 3) echo -e "${YELLOW_COLOR}唐僧${RESET_COLOR}" ;; 4) echo -e "${BLUE_COLOR}白龙马${RESET_COLOR}" ;; *) echo "you need input a number in {1|2|3|4}" esac
另一种写法:
#!/bin/bash RED_COLOR=‘\e[1;31m‘ GREEN_COLOR=‘\e[1;32m‘ YELLOW_COLOR=‘\e[1;33m‘ BLUE_COLOR=‘\e[1;34m‘ RESET_COLOR=‘\e[0m‘ function menu(){ cat <<END 1, 悟空 2, 八戒 3, 唐僧 4, 白龙马 END } function select_type(){ read -p "pls input a number:" n case $n in 1) echo -e "${RED_COLOR}悟空${RESET_COLOR}" ;; 2) echo -e "${GREEN_COLOR}八戒${RESET_COLOR}" ;; 3) echo -e "${YELLOW_COLOR}唐僧${RESET_COLOR}" ;; 4) echo -e "${BLUE_COLOR}白龙马${RESET_COLOR}" ;; *) echo "you need input a number in {1|2|3|4}" esac } function main(){ menu select_type } main
读取命令行参数,给内容设置颜色
#!/bin/bash RED_COLOR=‘\e[1;31m‘ GREEN_COLOR=‘\e[1;32m‘ YELLOW_COLOR=‘\e[1;33m‘ BLUE_COLOR=‘\e[1;34m‘ RESET_COLOR=‘\e[0m‘ if [ $# -ne 2 ]; then echo "Usage $0 input {red|green|yellow|blue}" exit fi case $2 in red) echo -e "${RED_COLOR}$1${RESET_COLOR}" ;; green) echo -e "${GREEN_COLOR}$1${RESET_COLOR}" ;; yellow) echo -e "${YELLOW_COLOR}$1${RESET_COLOR}" ;; blue) echo -e "${BLUE_COLOR}$1${RESET_COLOR}" ;; *) echo "usage $0 input {red|green|yellow|blue}" exit esac
修改成函数调用方式
#!/bin/bash function toColor(){ RED_COLOR=‘\e[1;31m‘ GREEN_COLOR=‘\e[1;32m‘ YELLOW_COLOR=‘\e[1;33m‘ BLUE_COLOR=‘\e[1;34m‘ RESET_COLOR=‘\e[0m‘ if [ $# -ne 2 ]; then echo "Usage $0 input {red|green|yellow|blue}" exit fi case $2 in red) echo -e "${RED_COLOR}$1${RESET_COLOR}" ;; green) echo -e "${GREEN_COLOR}$1${RESET_COLOR}" ;; yellow) echo -e "${YELLOW_COLOR}$1${RESET_COLOR}" ;; blue) echo -e "${BLUE_COLOR}$1${RESET_COLOR}" ;; *) echo "usage $0 input {red|green|yellow|blue}" exit esac } function main(){ toColor $1 $2 } main $*
以上是关于1.linux系统下shell脚本用case语句编写四则运算 2.linux系统下shell脚本输入数字串。进行反序输出的主要内容,如果未能解决你的问题,请参考以下文章