Problem with test syntax in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with test syntax in script
# 1  
Old 09-04-2012
Problem with test syntax in script

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

Code:
# 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

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.
# 2  
Old 09-04-2012
` is not the same as '
# 3  
Old 09-04-2012
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.

Code:
        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"


Last edited by Corona688; 09-04-2012 at 02:13 PM..
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-04-2012
@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.
This User Gave Thanks to zaxxon For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with if-else syntax

I'm calling the following if-else from nawk. But I keep getting an error at the "else". I've tried putting more brackets and ; but still I get complaints about the "else". Any ideas ? Thanks, wbrunc BEGIN { FS = "," ; OFS = "," } { if ( $8 ~ /A/ && $9 == B ) $1="4/29/2013" ; $2="J.Doe"... (2 Replies)
Discussion started by: wbrunc
2 Replies

2. Shell Programming and Scripting

Test shell script (PROBLEM)

Dears , kindly I wanna do test for one KSH script to know how is it working , the problem that I'm facing is whenever put "sh -x ./my_script.sh" the output seems very long & although I tried to to redirect it to files as it shown , but it failed :eek: :- sh -x ./my_script.sh >... (2 Replies)
Discussion started by: arm
2 Replies

3. Shell Programming and Scripting

syntax for IF test or AWK for octal

Using korn shell. I am reading a file line by line. If a record has a carriage return (octal 015) then I append the second record to the first record. Not all records have a carriage return. I have the unix shell script working with grep, but when my file has +100,000 records it runs slow. I would... (3 Replies)
Discussion started by: sboxtops
3 Replies

4. Shell Programming and Scripting

K script not running / syntax problem

Hi, I have following K script, that is in rc0.d directory to be run on shutdown. It's linked using the 'ln' command to init.d.Can anyone tell me as to why it's not running, or if i have incorrect syntax?I get: "test: argument expected type=uname -p + " When running it with sh -x ... (8 Replies)
Discussion started by: darrlaw
8 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

syntax error for if statment test expression

Hi what's the correct way of writing if 1)if "$time_diff" -gt 5 then echo "killing hung process \n" fi 2)if test $time_diff -gt 5 then echo "killing hung process \n" fi where -time_diff=$(($Sam - $current_min)) and current_min=`date +%M` infact both are giving Syntax... (1 Reply)
Discussion started by: Anteus
1 Replies

8. Shell Programming and Scripting

syntax error in shell test: unknown operator

Hi All, can some one figure out the syntax issue here. How to overcome this? #!/bin/sh $ HFR_MAIL=NO $ PRP_MAIL=NO $ MC_MAIL=NO $ if && && ]; then > echo "NO " > else > echo "YES" > fi test: unknown operator NO $ if && && ]; then > echo "NO" > else > echo "YES" >... (4 Replies)
Discussion started by: shellscripter
4 Replies

9. Shell Programming and Scripting

syntax problem

dear friends, I have a large size file containg two fields data like this *** **** 122 222 ***** ***** ***** ***** 232 233 i have file like this. i want to remove blank lines from file . i think awk is servive this problem i wrote a awk command but the error is... (3 Replies)
Discussion started by: rajan_ka1
3 Replies

10. Shell Programming and Scripting

problem with script and syntax error message

Hi I have the following script and have problem debugging the problems. The function of this script is to make sure the entire file is being received (the filesize of a data is not changing after 20 seconds) and start moving the file to another directory. This script should be started every 30mins.... (5 Replies)
Discussion started by: ReV
5 Replies
Login or Register to Ask a Question