Register
It is currently Wed Jul 23, 2014 9:49 pm

concatenate 2 variables in dialog using awk in for loop


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Wed Nov 23, 2011 5:23 am   

Joined: Wed Nov 23, 2011 5:19 am
Posts: 5
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.... :)


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 6:26 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Hi,

why use awk, bash can do it
Code:
$ 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"
$ oIFS="$IFS"; IFS=" ,"; for i in $accessgroups $restaccessgroups; do var="${i}_descr"; echo "${!var}"; done; IFS="$oIFS"
Description of group a
Description of group b
Description of group f
Description of group g
you might have to adapt this to your needs.


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 6:44 am   

Joined: Wed Nov 23, 2011 5:19 am
Posts: 5
OK...I am trying to figure out how to integrate this to the dialog command...please help (again :) )


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"

Code:
dialog--separate-output --output-separator "," --checklist blah 20 80 10 <item> <description> <on/off> <item> <description> <on/off> ...
(see below)

the bundle "<item> <description> <on/off>" can be replaced by one (or more) commands. (see my example in 1st Post)

Code:
<on/off>    <item>     <description>
(*)         group_a    Description of group_a
(*)         group_b    Description of group_b
( )         group_f    Description of group_f
( )         group_g    Description of group_g


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 7:10 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Code:
dialog --checklist blah 20 80 10 $(IFS=" ,"; pos=on; for i in $accessgroups \# $restaccessgroups; do [ "$i" = "#" ] && pos=off || echo "$i ${i}_descr $pos"; done)


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 7:15 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
This works with kdialog:
Code:
eval dialog --checklist --separate-output $(oIFS="$IFS"; IFS=" ,"; for i in $accessgroups $restaccessgroups; do var="${i}_descr"; echo $i "'${!var}'" off ; done; IFS="$oIFS")

You might need to add, remove or shuffle some information for dialog.


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 7:17 am   

Joined: Wed Nov 23, 2011 5:19 am
Posts: 5
Watael wrote:
Code:
dialog --checklist blah 20 80 10 $(IFS=" ,"; pos=on; for i in $accessgroups \# $restaccessgroups; do [ "$i" = "#" ] && pos=off || echo "$i ${i}_descr $pos"; done)


Works! You rule :))


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 8:27 am   

Joined: Wed Nov 23, 2011 5:19 am
Posts: 5
I might be burned-out, and I apologize.

Code:
dialog --checklist blah 20 80 10 $(IFS=" ,"; pos=on; for i in $accessgroups \# $restaccessgroups; do [ "$i" = "#" ] && pos=off || echo "$i ${i}_descr $pos"; done)

worked once, and never again. (?)

Below my Script

Code:
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"
dialog --checklist blah 20 80 10 $(IFS=" ,"; pos=on; for i in $accessgroups \# $restaccessgroups; do [ "$i" = "#" ] && pos=off || echo "$i ${i}_descr $pos"; done)


Top
 Profile  
 PostPosted: Wed Nov 23, 2011 8:30 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
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[@]}"


Or, if you want to use one array, only:

Code:
IFS=, read -ra grps <<< "${accessgroups},${restaccessgroups}"

for grp in "${grps[@]}"; do
    grp_descr=${grp}_descr
   
    if [[ $accessgroups = *"$grp"* ]]; then
        status=on
    fi 
   
    dialog_list+=("$grp" "${!grp_descr}" $status)
    status=off
done

dialog --separate-output \
       --output-separator , \
       --checklist blah 20 80 10 "${dialog_list[@]}"


Top
 Profile  
 PostPosted: Thu Nov 24, 2011 2:33 am   

Joined: Wed Nov 23, 2011 5:19 am
Posts: 5
thanks, worked!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP