- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: loop in script
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
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
тАО08-02-2002 01:07 AM - last edited on тАО10-22-2012 12:14 AM by Maiko-I
тАО08-02-2002 01:07 AM - last edited on тАО10-22-2012 12:14 AM by Maiko-I
Hi,
Appreciate help what's wrong w/ the set syntax:
set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
count=0
for i in $MONFILE
do
ERR=`tail -1 $i|grep ^ERR|wc -l`
count=`expr $ERR + $count`
echo $i
done
Rgds,
Shahril
P.S. This thread has been moved from HP-UX > General to HP-UX > languages - HP Forums Moderator
Solved! Go to Solution.
- Tags:
- ARRAY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 01:12 AM
тАО08-02-2002 01:12 AM
Re: loop in script
count=0
for i in "/tmp/RC1.log" "/tmp/RC2.log"
...
and expr doesnt work if not ^ERR found so change it to;
let count=$count+$ERR
and it works fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 01:47 AM
тАО08-02-2002 01:47 AM
Re: loop in script
Try with this:
set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
count=0
for i in $MONFILE
do
ERR=`tail -1 $i|grep "^ERR" |wc -l`
if [ $ERR -gt 0 ]
then
(( count = $ERR + $count ))
fi
echo $i
done
Hope this help you,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 01:57 AM
тАО08-02-2002 01:57 AM
Re: loop in script
you reference your array incorrectly,
it should be like this
for file in ${MONFILE[*]}; do
# do something with $file
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 02:01 AM
тАО08-02-2002 02:01 AM
Re: loop in script
if you really insist on indexing (which is kind of daft) you could do something like this (remember ${#MONFILE} gives the dimsion of your array)
typeset -i i=0
while [ i -lt ${#MONFILE[*] ]; do
file=${MONFILE[i]}
((i+=1))
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 02:04 AM
тАО08-02-2002 02:04 AM
Re: loop in script
the dimension of the array is of course
${#MONFILE[*]}
whereas
${#MONFILE}
only gives the length of the string of the 1st element
(but I guess you realized my typo ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 02:11 AM
тАО08-02-2002 02:11 AM
Re: loop in script
in the test condition of the while loop you have to prepend a dollar sign to the integer variable i,
(e.g. while [ $i -lt ...)
or use the [[ ]] test operator syntax
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 02:19 AM
тАО08-02-2002 02:19 AM
Re: loop in script
If you just want to count the number of error lines in 2 files
typeset -i count=$(grep ^ERR /tmp/RC1.log /tmp/RC2.log)
Steve Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2002 01:22 PM
тАО08-02-2002 01:22 PM
Re: loop in script
set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
typeset -i count=0 ERR=0
for i in ${MONFILE[@]}
do
ERR=$(tail -1 $i | grep ^ERR | wc -l)
let count+=$ERR
echo $i
done
echo $count
To use all the elements of the array, the array name must be suffixed with [@] or [*] and wrapped in {}.
You could even do it this way:
typeset -i count=0
set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
for i in ${MONFILE[@]}
do
tail -1 $i
done | grep ^ERR | wc -l | read count
echo $count
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2002 06:07 PM
тАО08-03-2002 06:07 PM
SolutionI have to agree that using an array is more trouble than it is worth in this case. I would use filename generation:
count=0
for I in /tmp/RC?.log
do
ERR=$(tail -1 $I | grep -c ^ERR)
((count += ERR))
print $I
done
The code following "in" should interpret to a list. /tmp/RC?.log returns all files that match (e.g. RC1.log, RCa.log, RCA.log). If you want to restrict to numbers, use /tmp/RC[0-9].log
-dlt-