- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Looping question
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
03-06-2006 03:50 AM
03-06-2006 03:50 AM
The format of the file is:
first_name last_name
The output from the following script
#!/bin/sh
for USER in `cat /scripts/scan/user2.list`
do
echo $USER
done
is
first_name
last_name
first_name
last_name
not
first_name last_name like I expected.
any ideas? I've looked through the forums a& I'm stumped. I'm not married to the for loop so any other suggestions are welcome.
Thank you
Gary
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 03:57 AM
03-06-2006 03:57 AM
Re: Looping question
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 03:58 AM
03-06-2006 03:58 AM
SolutionIf your file contains separate fields, as you suggest it does, do:
while read FIRST LAST THE_REST
do
echo "${FIRST} and ${LAST}"
done < /scripts/scan/user2.list
...or:
while read LINE
do
echo "${LINE}"
done < /scripts/scan/user2.list
Regards!
...JRF...
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 03:59 AM
03-06-2006 03:59 AM
Re: Looping question
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 04:01 AM
03-06-2006 04:01 AM
Re: Looping question
cat "/scripts/scan/user2.list" | while read USER
do
echo "User: ${USER}"
done
If you actually want to separate the names then:
cat "/scripts/scan/user2.list" | while read FNAME LNAME
do
echo "Firstname: ${FNAME} Lastname: ${LNAME}"
done
The read statement also breaks on whitespace
up to a NL. The read statement has the characteristic that any remaining characters in the line are insterted into the last listed variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 04:02 AM
03-06-2006 04:02 AM
Re: Looping question
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 04:08 AM
03-06-2006 04:08 AM
Re: Looping question
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.
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 05:52 AM
03-06-2006 05:52 AM
Re: Looping question
I'll try the suggestions listed.
Here's a sample of the input file
test testing
System PC
Wiley Coyote
Proxy PC
PC Support
IT Training
Thanks again.
Gary
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 08:50 AM
03-06-2006 08:50 AM
Re: Looping question
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.
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...
=============================================
#!/bin/sh
IFS=":"
for USER in `cat /scripts/scan/user2.list`
do
echo $USER
done
=============================================
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.
No need to worry though if you are executing from a shell script as the spawned subshell would exit after script completion.
hope it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2006 09:59 AM
03-06-2006 09:59 AM
Re: Looping question
I am compelled to add a couple of comments regarding technique and efficiency.
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:
while read LINE
do
...
done < inputfile
...than it is to write:
cat inputfile | while read LINE
...
The second point is an amplification os Sandman's reference to the IFS (Inter-Field Seperator).
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:
...
OLDIFS=${IFS}
IFS=":"
while read USER THE_REST
do
echo "I see ${USER}"
done < /etc/passwd
IFS=${OLDIFS}
...
Regards!
...JRF...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2006 03:18 AM
03-08-2006 03:18 AM