- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- while or for loop
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
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
10-16-2007 12:18 PM
10-16-2007 12:18 PM
while or for loop
I am so embarrased to ask this question after writing thousands of shell scripts ... but I'll ask anyways :)
My input file looks like this:
server1:user1 user2 user3
server2:user1 user2
server3:user1 user3
.......
I am reading this way:
SAVE_IFS="${IFS}"
IFS=":"
while read HOST USERS; do
do something ....
done
IFS="${SAVE_IFS}"
It just exits after the first read and wouldn't move on, I tried cat'ng the file out and piping it to while read but still the same issue ...
I want the variable HOST to get the server name and the USERS to get the rest of the line ...
Any thoughts?
What is going on?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2007 12:38 PM
10-16-2007 12:38 PM
Re: while or for loop
#!/usr/bin/sh -x
<...rest of script...>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2007 12:46 PM
10-16-2007 12:46 PM
Re: while or for loop
Yes, I am ssh'ng in (actually ttlg'ng in, this is opsware's version of ssh works the same way as ssh) ...
I ran it in debug mode earlier, just exits after the first read, nothing useful ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2007 03:26 PM
10-16-2007 03:26 PM
Re: while or for loop
export IFS=":"
Otherwise, nothing later "knows" about the value, since it was never exported into the shell's environment. I *think*...
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2007 04:27 PM
10-16-2007 04:27 PM
Re: while or for loop
Nah, export is not going to help ... I tried to echo '${HOST} - ${USERS}' earlier and it worked fine - went through the list and echo'ed it just like I expected, its just when I put in the ssh/ttlg line that it exits after the first line ... may be with ssh there are options like BatchMode etc that might work, but I am really not sure if 'ttlg' has any ...
Anyways, any other thoughts/suggestions are welcome.
Thanks,
Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2007 07:11 PM
10-16-2007 07:11 PM
Re: while or for loop
No, exporting IFS is near useless. As soon as you create a new shell, it is reset.
>Mike: Otherwise, nothing later "knows" about the value,
Nothing later needs to know. Only the current shell and that doesn't need to have it exported.
>it's just when I put in the ssh/ttlg line that it exits after the first line
Can you show what you mean?
When I use: ssh machine script-with-read
this works fine.
Are you using "ssh $HOST $USER" within your while loop? If so, you need to use:
ssh -n $HOST $USER
Otherwise ssh eats your input from /tmp/myinput.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 03:11 AM
10-17-2007 03:11 AM
Re: while or for loop
while read LINE
do
HOST=${LINE%%:*}
USERS=${LINE#*:}
... do something
done
Yes, you can make IFS work. It is just that so often what you think you told the shell is not what you said.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 03:44 AM
10-17-2007 03:44 AM
Re: while or for loop
have you specified which shell interpreter to use in the first line?
If not, try
#!/usr/bin/sh
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 05:56 AM
10-17-2007 05:56 AM
Re: while or for loop
calling a ssh in your loop will eat al remaining input, because ssh read stdin as well.
A usage of 'ssh -n' will prevent that command doing so.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 08:44 PM
10-17-2007 08:44 PM
Re: while or for loop
my suggetion is to load the HOSt USER into an array and after perform another loop reading data for array. probbaly the sheel used withi the first loop read the rest of teh file as STDIN.
Just my .02$
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 09:23 PM
10-17-2007 09:23 PM
Re: while or for loop
if you change
do something
in your script to
echo "hello $HOST $USERS"
does the script then produce only a single line of output? If yes, it must be the "do something" that causes the exit.
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2007 07:57 AM
10-19-2007 07:57 AM