Appending to a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending to a variable?
# 1  
Old 08-14-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove appending 0 from file variable

Dear All, i have filename RYK3201_032001002.pdf and i am using below command to get a file file_name1=$(echo $file_name | cut -d "_" -f2 | cut -d "." -f1 | cut -c -6) and then file_name2=${NewFile_NAME}_$file_name1 now the value of file_name1 will be 032001 i want to file_name1... (5 Replies)
Discussion started by: yadavricky
5 Replies

2. UNIX for Dummies Questions & Answers

Appending sed output to variable

I want to append matched output and cat the results into an variable. but I've been running into problems. sed is printing result on to screen instead of appending the output to $CAPTURE. I'm stumped...how should i fix this? contents of $TEST 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 expected... (5 Replies)
Discussion started by: jazzaddict
5 Replies

3. UNIX for Dummies Questions & Answers

appending running numbers on a variable

hi guys, would appreciate some help here. I need to append running numbers using sed onto a variable that contain a list of IP addresses. I'm basically stuck on the running number part. e.g. 1. 10.0.0.1 2. 10.0.0.2 3. 10.0.0.3 (10 Replies)
Discussion started by: jazzaddict
10 Replies

4. Shell Programming and Scripting

Appending value to variable

I have a Query! by using command cat >> file1 we can append data's to a already existing file, Similarly is it possible to append a data to a variable. Thanks in Advance!! (2 Replies)
Discussion started by: gwgreen1
2 Replies

5. Shell Programming and Scripting

Appending data into a variable

Hi, I would like to know if it's possible to append data into a variable, rather than into a file. Although I can write information into a temporary file in /tmp, I'd rather if possible write into a variable, as I don't like the idea that should my script fail, I'll be polluting the server with... (5 Replies)
Discussion started by: michaeltravisuk
5 Replies

6. Shell Programming and Scripting

appending strings to variable

is it possible? as i keep reading a file, i want one particular variable to keep storing the line that i've read so far (1 Reply)
Discussion started by: finalight
1 Replies

7. UNIX for Dummies Questions & Answers

appending variable number of files

In a particular path of a server I have number of files.The files are generated every date with a date_mth stap on this.There are different files for different clients. For example in /data1 path i have X_0416_Score Y_0416_Score Z_0417_Score X_0417_Score A_0417_Score If i will run the... (1 Reply)
Discussion started by: dr46014
1 Replies

8. Shell Programming and Scripting

appending space to variable

Hi I need to write a script where there the user enters 3 input parameter variable number the program should ask the user left or right if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is... (5 Replies)
Discussion started by: viv1
5 Replies

9. Shell Programming and Scripting

appending spaces to a variable

Hi All, I have a requirement, in which i have to append some spaces to the variable, and then send it to another function. I am new to the UNIX shell programming. Ultimately the length of the string should be 40 characters. exp: Login = "rallapalli" (length = 10) i have to append 30 spaces to... (2 Replies)
Discussion started by: rallapalli
2 Replies

10. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies
Login or Register to Ask a Question