<?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 Looping question in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964878#M101488</link>
    <description>I'm trying to process a list of users in a file.&lt;BR /&gt;The format of the file is:&lt;BR /&gt;first_name last_name&lt;BR /&gt;The output from the following script&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;for USER in `cat /scripts/scan/user2.list`&lt;BR /&gt;do&lt;BR /&gt;  echo $USER&lt;BR /&gt; done&lt;BR /&gt;&lt;BR /&gt;is &lt;BR /&gt;first_name&lt;BR /&gt;last_name&lt;BR /&gt;first_name&lt;BR /&gt;last_name&lt;BR /&gt;not&lt;BR /&gt;first_name last_name like I expected.&lt;BR /&gt;&lt;BR /&gt;any ideas?  I've looked through the forums a&amp;amp; I'm stumped.  I'm not married to the for loop so any other suggestions are welcome.&lt;BR /&gt;&lt;BR /&gt;Thank you&lt;BR /&gt;&lt;BR /&gt;Gary</description>
    <pubDate>Mon, 06 Mar 2006 11:50:20 GMT</pubDate>
    <dc:creator>Gary Glick</dc:creator>
    <dc:date>2006-03-06T11:50:20Z</dc:date>
    <item>
      <title>Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964878#M101488</link>
      <description>I'm trying to process a list of users in a file.&lt;BR /&gt;The format of the file is:&lt;BR /&gt;first_name last_name&lt;BR /&gt;The output from the following script&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;for USER in `cat /scripts/scan/user2.list`&lt;BR /&gt;do&lt;BR /&gt;  echo $USER&lt;BR /&gt; done&lt;BR /&gt;&lt;BR /&gt;is &lt;BR /&gt;first_name&lt;BR /&gt;last_name&lt;BR /&gt;first_name&lt;BR /&gt;last_name&lt;BR /&gt;not&lt;BR /&gt;first_name last_name like I expected.&lt;BR /&gt;&lt;BR /&gt;any ideas?  I've looked through the forums a&amp;amp; I'm stumped.  I'm not married to the for loop so any other suggestions are welcome.&lt;BR /&gt;&lt;BR /&gt;Thank you&lt;BR /&gt;&lt;BR /&gt;Gary</description>
      <pubDate>Mon, 06 Mar 2006 11:50:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964878#M101488</guid>
      <dc:creator>Gary Glick</dc:creator>
      <dc:date>2006-03-06T11:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964879#M101489</link>
      <description>I guess it must depend on the format of your input file, which, from the output, appears to be first name on one line, followed by last name on the next line.  Can you confirm?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete</description>
      <pubDate>Mon, 06 Mar 2006 11:57:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964879#M101489</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2006-03-06T11:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964880#M101490</link>
      <description>Hi Gary:&lt;BR /&gt;&lt;BR /&gt;If your file contains separate fields, as you suggest it does, do:&lt;BR /&gt;&lt;BR /&gt;while read FIRST LAST THE_REST&lt;BR /&gt;do&lt;BR /&gt;    echo "${FIRST} and ${LAST}"&lt;BR /&gt;done &amp;lt; /scripts/scan/user2.list&lt;BR /&gt;&lt;BR /&gt;...or:&lt;BR /&gt;&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;    echo "${LINE}"&lt;BR /&gt;done &amp;lt; /scripts/scan/user2.list&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 06 Mar 2006 11:58:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964880#M101490</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-03-06T11:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964881#M101491</link>
      <description>for USER in `cat /scripts/scan/user2.list |awk '{print $0}'`&lt;BR /&gt;&lt;BR /&gt;Rgds...Geoff</description>
      <pubDate>Mon, 06 Mar 2006 11:59:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964881#M101491</guid>
      <dc:creator>Geoff Wild</dc:creator>
      <dc:date>2006-03-06T11:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964882#M101492</link>
      <description>The script is doing just as I expect it you  so one of us must be wrong. Essentially the for is parsing on any white space -- not just NL's.&lt;BR /&gt;&lt;BR /&gt;cat "/scripts/scan/user2.list" | while read USER&lt;BR /&gt;  do&lt;BR /&gt;    echo "User: ${USER}"&lt;BR /&gt;  done&lt;BR /&gt;&lt;BR /&gt;If you actually want to separate the names then:&lt;BR /&gt;&lt;BR /&gt;cat "/scripts/scan/user2.list" | while read FNAME LNAME &lt;BR /&gt;  do&lt;BR /&gt;    echo "Firstname: ${FNAME} Lastname: ${LNAME}"&lt;BR /&gt;  done&lt;BR /&gt;&lt;BR /&gt;The read statement also breaks on whitespace&lt;BR /&gt;up to a NL. The read statement has the characteristic that any remaining characters in the line are insterted into the last listed variable.</description>
      <pubDate>Mon, 06 Mar 2006 12:01:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964882#M101492</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-03-06T12:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964883#M101493</link>
      <description>As James and Geoff point out (and I missed earlier), the blank delimited first and last names will be interpreted as separate users.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete</description>
      <pubDate>Mon, 06 Mar 2006 12:02:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964883#M101493</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2006-03-06T12:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964884#M101494</link>
      <description>Hi Gary,&lt;BR /&gt;&lt;BR /&gt;Could you paste a sample of the input file that you are trying to process. That would make things clearer as to the task you are trying to accomplish.&lt;BR /&gt;&lt;BR /&gt;cheers!</description>
      <pubDate>Mon, 06 Mar 2006 12:08:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964884#M101494</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-03-06T12:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964885#M101495</link>
      <description>It does look like it's parsing on the white space.  I didn't know it would do that.&lt;BR /&gt;I'll try the suggestions listed.&lt;BR /&gt;&lt;BR /&gt;Here's a sample of the input file&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;test testing&lt;BR /&gt;System PC&lt;BR /&gt;Wiley Coyote&lt;BR /&gt;Proxy PC&lt;BR /&gt;PC Support&lt;BR /&gt;IT Training&lt;BR /&gt;&lt;BR /&gt;Thanks again.&lt;BR /&gt;Gary</description>
      <pubDate>Mon, 06 Mar 2006 13:52:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964885#M101495</guid>
      <dc:creator>Gary Glick</dc:creator>
      <dc:date>2006-03-06T13:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964886#M101496</link>
      <description>Gary,&lt;BR /&gt;&lt;BR /&gt;Doing a man on sh-posix will tell you that the field separator for shell input is the IFS (internal field separator) variable, which by default is set to blank space.&lt;BR /&gt;&lt;BR /&gt;In your script you can reset it to something other than a space (colon perhaps) and then the results will be according to your expectations. Otherwise the shell is doing its job correctly by outputting the field one per line. For example...&lt;BR /&gt;&lt;BR /&gt;=============================================&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;IFS=":"&lt;BR /&gt;for USER in `cat /scripts/scan/user2.list`&lt;BR /&gt;do&lt;BR /&gt;echo $USER&lt;BR /&gt;done&lt;BR /&gt;=============================================&lt;BR /&gt;&lt;BR /&gt;Make sure you reset the IFS back to its original value (blank space) otherwise it'll stay on for the remainder of your session and possibly impact other scripts or programs which expect default functionality from the shell.&lt;BR /&gt;&lt;BR /&gt;No need to worry though if you are executing from a shell script as the spawned subshell would exit after script completion.&lt;BR /&gt; &lt;BR /&gt;hope it helps!</description>
      <pubDate>Mon, 06 Mar 2006 16:50:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964886#M101496</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-03-06T16:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964887#M101497</link>
      <description>Hi (again) Gary:&lt;BR /&gt;&lt;BR /&gt;I am compelled to add a couple of comments regarding technique and efficiency.&lt;BR /&gt;&lt;BR /&gt;The most economical appproach to reading a file in a shell script does *not* initiate a separate process to to so, as commonly seen with solutions involving 'cat'.  It is more efficient to write:&lt;BR /&gt;&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;...&lt;BR /&gt;done &amp;lt; inputfile&lt;BR /&gt;&lt;BR /&gt;...than it is to write:&lt;BR /&gt;&lt;BR /&gt;cat inputfile | while read LINE&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;The second point is an amplification os Sandman's reference to the IFS (Inter-Field Seperator).&lt;BR /&gt;&lt;BR /&gt;Whenever you have a script in which you want to temporarily change the default value of IFS to something else, as for instance to set it to a colon character to parse the '/etc/password' file, a safe technique is to do this:&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;OLDIFS=${IFS}&lt;BR /&gt;IFS=":"&lt;BR /&gt;while read USER THE_REST&lt;BR /&gt;do&lt;BR /&gt;    echo "I see ${USER}"&lt;BR /&gt;done &amp;lt; /etc/passwd&lt;BR /&gt;IFS=${OLDIFS}&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 06 Mar 2006 17:59:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964887#M101497</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-03-06T17:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looping question</title>
      <link>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964888#M101498</link>
      <description>Wow, thank you all for your help.  I did get my  script working.  Whe while loop ended up being the best solution for me.</description>
      <pubDate>Wed, 08 Mar 2006 11:18:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/looping-question/m-p/4964888#M101498</guid>
      <dc:creator>Gary Glick</dc:creator>
      <dc:date>2006-03-08T11:18:45Z</dc:date>
    </item>
  </channel>
</rss>

