怎样才能在一个shell脚本飞新变量的名字呢? [英] How can I generate new variable names on the fly in a shell script?

查看:146
本文介绍了怎样才能在一个shell脚本飞新变量的名字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个shell脚本动态无功名字来处理一组不同的名称的文件在一个循环如下:

I'm trying to generate dynamic var names in a shell script to process a set of files with distinct names in a loop as follows:

#!/bin/bash

SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'

for (( i = 1; i <= 2; i++ ))
do
  echo SAMPLE{$i}
done

我期望的输出:

1-first.with.custom.name
2-second.with.custom.name

但我得到了:

SAMPLE{1}
SAMPLE{2}

是在飞有可能产生变种的名字呢?

Is it possible generate var names in the fly?

推荐答案

略有变化

SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'

for (( i = 1; i <= 2; i++ ))
do
   var="SAMPLE$i"
   echo ${!var}
done

这篇关于怎样才能在一个shell脚本飞新变量的名字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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