Find the answer to your Linux question:
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By atreyu
Hi, I have a variable $accessgroups, which has a series of comma-delimited values, like: group_a,group_b The other variable $restaccessgroups has: group_f,group_g I also have another set of variables: group_a_descr=Description of ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    6

    concatenate 2 variables in dialog using awk while in for loop

    Hi,

    I have a variable $accessgroups, which has a series of comma-delimited values, like:

    group_a,group_b

    The other variable $restaccessgroups has:

    group_f,group_g

    I also have another set of variables:

    group_a_descr=Description of group a
    group_b_descr=Description of group b
    group_f_descr=Description of group f
    group_g_descr=Description of group g


    I now want a dialog with group_a and the value of $group_a_descr

    Code:
    dialog --separate-output --output-separator "," --checklist blah 20 80 10 `echo $accessgroups | awk -F, '{ for (i = 1; i < NF; ++i ) print $i " " ${i}_descr " on"}'` `echo $restaccessgroups | awk -F, '{ for (i = 1; i < NF; ++i ) print $i " " ${i}_descr " off"}'`
    This gives me

    Code:
    (*)   group_a    group_a,group_b
    (*)   group_b    group_a,group_b
    ( )   group_f    group_f,group_g
    ( )   group_g    group_f,group_g
    What I want:

    Code:
    (*)   group_a    Description of group_a
    (*)   group_b    Description of group_b
    ( )   group_f    Description of group_f
    ( )   group_g    Description of group_g
    I have tried:

    awk -F, '{ for (i = 1; i < NF; ++i ) print $i " " $i_descr " on"}'
    awk -F, '{ for (i = 1; i < NF; ++i ) print $i " " ${$i}_descr " on"}'
    awk -F, '{ for (i = 1; i < NF; ++i ) print $i " ${i}_descr" " on"}'

    with no success.

    I am open to alternatives to awk....

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    I couldn't figure out how to do that w/awk. To make it easier, I'd put things into variables first, then stuff them all into a single variable and pass that variable to dialog, e.g.:

    Code:
    #!/bin/bash
    accessgroups='group_a,group_b'
    restaccessgroups='group_f,group_g'
    
    group_a_descr='Description of group a'
    group_b_descr='Description of group b'
    group_f_descr='Description of group f'
    group_g_descr='Description of group g'
    
    for group in $(echo $accessgroups|sed -e 's|,| |g'); do
    #  echo "group: $group"
      eval descr=$(echo \$${group}_descr)
    #  echo "descr: $descr"
      stuff="$stuff $group '$descr' on"
    done
    
    for group in $(echo $restaccessgroups|sed -e 's|,| |g'); do
    #  echo "group: $group"
      eval descr=$(echo \$${group}_descr)
    #  echo "descr: $descr"
      stuff="$stuff $group '$descr' off"
    done
    #echo "stuff: '$stuff'"
    
    text='blah'
    height=20
    width=80
    list_ht=10
    eval dialog --checklist $text $height $width $list_ht $stuff
    haraldboehmecke likes this.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    6
    Hi,

    Thanks for the answer!

    After evaluating various alternatives, the following worked best for me (basically the same you said):

    Code:
    #!/bin/bash
    accessgroups='group_a,group_b'
    restaccessgroups='group_f,group_g'
    
    group_a_descr='Description of group a'
    group_b_descr='Description of group b'
    group_f_descr='Description of group f'
    group_g_descr='Description of group g'
    
    
    IFS=, read -ra grps <<< "${accessgroups},${restaccessgroups}"
    
    for grp in "${grps[@]}"; do
        grp_descr=${grp}_descr
       
        if [[ $accessgroups = *"$grp"* ]]; then
            dialog_list_on+=("$grp" "${!grp_descr}" on)
        else
            dialog_list_off+=("$grp" "${!grp_descr}" off)
        fi 
    done
    
    dialog --separate-output --output-separator , --checklist blah 20 80 10 "${dialog_list_on[@]}" "${dialog_list_off[@]}"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...