- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Stripping out a character to use again...
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
08-06-2007 12:35 AM
08-06-2007 12:35 AM
I've been playing around with different things but can't get it to do exactly what I want.
for i in `cat /tmp/file`
do
USERNAME=$i
UID=$USERNAME | cut -c2-6
done
$UID reports back blank.
"cat /tmp/file | cut -c2-6" gives me the output I want for $UID but I can't translate this into something that will work in the shell script.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 12:43 AM
- Tags:
- command substitution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 12:45 AM
08-06-2007 12:45 AM
Re: Stripping out a character to use again...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 12:52 AM
08-06-2007 12:52 AM
Re: Stripping out a character to use again...
UID=${USERNAME#?}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 01:33 AM
08-06-2007 01:33 AM
Re: Stripping out a character to use again...
If I add a second field to /tmp/file:
a12345:Joe Bloggs,Somewhere,,
Can you suggest how I can set a COMMENT variable? Taking into account I'll need to quote it with "blah blah"
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 02:01 AM
08-06-2007 02:01 AM
Re: Stripping out a character to use again...
You could do something like this:
#!/usr/bin/sh
while read LINE
do
UID=`echo ${LINE}|awk -F":" '{print substr($1,2,6)}`
WHO=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print a[1]}'`
CMT=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print "\""a[2]"\""}'`
echo ${UID}
echo ${WHO}
echo ${CMT}
done < /tmp/file
...Note that I echoed the resulting fields only for demonstration purposes.
Notice too, that I eliminated the extra process (the 'cat') and let the shell open the file and read it directly.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 02:32 AM
08-06-2007 02:32 AM
Re: Stripping out a character to use again...
For future reference, and just to close the thread, the finished script looks like:
#!/usr/bin/sh
cat /tmp/file
while read LINE
do
UID=`echo ${LINE}|awk -F":" '{print substr($1,2,6)}`
USER=`echo ${LINE} | awk -F":" '{print substr($1,1,6)}`
CMT=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print "\""a[1]", "a[2]"\""}'`
useradd -u ${UID} -g
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 02:40 AM
08-06-2007 02:40 AM
Re: Stripping out a character to use again...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 08:06 PM
08-06-2007 08:06 PM
Re: Stripping out a character to use again...
If you look closely at JRF's loop, you need to remove your cat (that's not on the same line as the while), and then change your done to:
done < /tmp/file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 08:49 PM
08-06-2007 08:49 PM
Re: Stripping out a character to use again...
#!/usr/bin/sh
set -x
cat /tmp/file
while read LINE
do
UID=`echo ${LINE}|awk -F":" '{print substr($1,2,6)}`
USER=`echo ${LINE} | awk -F":" '{print substr($1,1,6)}`
WHO=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print a[1]}'`
CMT=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print "\""a[1]", "a[2]"\""}'`
/usr/sbin/useradd -u ${UID} -g 2005 -c ${CMT} -d
/usr/lbin/modprpw -x ${USER}
done < /tmp/file
Dennis, do I need to alter that?
I'm not getting the results I'd expect. It seems to pick up the variables fine, but running the script produces a "To many arguements" error from the useradd.
Set -x echo's the correct syntax for the useradd, and if i were to cut and paste that to the command line and run it, it works.
Take out the -c and the script would work, so I'm presuming it's something to do with the quotes.
+ cat /tmp/helpusers
a12345:Joe Bloggs,Helpdesk,,
+ 0< /tmp/file
+ read LINE
+ + echo a12345:Joe Bloggs,Helpdesk,,
+ awk -F: {print substr($1,2,6)}
UID=12345
+ + echo a12345:Joe Bloggs,Helpdesk,,
+ awk -F: {print substr($1,1,6)}
USER=a12345
+ + echo a12345:Joe Bloggs,Helpdesk,,
+ awk -F: {split($2,a,/,/);print a[1]}
WHO=Joe Bloggs
+ + echo a12345:Joe Bloggs,Helpdesk,,
+ awk -F: {split($2,a,/,/);print "\""a[1]", "a[2]"\""}
CMT="Joe Bloggs, Helpdesk"
+ /usr/sbin/useradd -u 12345 -g 2005 -c "Joe Bloggs, Helpdesk" -d /home/a12345 -s /usr/bin/sh a12345
Too Many Arguements specified
Usage: /usr/sbin/useradd [-u
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 09:23 PM
08-06-2007 09:23 PM
Re: Stripping out a character to use again...
(I assume this is now just for debugging?)
>Dennis, do I need to alter that?
Well, given the usage of the archaic `` is an anathema to me and I'm on a mission to stomp them out... ;-)
You should replace them all by $(...):
UID=$(echo ${LINE}|awk -F":" '{print substr($1,2,6)})
>set -x echo's the correct syntax for the useradd, and if i were to cut and paste that to the command line and run it, it works.
That's the problem, it shouldn't work.
You need to do two things, remove the quotes from the variable assignment.
CMT=$(echo ${LINE}|awk -F":" '{split($2,a,/,/);print a[1] ", " a[2]}')
>Take out the -c and the script would work, so I'm presuming it's something to do with the quotes.
Exactly. Here is where you need the quotes:
... -c "${CMT}"
If you are dealing with quoting problems, you should really make a script that prints its args. You'll see that your script has:
+ printenv /usr/sbin/useradd -u 12345 -g 2005 -c "Joe Bloggs, Helpdesk" -d
the count is 15
printenv
/usr/sbin/useradd
-u
12345
-g
2005
-c
"Joe
Bloggs,
Helpdesk"
-d
-s
a12345
And my script has:
+ printenv /usr/sbin/useradd -u 12345 -g 2005 -c Joe Bloggs, Helpdesk -d
the count is 13
printenv
/usr/sbin/useradd
-u
12345
-g
2005
-c
Joe Bloggs, Helpdesk
-d
-s
a12345
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2007 09:31 PM
08-06-2007 09:31 PM
Re: Stripping out a character to use again...
UID=$(echo ${LINE}|awk -F":" '{print substr($1,2,6)})
Oops, this is why `` is bad. JRF gave you some examples that had mismatched '' around the awk program and you didn't get errors. But with $() you do:
itrc_awk_passwd.sh[10]: Syntax error at line 24 : `'' is not matched.
So the line above (and the others) should end like:
UID=$(echo ${LINE}|awk -F":" '{print substr($1,2,6)}')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2007 01:43 AM
08-07-2007 01:43 AM
Re: Stripping out a character to use again...
Yes, sorry, as Dennis noted, I dropped a closing single quote from 'awk'.
It is indeed interesting that the use of the backtick (grave accent) in the context of this syntax error isn't detected, whereas the POSIX $(...) syntax exposes the error.
Without starting a religous war, I would note that you should be familiar with both syntaxes :-)) As Dennis noted, the POSIX standard deprecates the use of the grave accents for command substitution.
Regards!
...JRF...