Sponsored Content
Top Forums Shell Programming and Scripting Assigning variable from command outputs to shell Post 302329445 by staze on Saturday 27th of June 2009 10:10:38 PM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
groupmod(1M)															      groupmod(1M)

NAME
groupmod - modify a group on the system SYNOPSIS
alternate_password_file] gid name] userlist] group DESCRIPTION
The command modifies a group on the system by altering the appropriate entry in the file. The command must be used with the group argument, which is the name of the group to be modified. The name consists of a string of print- able characters that may not include a colon or newline Refer to usergroupname(5) to understand the functionality changes with the Numeric User Group Name feature. Options The command uses the following options: Specify the path to the alternate password file. When this option is specified, user information is looked up in this file, instead of If specified, the option should used as the first option. Change the value of the group ID to gid. gid must be a non-negative decimal integer less than as defined in the header file. Allow the gid to be non-unique (that is, a duplicate). Change the name of the group to name. name consists of a string of printable characters that may not include a colon or newline Add userlist to the supplementary group membership list of group. It should be used in conjunction with option. Modify the supplementary group membership list of group to userlist. It should be used in conjunction with option. Delete userlist from the supplementary group membership list of group. It should be used in conjunction with option. Specifies the list of users for modifying the supplementary group membership list of the group. When options and options are not specified, is assumed. NIS The command is aware of NIS user entries. Only local groups may be modified with Attempts to modify an NIS group will result in an error. NIS groups must be administered from the NIS server. If is used on a system where NIS is installed, it may fail with the error (return value 6) if the group specified is an NIS group (see group(4)). However, NIS groups are checked when verifying uniqueness of the new gid or new group name, which may result in the above error, or the error (return value 4). RETURN VALUE
The command exits with one of the following values: No error. Invalid command syntax. Invalid argument supplied to an option. gid is not unique (when is not used). group does not exist. group is not unique. Cannot modify the file. file or file busy. Another command may be modifying the file. Unable to open file or the file is non-existent. Exceeding permissible limit of maximum members in a group. The file is not modified. EXAMPLES
Change the group ID of the group to in the file if the group exists. This is done even if the group ID is already in use. Change the name of to in the file if the group does not already exist. WARNINGS
Because many users try to write to the file simultaneously, a password locking mechanism was devised. If this locking fails after subse- quent retrying, terminates. FILES
SEE ALSO
users(1), groupadd(1M), groupdel(1M), logins(1M), useradd(1M), userdel(1M), usermod(1M), group(4), usergroupname(5). STANDARDS CONFORMANCE
groupmod(1M)
All times are GMT -4. The time now is 04:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy