【问题标题】:Shell Script no output in finding remainderShell 脚本在查找余数时没有输出
【发布时间】:2021-05-10 22:36:28
【问题描述】:
echo "Enter the number"
read $num

echo "number in reverse order is : "

ten=10

while [[ $num -gt 0 ]];
 do
    echo $((num%ten))
   
    num=`expr $num / 10`
done

我在 mac 终端上运行这个 终端上没有打印或回显。以相反顺序打印数字的问题。 请帮帮我,我已经受够了,如果有人能在这方面向我推荐一些好的内容,我将不胜感激。

【问题讨论】:

  • 您应该考虑在发帖前使用shellcheck 解决某些问题...提示:read $num 不起作用,您不要在作业中使用$

标签: linux shell unix terminal


【解决方案1】:

不管怎样,闻起来有点像家庭作业——我们开始吧:

for ((i=${#num}-1; i>=0; i--))
   do printf "%s" ${num:$i:1}
   done
echo ""

此解决方案也适用于非数字

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多