Cloud Native-Shell Comprehensive Exercise

1. Print the reverse order of a given number, such as input 10572, output 27501, if there is no input data, it should throw an error and use the script instructions

#!/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: You can also use the division by 10 remainder method to filter out each digit to be input and then use reverse to transpose. The core code is as follows:

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

2. Write the shell function RevertInput. The function must obtain three parameters, and then print out the echo of the three parameters in reverse. The function must check the legality of the number of parameters. If the parameters are illegal, print "Illegal parameters". For the following input : ReverInput "this is para1" para2 para3 should output: para3 para2 this is para1

 

Supongo que te gusta

Origin blog.csdn.net/AChain1117/article/details/130140890
Recomendado
Clasificación