簡體   English   中英

如何在Unix Shell中獲取嵌套在同一配置文件中的變量的值

[英]How to get value of variable nested in same config file in unix shell

我有定義變量的配置文件

A=b c d
b=BALL
c=CAT
d=DOG

這是我的腳本:

for word in $A
do
    for config in $word
    do
        echo $config
    done
    echo "End of nested for loop"
done

以上印刷品

b
c
d

我需要bc和d的值

Ball
CAT 
DOG

您必須訪問變量所指向的變量,而不是變量本身:

for word in $A
do
    for config in ${!word}
    do
        echo $config
    done
    echo "End of nested for loop"
done

在這里您可以找到更多信息

您可以使用以下內容:

#!/bin/bash

A="b c d"
b=BALL
c=CAT
d=DOG


for word in $A
do
    for config in $word
    do
        echo ${!config}
    done
    echo "End of nested for loop"
done

從邏輯上講,您只有一個循環,而沒有嵌套循環,因此您也應該僅實現一個循環:

for variable in $A
do
  echo "${!variable}"
done

順便說一句,如果您只是在bash中執行它,那么您將需要在該賦值周圍用引號引起來:

A='a b c'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM