以相反的顺序打印bash参数 [英] Print bash arguments in reverse order

查看:64
本文介绍了以相反的顺序打印bash参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个脚本,该脚本将接收所有参数并反向打印它们.

I have to write a script, which will take all arguments and print them in reverse.

我已经提出了解决方案,但是发现它很糟糕.您有更聪明的主意吗?

I've made a solution, but find it very bad. Do you have a smarter idea?

#!/bin/sh
> tekst.txt

for i in $* 
do
    echo $i | cat - tekst.txt > temp && mv temp tekst.txt
done

cat tekst.txt

推荐答案

可以做到这一点

for (( i=$#;i>0;i-- ));do
        echo "${!i}"
done

这使用以下内容
c循环样式
参数间接扩展 (${!i}朝页面底部)

This uses the below
c style for loop
Parameter indirect expansion (${!i}towards the bottom of the page)

$#是脚本参数的数量

这篇关于以相反的顺序打印bash参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆