Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable call not working 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
This script fetches users and their groups. I was trying to make a function or something where I call groups $USER_ID instead of printing it out each time.
Here is how it is now:
[tt]
cat myFile | while read LINE
do
USER_ID=`echo $LINE | awk -F: '{print $1}'`
REAL_NAME=`echo $LINE | awk -F: '{print $2}'`
i=`groups $USER_ID | awk '{print NF}'`
echo "-----------------------------------------"
if (( $i == 1 ))
then
echo "$REAL_NAME is a member of $i group:"
groups $USER_ID
else
echo "$REAL_NAME is a member of $i groups:"
groups $USER_ID
fi
done[/tt]


Here is my attempt at putting the groups $USER_ID in a variable:
[tt]
cat myFile | while read LINE
do
USER_ID=`echo $LINE | awk -F: '{print $1}'`
var=`groups $USER_ID`
REAL_NAME=`echo $LINE | awk -F: '{print $2}'`
i=`groups $USER_ID | awk '{print NF}'`
echo "-----------------------------------------"
if (( $i == 1 ))
then
echo "$REAL_NAME is a member of $i group:"
$var
else
echo "$REAL_NAME is a member of $i groups:"
$var
fi
done[/tt]

I keep getting an error message with the $var variable. Please advise the most proficient way of doing this?

 
Seems you just omit the
Code:
echo
.
Code:
    echo "$REAL_NAME is a member of $i group:"
[tt]echo[/tt]
Code:
 $var
 
I tried the [tt]echo "$var"[/tt] and still cant get it to work.

There must be a way to do this with the groups command.

I also tried making it into a function with no luck.

myfunciton() = { groups $USER_ID }

then calling myfunction such as:
if (( $i == 1 ))
then
echo "$REAL_NAME is a member of $i group:"
myfunction

 
I tried the script and it works fine with just the echo added.
What is the problem: error message during execution or unexpected output (or both) ?
Could you post a sample output ?
 
Here is what I tried:
[tt]
cat gr2 | while read LINE
do
USER_ID=`echo $LINE | awk -F: '{print $1}'`
var = `groups $USER_ID`
REAL_NAME=`echo $LINE | awk -F: '{print $2}'`
i=`groups $USER_ID | awk '{print NF}'`
echo "-----------------------------------------"
if (( $i == 1 ))
then
echo "$REAL_NAME is a member of $i group:"
echo $var
else
echo "$REAL_NAME is a member of $i groups:"
echo $var
fi
done [/tt]

Here is my output:

-----------------------------------------
Mike Jones is a member of 10 groups:

grabr[8]: var: not found
-----------------------------------------
Jim Smith is a member of 10 groups:

grabr[8]: var: not found
-----------------------------------------
 
Loose the spaces when assigning var.

change:
var = `groups $USER_ID`

to:
var=`groups $USER_ID`

Hope this helps ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top