如何在 shell 中转换变量中的字符串

How to convert a string in variable in shell

set lamdrills = ("drill_1-2" drill_2-3")
set drill_1_2_fill_layers = ("cvf1") #input from other files
set span = `echo "$lam_drl" | cut -f2 -d'_'`
set startspan = `echo "$span" | cut -f1 -d'-'`
set endspan = `echo "$span" | cut -f2 -d'-'`
set fill_layers = `echo drill_"$startspan"_"$endspan"_fill_layers`

我想将 drill_1_2_fill_layers 分配给 fill_layers

你好像在找

set lamdrills = ("drill_1-2" "drill_2-3")  # typo fixed
foreach drill ($lamdrills)                 # loop over array
  set fill_layers = drill_`echo $drill | sed 's/.*_//;s/-/_/'`_fill_layers
  # ... use this variable
end

C shell 比较狡猾,不受欢迎(for good reasons);也许您想改用 Bourne 兼容 shell?

for drill in "drill_1-2" "drill_2-3"; do
  fill_layers="drill_$(echo "$drill" | sed 's/.*_//;s/-/_/')_fill_layers"
  # ... use "$fill_layers"
done

当然,如果您使用的标签始终使用相同的约定和标点符号模式,那就更好了。