- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script help!
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 11:31 AM
12-18-2006 11:31 AM
Please hekp me to correct this script.
this script will read the file "test_userlist" and create users with 2 comments
But i always receive error below.
#!/usr/bin/sh -x
for user in `cat test_userlist`
NAME=`echo $user | awk -F\: '{print $1}'`
FULLNAME=`echo $user | awk -F\: '{print $2}'`
COUNTRY=`echo $user | awk -F\: '{print $3}'`
do useradd -m -s /usr/bin/false -g sunml -c "${FULLNAME},${COUNTRY}" $NAME
done
the content of file "test_userlist"
sally00:test00:australia
sally01:test01:australia
sally02:test02:australia
received this error when ran it
./cr_user.sh.t[2]: Syntax error at line 4 : `NAME=`echo $user | awk -F: '{print $1}'`' is not expected.
thanks in advance.
Tom
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 11:46 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 11:51 AM
12-18-2006 11:51 AM
Re: Script help!
The new way to do this is with the $(statement) syntax.
Your script for example:
#!/usr/bin/sh -x
for user in $(cat test_userlist)
do
NAME=$(echo $user | awk -F\: '{print $1}')
FULLNAME=$(echo $user | awk -F\: '{print $2}')
COUNTRY=$(echo $user | awk -F\: '{print $3}')
useradd -m -s /usr/bin/false -g sunml -c "${FULLNAME},${COUNTRY}" $NAME
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 12:07 PM
12-18-2006 12:07 PM
Re: Script help!
The use of 'cat' adds another process that isn't needed. In fact, you can do away with 'awk' and use the shell to split your line into component fields:
#!/usr/bin/sh
OLDIFS=${IFS}
IFS=":"
while read NAME FULLNAME COUNTRY X
do
useradd -m -s /usr/bin/false -g sunml -c "${FULLNAME},${COUNTRY}" $NAME
done < test_userlist
IFS=${OLDIFS}
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 12:29 PM
12-18-2006 12:29 PM
Re: Script help!
Regards,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 12:30 PM
12-18-2006 12:30 PM
Re: Script help!
the "cut" command, when it can be used is much faster than firing up awk for each line.
--- cut example ---
for user in `cat test_userlist`
do
NAME=`echo $user | cut -f1 -d:`
FULLNAME=`echo $user | cut -f2 -d:`
COUNTRY=`echo $user | cut -f3 -d:`
useradd -m -s /usr/bin/false -g sunml -c "${FULLNAME},${COUNTRY}" $NAME
done
----- end of cut example ---
BUT, you could get this done in fewer lines using the "set" command:
----- set example -------
for user in `cat test_userlist`
do
set `echo $user | sed "s/:/ /g"`
useradd -m -s /usr/bin/false -g sunml -c "${2},${3}" $1
done
---- end of set example ----
What the example above is doing is using sed to put spaces between the words so that the shell command "set" would be able to parse the contents of $user into $1, $2 and $3. Then of course those arguments used as would you have with the variables you used for the useradd command ... it's just less readable - as is it seems most things which can be done "quicker" in the shell are.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 12:31 PM
12-18-2006 12:31 PM
Re: Script help!
:-) :-) :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2006 02:13 PM
12-18-2006 02:13 PM
Re: Script help!
for user in $(cat test_userlist)
to: for user in $(< test_userlist)
It seems that Clay was not quite right in the original case. The "do" is there but it is on the wrong line and there is a bunch of missing "`".
In regards to John's comment about cut vs awk, I've found that cut has an interface that is just too complex to understand. awk is simple and doesn't have a problem with "field delimiters delimit null fields".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2006 02:51 AM
12-19-2006 02:51 AM
Re: Script help!
you're right, cut is a bit funkier especially when the delimiters are not a single character: that's why I added "when it can be used".
I use awk when the parse is more complex, but when it a simple reliable single character - I'll grab the "cut" command as it really does run much faster, especially when one is looking to parse a large number of lines (like thousands). But, I don't get to use it as often, because data these days don't seem to be laid out as rigourosly in printed record form like in the old IBM mainframe and /or old Fortran days (not that I'm getting wistful for going back to that stuff again).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2006 05:36 AM
12-19-2006 05:36 AM
Re: Script help!
never use this "," before
to: for user in $(< test_userlist)
just learn something new.
Thanks again.
Tom