Hi guys here i'm again with more question
The code below try to find an user and write everything about him if exist, so my problem appear on line of test, where the program test if the user has secondary groups related. The rest it's clear
# usugrup.sh lista todas las caracteristicas de un usuario dado su nombre de login.
USUARIO=$1
id $USUARIO 1>/dev/null 2>&1
ERROR=$?
if [ $ERROR -ne 0 ]
then
echo "El usuario " $USUARIO " no existe"
exit
fi
NOMBRE='id $USUARIO | cut -f1 -d" "'
echo \(UID\) y nombre: $NOMBRE
GRUPOID1='id $USUARIO | cut -f2 -d" "'
echo \(GID\) y numero de goup1: $GRUPOID1
if test 'id $USUARIO | tr " " "\n" | wc -l' -gt 2
then
GRUPOID2='id $USUARIO | cut -f3 -d" "'
echo \(GID2) y numero de group2: $GRUPOID2
fi
You don't need to run ID 5 times to get 5 different pieces of data from it. Run it once, get all the data from it the first time.
You are using single quotes, which take their contents literally, where you need backticks, which run their contents. But you don't even need that statement.
You don't need to do \( all the time, just use quotes. Variables will expand inside double-quotes, even, so you don't need to end the quotes to use a variable.
Whenever you're doing if [ `... | wc -l` -gt somevalue ] to check something, you've probably missed a simpler way. Here, since I've overwritten the $1 ... arguments, I use the special "$#" variable, the number of arguments instead.
USUARIO=$1
DATA=`id $USARIO 2>/dev/null` # Save the data, ignore the error strings
if [ $? -ne 0 ]
then
echo "El usuario $USUARIO no existe"
exit
fi
# Note that $DATA is not in quotes and MUST NOT be in quotes.
set -- $DATA # $1=NOMBRE, $2=GRUPOID, $3=GRUPOID2, etc.
echo "(UID) y nombre: $1"
echo "(GID) y numero de goup1: $2"
[ "$#" -gt 2 ] && echo "(GID2) y numero de group2: $3"
@Newer
Also please refrain from naming all your threads "Help with commands" as this will not help much when people browse or search for topics. Be a tad more inventive and descriptive. I have altered your thread's subject. Thanks.