Search code examples
shellcsh

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`

I want assign the drill_1_2_fill_layers to the fill_layers


Solution

  • It looks vaguely like you are looking for

    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
    

    The C shell is tricky and unpopular (for good reasons); perhaps you would like to use a Bourne-compatible shell instead?

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

    Of course, even better would be if the labels you use would use the same conventions and punctuation patterns consistently.