- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Grep not functioning inside for/next or while 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
Discussions
Discussions
Discussions
Forums
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
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-31-2006 10:31 PM
тАО10-31-2006 10:31 PM
Outside a while or for/next loop, works fine, and inside the loop the shell knows of all the variables ( the echo statements )
But it simply will not generate output
If I change the $HOSTSFILE variable to an invalid directory, grep generates errors, so it is probally working, but I can't fathom where the output might be going. This is slowly driving me insane, while my project falls further behind. Any thoughts gratefully recieved
HP-UX B.11.00
#! /usr/bin/posix/sh
echo "$CONFIGCHG" | while read X ;do
echo "$X"; echo "$HOSTSFILE"
grep "$X" "$HOSTSFILE" | awk '{print $2}'
done
gives me this result:
$ storeconfig
138.27.120.94
/home/hynesm/bin/hosts
138.7.100.94
/home/hynesm/bin/hosts
echo statements work fine, but no result from the grep statement.
While directly on the command line, works fine.
$ HOSTSFILE="/home/hynesm/bin/hosts"
$ X=138.7.100.94
$
$ grep "$X" "$HOSTSFILE" | awk '{print $2}'
TST-RMC-2R1
$
Exactly what I would expect. Is grep working, and if so where is it sending it's output.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-31-2006 10:45 PM
тАО10-31-2006 10:45 PM
Re: Grep not functioning inside for/next or while loops.
#! /usr/bin/posix/sh
while read X
do
echo "$X"
echo "$HOSTSFILE"
grep "$X" "$HOSTSFILE" | awk '{print $2}'
done < $CONFIGCHG
exit
Yang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-31-2006 10:45 PM
тАО10-31-2006 10:45 PM
Re: Grep not functioning inside for/next or while loops.
what data is the while loop reading ?
Should the echo "$CONFIGCHG" be a cat "$CONFIGCHG"? Are there some semi-colons missing after echo "$HOSTSFILE".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-31-2006 11:33 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-31-2006 11:45 PM
тАО10-31-2006 11:45 PM
Re: Grep not functioning inside for/next or while loops.
- Do you need all those double quotes?
- Why not combine stuff?
I think the whoe script can be re-written as:
grep -f $CONFIGCHG $HOSTSFILE | awk '{print $2}'
And if you do need the loop for other reasons, then you can have AWK alone do the job. For example
awk -v x=$X '($1 ~ x){print $2}'
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-31-2006 11:56 PM
тАО10-31-2006 11:56 PM
Re: Grep not functioning inside for/next or while loops.
Yang..I still get the grep mysterously not working.
Peter... the data is a list of IP address, as they are kept in a variable rather than a file, I beleive echo is the correct command
The semi colon takes place of a line break, as such I don't think it is required.
spex, I tried your awk command, and got exactly the same result..which is good as it means that the problem is not with grep or awk, but bad because I still don't have a clue while simple commands are not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 12:28 AM
тАО11-01-2006 12:28 AM
Re: Grep not functioning inside for/next or while loops.
#! /usr/bin/posix/sh
for $X in $CONFIGCHG
do
echo "$X"
echo "$HOSTSFILE"
grep "$X" "$HOSTSFILE" | awk '{print $2}'
done
Yang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 12:36 AM
тАО11-01-2006 12:36 AM
Re: Grep not functioning inside for/next or while loops.
change the $X to X in Yangs code and it works:
for $X in $CONFIGCHG
to
for X in $CONFIGCHG
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 01:23 AM
тАО11-01-2006 01:23 AM
Re: Grep not functioning inside for/next or while loops.
I appreciate the code is correct and it works on your machines, but there is obviously something mysterous going on with the box I'm working on.
As neither grep nor awk return results, the question is now:
"Why dosn't my shell show std output from inside loops ?"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 01:45 AM
тАО11-01-2006 01:45 AM
Re: Grep not functioning inside for/next or while loops.
Yang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 02:01 AM
тАО11-01-2006 02:01 AM
Re: Grep not functioning inside for/next or while loops.
/usr/bin/grep or /usr/bin/awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 02:44 AM
тАО11-01-2006 02:44 AM
Re: Grep not functioning inside for/next or while loops.
the following appears to work as desired:
for X in `echo $CONFIGCHG`; do
echo ${X}
grep ${X} ${HOSTSFILE} | awk '{print $2}'
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 03:14 AM
тАО11-01-2006 03:14 AM
Re: Grep not functioning inside for/next or while loops.
I tried the full paths, but with the same result.
I'm convinced this is something weird with the setup on the HP Box, and not with the code. All the examples provided should work.
I can't get anybody who looks after that box, to give me any time at all, so I'm thinking of going back to my orginal idea of using a LINUX/Bash enviroment.
Just have to convince my boss it's not "shareware" ( Corprates are weird, unless they spent thousands of quid on something, it's "worthless" )
Thanks for all your help, but this one is going into the "too hard" basket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 04:09 AM
тАО11-01-2006 04:09 AM
Re: Grep not functioning inside for/next or while loops.
If CONFIGCHG contains more than one ip, for example:
123.234.123.1 123.234.222.111
it won't work with the following
echo $CONFIGCHK | while read X; do
the X gets the entire list, not the first IP. Changing it to
for X in $CONFIGCHK; do
appears to work as desired.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 04:11 AM
тАО11-01-2006 04:11 AM
Re: Grep not functioning inside for/next or while loops.
set -x at the beginning of your script to see how things are actually expanding.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 04:13 AM
тАО11-01-2006 04:13 AM
Re: Grep not functioning inside for/next or while loops.
Change your script to pipe the output of "echo $X" into "od" command with suitable options. Inspect for invisible characters, which might make it difficult for grep to find a match.
If the problem is there, re-think the way you're using to create the value of $CONFIGCHG.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-01-2006 04:23 AM
тАО11-01-2006 04:23 AM
Re: Grep not functioning inside for/next or while loops.
echo $CONFIGCHG
#! /usr/bin/posix/sh -x
.
.
.
rest of script
.
.
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-02-2006 12:33 AM
тАО11-02-2006 12:33 AM
Re: Grep not functioning inside for/next or while loops.
try changeing your read statement to
"read X junk;".
I beleive the read command will load variables in sequence as they are contained in the line so that
read X makes X = the entire line
read X Y makes X = 1st word and Y = the rest of line
read X Y Z makes X = 1st, Y = 2nd and Z = rest of line.
RayB