<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Stripping out a character to use again... in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050233#M94270</link>
    <description>Thanks Dennis. I've just realised I've forgotten about the Comment field that'll I'll need.&lt;BR /&gt;If I add a second field to /tmp/file:&lt;BR /&gt;&lt;BR /&gt;a12345:Joe Bloggs,Somewhere,,&lt;BR /&gt;&lt;BR /&gt;Can you suggest how I can set a COMMENT variable? Taking into account I'll need to quote it with "blah blah"&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
    <pubDate>Mon, 06 Aug 2007 08:33:23 GMT</pubDate>
    <dc:creator>RobinKing</dc:creator>
    <dc:date>2007-08-06T08:33:23Z</dc:date>
    <item>
      <title>Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050229#M94266</link>
      <description>I trying to create a simple script to add some users. Usernames are listed in /tmp/file, each line is formed like a12345, I want the numeric characters to form the UID.&lt;BR /&gt;&lt;BR /&gt;I've been playing around with different things but can't get it to do exactly what I want. &lt;BR /&gt;&lt;BR /&gt;for i in `cat /tmp/file`&lt;BR /&gt;do&lt;BR /&gt;USERNAME=$i&lt;BR /&gt;UID=$USERNAME | cut -c2-6&lt;BR /&gt;&lt;USERADD command="" in="" here="" using=""&gt;&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;$UID reports back blank. &lt;BR /&gt;&lt;BR /&gt;"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. &lt;BR /&gt;&lt;BR /&gt;Thanks!&lt;/USERADD&gt;</description>
      <pubDate>Mon, 06 Aug 2007 07:35:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050229#M94266</guid>
      <dc:creator>RobinKing</dc:creator>
      <dc:date>2007-08-06T07:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050230#M94267</link>
      <description>UID=$USERNAME | cut -c2-6&lt;BR /&gt;&lt;BR /&gt;You need to send output to the pipe:&lt;BR /&gt;UID=$(echo $USERNAME | cut -c2-6)</description>
      <pubDate>Mon, 06 Aug 2007 07:43:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050230#M94267</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-08-06T07:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050231#M94268</link>
      <description>Thanks, spot on!</description>
      <pubDate>Mon, 06 Aug 2007 07:45:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050231#M94268</guid>
      <dc:creator>RobinKing</dc:creator>
      <dc:date>2007-08-06T07:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050232#M94269</link>
      <description>I think a shell only solution to strip the first char would be:&lt;BR /&gt;UID=${USERNAME#?}</description>
      <pubDate>Mon, 06 Aug 2007 07:52:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050232#M94269</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-08-06T07:52:22Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050233#M94270</link>
      <description>Thanks Dennis. I've just realised I've forgotten about the Comment field that'll I'll need.&lt;BR /&gt;If I add a second field to /tmp/file:&lt;BR /&gt;&lt;BR /&gt;a12345:Joe Bloggs,Somewhere,,&lt;BR /&gt;&lt;BR /&gt;Can you suggest how I can set a COMMENT variable? Taking into account I'll need to quote it with "blah blah"&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Mon, 06 Aug 2007 08:33:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050233#M94270</guid>
      <dc:creator>RobinKing</dc:creator>
      <dc:date>2007-08-06T08:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050234#M94271</link>
      <description>Hi Robin:&lt;BR /&gt;&lt;BR /&gt;You could do something like this:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;    UID=`echo ${LINE}|awk -F":" '{print substr($1,2,6)}`&lt;BR /&gt;    WHO=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print a[1]}'`&lt;BR /&gt;    CMT=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print "\""a[2]"\""}'`&lt;BR /&gt;&lt;BR /&gt;    echo ${UID}&lt;BR /&gt;    echo ${WHO}&lt;BR /&gt;    echo ${CMT}&lt;BR /&gt;done &amp;lt; /tmp/file&lt;BR /&gt;&lt;BR /&gt;...Note that I echoed the resulting fields only for demonstration purposes.&lt;BR /&gt;&lt;BR /&gt;Notice too, that I eliminated the extra process (the 'cat') and let the shell open the file and read it directly.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 06 Aug 2007 09:01:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050234#M94271</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-08-06T09:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050235#M94272</link>
      <description>Thanks James, this thread will be very useful reference for the future.&lt;BR /&gt;&lt;BR /&gt;For future reference, and just to close the thread, the finished script looks like:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;cat /tmp/file&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;UID=`echo ${LINE}|awk -F":" '{print substr($1,2,6)}`&lt;BR /&gt;USER=`echo ${LINE} | awk -F":" '{print substr($1,1,6)}`&lt;BR /&gt;CMT=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print "\""a[1]", "a[2]"\""}'`&lt;BR /&gt;&lt;BR /&gt;useradd -u ${UID} -g &lt;GID&gt; -c ${CMT} -d &lt;HOMEDIR&gt; -s /usr/bin/sh ${USER}&lt;BR /&gt;&lt;/HOMEDIR&gt;&lt;/GID&gt;</description>
      <pubDate>Mon, 06 Aug 2007 09:32:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050235#M94272</guid>
      <dc:creator>RobinKing</dc:creator>
      <dc:date>2007-08-06T09:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050236#M94273</link>
      <description>Of course, with "done" to complete the loop.</description>
      <pubDate>Mon, 06 Aug 2007 09:40:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050236#M94273</guid>
      <dc:creator>RobinKing</dc:creator>
      <dc:date>2007-08-06T09:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050237#M94274</link>
      <description>&amp;gt;Of course, with "done" to complete the loop.&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;done &amp;lt; /tmp/file</description>
      <pubDate>Tue, 07 Aug 2007 03:06:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050237#M94274</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-08-07T03:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050238#M94275</link>
      <description>This is what I ended up with:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -x&lt;BR /&gt;cat /tmp/file&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;UID=`echo ${LINE}|awk -F":" '{print substr($1,2,6)}`&lt;BR /&gt;USER=`echo ${LINE} | awk -F":" '{print substr($1,1,6)}`&lt;BR /&gt;WHO=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print a[1]}'`&lt;BR /&gt;CMT=`echo ${LINE}|awk -F":" '{split($2,a,/,/);print "\""a[1]", "a[2]"\""}'`&lt;BR /&gt;&lt;BR /&gt;/usr/sbin/useradd -u ${UID} -g 2005 -c ${CMT} -d &lt;HOMEDIR&gt; -s &lt;SHELL&gt; ${USER}&lt;BR /&gt;/usr/lbin/modprpw -x ${USER}&lt;BR /&gt;&lt;BR /&gt;done &amp;lt; /tmp/file&lt;BR /&gt;&lt;BR /&gt;Dennis, do I need to alter that?&lt;BR /&gt;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. &lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;Take out the -c and the script would work, so I'm presuming it's something to do with the quotes.&lt;BR /&gt;&lt;BR /&gt;+ cat /tmp/helpusers&lt;BR /&gt;a12345:Joe Bloggs,Helpdesk,,&lt;BR /&gt;+ 0&amp;lt; /tmp/file&lt;BR /&gt;+ read LINE&lt;BR /&gt;+ + echo a12345:Joe Bloggs,Helpdesk,,&lt;BR /&gt;+ awk -F: {print substr($1,2,6)}&lt;BR /&gt;UID=12345&lt;BR /&gt;+ + echo a12345:Joe Bloggs,Helpdesk,,&lt;BR /&gt;+ awk -F: {print substr($1,1,6)}&lt;BR /&gt;USER=a12345&lt;BR /&gt;+ + echo a12345:Joe Bloggs,Helpdesk,,&lt;BR /&gt;+ awk -F: {split($2,a,/,/);print a[1]}&lt;BR /&gt;WHO=Joe Bloggs&lt;BR /&gt;+ + echo a12345:Joe Bloggs,Helpdesk,,&lt;BR /&gt;+ awk -F: {split($2,a,/,/);print "\""a[1]", "a[2]"\""}&lt;BR /&gt;CMT="Joe Bloggs, Helpdesk"&lt;BR /&gt;+ /usr/sbin/useradd -u 12345 -g 2005 -c "Joe Bloggs, Helpdesk" -d /home/a12345 -s /usr/bin/sh a12345&lt;BR /&gt;Too Many Arguements specified&lt;BR /&gt;Usage: /usr/sbin/useradd [-u &lt;UID&gt; [-o]] [-g &lt;GROUP&gt;] [-G &lt;GROUP&gt;[,&lt;GROUP...&gt;]] [-d &lt;DIR&gt;] [-s &lt;SHELL&gt;] [-c ] [-m [-k &amp;amp;lt;skel&amp;amp;lt;BR /&amp;amp;gt; dir&amp;amp;gt;]] [-f &amp;amp;lt;inactive&amp;amp;gt;] [-e &amp;amp;lt;expire&amp;amp;gt;] [-r &amp;amp;lt;yes|no&amp;amp;gt;] &amp;amp;lt;login&amp;amp;gt;&amp;amp;lt;BR /&amp;amp;gt;Usage: /usr/sbin/useradd -D [-g &amp;amp;lt;group&amp;amp;gt;] [-b &amp;amp;lt;base dir&amp;amp;gt;] [-f &amp;amp;lt;inactive&amp;amp;gt;] [-e &amp;amp;lt;expire&amp;amp;gt;] [-r &amp;amp;lt;yes|no&amp;amp;gt;]&amp;amp;lt;BR /&amp;amp;gt;+ read LINE&lt;/SHELL&gt;&lt;/DIR&gt;&lt;/GROUP...&gt;&lt;/GROUP&gt;&lt;/GROUP&gt;&lt;/UID&gt;&lt;/SHELL&gt;&lt;/HOMEDIR&gt;</description>
      <pubDate>Tue, 07 Aug 2007 03:49:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050238#M94275</guid>
      <dc:creator>RobinKing</dc:creator>
      <dc:date>2007-08-07T03:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050239#M94276</link>
      <description>cat /tmp/file&lt;BR /&gt;&lt;BR /&gt;(I assume this is now just for debugging?)&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Dennis, do I need to alter that?&lt;BR /&gt;&lt;BR /&gt;Well, given the usage of the archaic `` is an anathema to me and I'm on a mission to stomp them out...  ;-)&lt;BR /&gt;You should replace them all by $(...):&lt;BR /&gt;UID=$(echo ${LINE}|awk -F":" '{print substr($1,2,6)})&lt;BR /&gt;&lt;BR /&gt;&amp;gt;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.&lt;BR /&gt;&lt;BR /&gt;That's the problem, it shouldn't work.&lt;BR /&gt;You need to do two things, remove the quotes from the variable assignment.&lt;BR /&gt;CMT=$(echo ${LINE}|awk -F":" '{split($2,a,/,/);print a[1] ", " a[2]}')&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Take out the -c and the script would work, so I'm presuming it's something to do with the quotes.&lt;BR /&gt;&lt;BR /&gt;Exactly.  Here is where you need the quotes:&lt;BR /&gt;... -c "${CMT}"&lt;BR /&gt;&lt;BR /&gt;If you are dealing with quoting problems, you should really make a script that prints its args.  You'll see that your script has:&lt;BR /&gt;+ printenv /usr/sbin/useradd -u 12345 -g 2005 -c "Joe Bloggs, Helpdesk" -d &lt;HOMEDIR&gt; -s &lt;SHELL&gt; a12345&lt;BR /&gt;the count is 15&lt;BR /&gt;printenv&lt;BR /&gt;/usr/sbin/useradd&lt;BR /&gt;-u&lt;BR /&gt;12345&lt;BR /&gt;-g&lt;BR /&gt;2005&lt;BR /&gt;-c&lt;BR /&gt;"Joe&lt;BR /&gt;Bloggs,&lt;BR /&gt;Helpdesk"&lt;BR /&gt;-d&lt;BR /&gt;&lt;HOMEDIR&gt;&lt;BR /&gt;-s&lt;BR /&gt;&lt;SHELL&gt;&lt;BR /&gt;a12345&lt;BR /&gt;&lt;BR /&gt;And my script has:&lt;BR /&gt;+ printenv /usr/sbin/useradd -u 12345 -g 2005 -c Joe Bloggs, Helpdesk -d &lt;HOMEDIR&gt; -s &lt;SHELL&gt; a12345&lt;BR /&gt;the count is 13&lt;BR /&gt;printenv&lt;BR /&gt;/usr/sbin/useradd&lt;BR /&gt;-u&lt;BR /&gt;12345&lt;BR /&gt;-g&lt;BR /&gt;2005&lt;BR /&gt;-c&lt;BR /&gt;Joe Bloggs, Helpdesk&lt;BR /&gt;-d&lt;BR /&gt;&lt;HOMEDIR&gt;&lt;BR /&gt;-s&lt;BR /&gt;&lt;SHELL&gt;&lt;BR /&gt;a12345&lt;/SHELL&gt;&lt;/HOMEDIR&gt;&lt;/SHELL&gt;&lt;/HOMEDIR&gt;&lt;/SHELL&gt;&lt;/HOMEDIR&gt;&lt;/SHELL&gt;&lt;/HOMEDIR&gt;</description>
      <pubDate>Tue, 07 Aug 2007 04:23:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050239#M94276</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-08-07T04:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050240#M94277</link>
      <description>&amp;gt;You should replace them all by $(...):&lt;BR /&gt;UID=$(echo ${LINE}|awk -F":" '{print substr($1,2,6)})&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;itrc_awk_passwd.sh[10]: Syntax error at line 24 : `'' is not matched.&lt;BR /&gt;&lt;BR /&gt;So the line above (and the others) should end like:&lt;BR /&gt;UID=$(echo ${LINE}|awk -F":" '{print substr($1,2,6)}')</description>
      <pubDate>Tue, 07 Aug 2007 04:31:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050240#M94277</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-08-07T04:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Stripping out a character to use again...</title>
      <link>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050241#M94278</link>
      <description>Hi (again) Robin:&lt;BR /&gt;&lt;BR /&gt;Yes, sorry, as Dennis noted, I dropped a closing single quote from 'awk'.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 07 Aug 2007 08:43:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/stripping-out-a-character-to-use-again/m-p/4050241#M94278</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-08-07T08:43:54Z</dc:date>
    </item>
  </channel>
</rss>

