bash brace expansion based on variable content not working

主宰稳场 提交于 2020-12-27 05:39:21

问题


how can i make bash expand whatever it finds in a variable i pass to mkdir? so far i've tried using eval and bash -c, but nothing seems to work

LEVEL_1=1,2,3
LEVEL_2=a,b,c

DATA_L1="/tmp/{$LEVEL_1}"
DATA_L2="$DATA_L1/{$LEVEL_2}"

for LINE in $(cat file.txt) ; do
  #"cat" here returns values like 
  #$DATA_L2/yy/data

  mkdir -pv $LINE #it actually contains e.g. this $DATA_L2/yy/data
done

i would expect that this will expand to

mkdir -p /tmp/1/a/yy/data
mkdir -p /tmp/2/a/yy/data
mkdir -p /tmp/3/a/yy/data
mkdir -p /tmp/1/b/yy/data
mkdir -p /tmp/2/b/yy/data
mkdir -p /tmp/3/b/yy/data
mkdir -p /tmp/1/c/yy/data
mkdir -p /tmp/2/c/yy/data
mkdir -p /tmp/3/c/yy/data

回答1:


I suspect your problem is that you have two layers of expansion, even though you don't show that in your question. That is, you show:

mkdir -pv $DATA_L2/yy/data

And this would work if you simply added an eval to it:

eval mkdir -pv $DATA_L2/yy/data

But inside your loop, you're not actually running the above command. I think you're running something like:

mkdir -pv $LINE

If we stick an echo in front of that mkdir, we can see that:

echo mkdir -pv $LINE

Results in:

mkdir -pv $DATA_L2/yy/data

And:

eval echo mkdir -pv $LINE

Results in:

mkdir -pv /tmp/{1,2,3}/{a,b,c}/yy/data

If you double the eval:

eval eval echo mkdir -pv $LINE

You get what you are looking for:

mkdir -pv /tmp/1/a/yy/data /tmp/1/b/yy/data /tmp/1/c/yy/data /tmp/2/a/yy/data /tmp/2/b/yy/data /tmp/2/c/yy/data /tmp/3/a/yy/data /tmp/3/b/yy/data /tmp/3/c/yy/data

But a double eval is a really good sign that need to rethink your approach to the problem.




回答2:


I suspect this is an XY problem, but one option may be arrays and for loops, similar to this:

numbers=(1 2 3)
letters=(a b c)

dir="/tmp"

for letter in "${letters[@]}"; do
    for number in "${numbers[@]}"; do
        echo mkdir -pv "$dir/$number/$letter/yy/data"
    done
done

Output:

mkdir -pv /tmp/1/a/yy/data
mkdir -pv /tmp/2/a/yy/data
mkdir -pv /tmp/3/a/yy/data
mkdir -pv /tmp/1/b/yy/data
mkdir -pv /tmp/2/b/yy/data
mkdir -pv /tmp/3/b/yy/data
mkdir -pv /tmp/1/c/yy/data
mkdir -pv /tmp/2/c/yy/data
mkdir -pv /tmp/3/c/yy/data


来源:https://stackoverflow.com/questions/57478499/bash-brace-expansion-based-on-variable-content-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!