The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Appending data into a variable michaeltravisuk Shell Programming and Scripting 5 01-31-2009 04:20 AM
appending strings to variable finalight Shell Programming and Scripting 1 05-20-2008 04:50 AM
appending space to variable viv1 Shell Programming and Scripting 5 02-04-2008 07:01 AM
appending spaces to a variable rallapalli Shell Programming and Scripting 2 08-13-2007 02:23 AM
Sed - Appending a line with a variable on it eltinator Shell Programming and Scripting 4 07-30-2007 10:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-14-2007
paqman paqman is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 21
Appending to a variable?

Hey, I'm creating a custom useradd script, and I'm giving the option to add secondary groups. Basically what I want to do is ask for the name of the group, you type in the group you want to add, it assigns that group name to the variable $sgroup. Then the scripts asks if you want add another. If you say yes, then I want it to grab the next one you type in, and append it with a comma to the end of $sgroup.

So when all is said and done, if you have added three groups, then the value of $sgroup would be like this:
$sgroup="group1,group2,group3"

See what I mean, that way, when I'm doing the useradd command in my script, I can just use the -G option, and say, "-G $sgroup"

I'm trying to use a while loop to do it, but it doesn't seem to be working. How should I go about doing this?

I'll post my current code below, but you don't need to look at it if you don't want. It's kind of lengthy for such a simple thing. FYI, I know this code doesn't work, currently, if you choose to add another, it will just overwrite the old $sgroup with a new group name.

Code:
echo "Would you like to add any secondary groups to this account [y/n]:"
read cont
while [[ $cont = "y" || $cont = "yes" ]]
do
   echo "Type the name of the group, or hit enter to list all available groups:"
   read sgroup
   if [[ $sgroup = "" ]]
        then
        awk -F: '{ print $1 }' /etc/group
        echo "Type the name of the group:"
        read sgroup
        awk -F: '{ print $1 }' /etc/group | grep -qx $sgroup
        while [ $? != "0" ]
           do
           echo "That group does not exist."
           echo "Type the name of the group, or hit enter to list all available groups:"
           read sgroup
           if [[ $sgroup = "" ]]
                then
                awk -F: '{ print $1 }' /etc/group
                echo "Type the name of the group:"
                read sgroup
                awk -F: '{ print $1 }' /etc/group | grep -qx $sgroup 
           fi
           awk -F: '{ print $1 }' /etc/group | grep -qx $sgroup
           done
   else
        awk -F: '{ print $1 }' /etc/group | grep -qx $sgroup
        while [ $? != "0" ]
           do
           echo "That group does not exist."
           echo "Type the name of the group, or hit enter to list all available groups:"
           read sgroup
            if [[ $sgroup = "" ]]
                then
                awk -F: '{ print $1 }' /etc/group
                echo "Type the name of the group:"
                read sgroup
                awk -F: '{ print $1 }' /etc/group | grep -x $sgroup
            fi
            awk -F: '{ print $1 }' /etc/group | grep -x $sgroup
            done

   fi

echo "You have added $username to $sgroup.  Add another? [y/n]:"
read cont
done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0