云原生-Shell综合练习

1.打印一个给定数字的反序,如输入10572,输出27501,如果没有输入数据,应该抛出错误和使用脚本说明

#!/bin/bash
read -p "please input a num:" num
if [ -z $num ]
then
  echo "Error!The input cannot be empty."
elif ! expr $num + 1 &> /dev/null
then
  echo "Error!The input must be integer."
else
  echo $num > /tmp.txt
  rev /tmp.txt
fi
rm -f /tmp.txt

ps:也可以使用除10余数法,将待输入的每位数字过滤出来后再使用reverse进行转置,核心代码如下:

#!/bin/bash
    while [ $num -ge 10 ]
    do
        echo -n `expr $num % 10`
        let num/=10
    done
    echo $num

2.写出shell函数RevertInput,函数必须获取三个参数,然后将三个参数倒叙echo打印出来,函数必须检查参数个数的合法性,如果参数非法,则打印"Illegal parameters",对于下面的输入:ReverInput "this is para1" para2 para3 应该输出:para3 para2 this is para1

猜你喜欢

转载自blog.csdn.net/AChain1117/article/details/130140890