Assigning variable from command outputs to shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning variable from command outputs to shell
# 1  
Old 06-27-2009
Assigning variable from command outputs to shell

First, this is bash (3.2.17), on a Mac, 10.5.7.

What I'm trying to do is look at a list of users, and check to see if each exists. If they do, do some more stuff, if they don't, drop them into an error file.

So, my user list is:
foo - exists
bar - does not exist
blah - does not exist

K, so, here's the script:

Code:
#!/bin/bash

#This script will take two arguments, the group name, and the user list. 
#It is recommended that the group be cleared before running this script since
#it will not remove people from the group.

if [ "$#" != "2" ]; then
echo -e "Usage of the groupadd script: $0 groupname userlist\n"
exit 1
else

group="$1"
userlist="$2"

echo "Adding users in $userlist to group $group"

read -p 'Enter username with directory write : ' admin
read -p 'Enter password for user : ' -s password

echo "test user: $admin, test pass: $password"
echo "group: $group, userlist: $userlist"

for user in `cat $userlist`; do
	check=$(id -u $user | grep "no such user")
	if [ $check != NULL ]; then
		echo "blah"
		#echo "User $user does not exist!" 
	fi
done
fi

I am obviously going to do more. The issue is, the output from id ends up outputting to the shell, rather than "blah". So, in the case of bar and blah, I get:
"id: bar: no such user
id: blah: no such user"
output to the shell. Where, I'd THINK that I'd get the output:
"blah
blah"

Anyone got any idea what's wrong?

Thanks!
# 2  
Old 06-27-2009
Quote:
Originally Posted by staze
First, this is bash (3.2.17), on a Mac, 10.5.7.

What I'm trying to do is look at a list of users, and check to see if each exists. If they do, do some more stuff, if they don't, drop them into an error file.

So, my user list is:
foo - exists
bar - does not exist
blah - does not exist

K, so, here's the script:

Code:
#!/bin/bash

#This script will take two arguments, the group name, and the user list. 
#It is recommended that the group be cleared before running this script since
#it will not remove people from the group.

if [ "$#" != "2" ]; then
echo -e "Usage of the groupadd script: $0 groupname userlist\n"
exit 1
else

group="$1"
userlist="$2"

echo "Adding users in $userlist to group $group"

read -p 'Enter username with directory write : ' admin
read -p 'Enter password for user : ' -s password

echo "test user: $admin, test pass: $password"
echo "group: $group, userlist: $userlist"

for user in `cat $userlist`; do
	check=$(id -u $user | grep "no such user")


You haven't redirected standard error, only standard output:

Code:
check=$(id -u $user 2>&1 | grep "no such user")

Quote:
Code:
	if [ $check != NULL ]; then


The test will give a syntax error is there is nothing in $check; quote the variable:

Code:
if [ "$check" != NULL ]; then

However, that's not what you want; you are comparing the value of $check against the literal word NULL. What you want is:

Code:
if [ -n "$check" ]; then

Better would be:

Code:
if ! id -u "$user" >/dev/null 2>&1; then
  echo blah
fi

Quote:
Code:
		echo "blah"
		#echo "User $user does not exist!" 
	fi
done
fi

I am obviously going to do more. The issue is, the output from id ends up outputting to the shell, rather than "blah". So, in the case of bar and blah, I get:
"id: bar: no such user
id: blah: no such user"
output to the shell. Where, I'd THINK that I'd get the output:
"blah
blah"

Anyone got any idea what's wrong?

You haven't redirected standard error, only standard output:

Code:
check=$(id -u $user 2>&1 | grep "no such user")

# 3  
Old 06-28-2009
yeah, I figured out id was outputting the "no such user" to stderr just a moment ago.

Your suggestion worked brilliantly.

Thanks!

---------- Post updated at 08:40 PM ---------- Previous update was at 07:42 PM ----------

Incase anyone's interested, here's the "draft 1". It could be streamlined more, and I also plan to add the ability to delete members, and maybe compare the group against the input, and only add/remove those users that differ.

Feel free to use, though, I doubt it's worth much to people other than myself. =)

Code:
#!/bin/bash

#This script will take two arguments, the group name, and the user list. 
#It is recommended that the group be cleared before running this script since
#it will not remove people from the group.

dirnode="/LDAPv3/ldap.example.com"

if [ "$#" != "2" ]; then
echo -e "Usage of the groupadd script: $0 groupname userlist\n"
exit 1
else


echo "The following users do not exist in the directory" > ./non_existant_users.txt
group="$1"
userlist="$2"

echo "Adding users in $userlist to group $group"

read -p 'Enter username with directory write : ' admin
read -p 'Enter password for user : ' -s password

#echo "test user: $admin, test pass: $password"
echo "group: $group, userlist: $userlist"

for user in `cat $userlist`; do
	if ! id -u "$user" >/dev/null 2>&1; then
		echo "User $user does not exist!"
		non_existant="$non_existant $user"
	else
		exists="$exists $user"
	fi
done

for isuser in $exists; do
	#dseditgroup -o checkmember -m $isuser $group
	dseditgroup -o edit -n $dirnode -u $admin -P $password -a $isuser -t user $group
done

for nonuser in $non_existant; do
	echo $nonuser >> ./non_existant_users.txt
done

fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Assigning an expression to a variable in shell script

i am trying to assign the following expression to a variable in Unix shell script and want to use that variable in some other expression. But unable to get the required thing done. Please help with this.... This is the expression which i want to provide as input the variable date '+%y:%m:%d' |... (3 Replies)
Discussion started by: ssk250
3 Replies

2. Shell Programming and Scripting

[Solved] Assigning Shell variable

Hello, Need a small help to execute below script. #!/bin/bash . new.txt for no in 3 4 do echo $((uname_$no)) done new.txt contains uname_1="XXXXXX" uname_2="YYYYY" uname_3="ZZZZZ" ......... (6 Replies)
Discussion started by: prasanna2166
6 Replies

3. Shell Programming and Scripting

assigning fields variables value to shell variable

suppose I have a file named abc.txt.The contents of the file is sited below abc.txt maitree,test,test3 Using awk command can I store these 3 values in 3 different variable and in one single line command of awk. suppose variable a b c is there. I don't want like this a=`awk -F"," '{print... (2 Replies)
Discussion started by: maitree
2 Replies

4. Shell Programming and Scripting

Assigning command to a variable

I've been searching these forums for a while, but this is my first actual post, so please bear with me :) I'm writing a short script using ksh and am trying to store a command and parameters in a variable. My intention is to issue the command by calling the variable. The command will contain... (4 Replies)
Discussion started by: makodarear
4 Replies

5. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

6. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

7. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

8. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies

9. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies

10. Shell Programming and Scripting

Assigning a shell variable as a float

I'm confused as to how to handle floating point numbers in shell scripts. Is there a way to convert a number (string) read into a shell variable so that it can be used as a floating point decimal for calculation purposes? Or am I stuck with integrating C or Perl into my script? Ex: --input ... (3 Replies)
Discussion started by: spieterman
3 Replies
Login or Register to Ask a Question