- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: for loop problem
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
05-16-2006 08:05 AM
05-16-2006 08:05 AM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:09 AM
05-16-2006 08:09 AM
Re: for loop problem
Posting the actual code you have would help us help you.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:13 AM
05-16-2006 08:13 AM
Re: for loop problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:18 AM
05-16-2006 08:18 AM
Re: for loop problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:20 AM
05-16-2006 08:20 AM
Re: for loop problem
Try this to get some ideas:
create a text file, x.txt that looks something like this:
aaa bbb
ccc
ddd eee
fff gg
Now execute the following script and see if the results are what you expect:
#!/usr/bin/ksh
FILES="aa bb cc dd ee ff ggg"
echo "Test 1"
for F in ${FILES}
do
echo "${F}"
done
echo "Test 2"
for F in "${FILES}"
do
echo "${F}"
done
echo "Test 3"
for F in $(cat x.txt)
do
echo "${F}"
done
echo "Test 4"
for F in "$(cat x.txt)"
do
echo "${F}"
done
I suspect that one of these outputs will ring a bell.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:30 AM
05-16-2006 08:30 AM
Re: for loop problem
# Loop through and set temp files for users
user1=$(cat userqty_temp )
for user in $user1
to this:
for user in $(cat userqty_temp)
I don't know if it will make a difference, but it is possible.
Second, I would change the use of the back-ticks ( `statement` ) for executing stuff and instead use the $(statement) syntax. It drastically improves readability and prevents confusion of ' for `.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:39 AM
05-16-2006 08:39 AM
Re: for loop problem
Each time through the 'for' loop you do something like:
# cat accounts_to_process_temp |grep $user |awk '{print $3}' > userc_$user
...By using ">" you clobber the output file with each pass leaving one (the last) entry.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:44 AM
05-16-2006 08:44 AM
Re: for loop problem
Each time through the 'for' loop you do something like:
# cat accounts_to_process_temp |grep $user |awk '{print $3}' > userc_$user
...By using ">" you clobber the output file with each pass leaving one (the last) entry.
I think you mean to use ">>" to collect multiple entries.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 08:55 AM
05-16-2006 08:55 AM
Re: for loop problem
As an aside:
Instead of spawning multiple processes in a pipeline to read a file (with 'cat'); discard lines (with 'grep') and then create a separate process ('awk') to print fields from the stream, you can do:
# awk -v user=Darrell '{if ($0 ~ user) {print $1}}' yourfile
...instead of:
# cat yourfile | grep Darrell | awk '{print $1}'
You will find this much more efficient and kinder on your system.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 09:03 AM
05-16-2006 09:03 AM
Re: for loop problem
head -1 userqty_tmp #Check the first line of the file.
for user in $(cat userqty_temp ) #This is the recommended way of doing the loop.
do
echo $user #This will tell you what value the variable has been assigned.
cat accounts_to_process_temp |grep $user |awk '{print $3}' > userc_$user
cat accounts_to_process_temp |grep $user |awk '{print $4}' > userpv_$user
cat accounts_to_process_temp |grep $user |awk '{print $5, $6}' > username_$user
cat accounts_to_process_temp |grep $user |awk '{print $7, $8}' > usersuper_$user
cat accounts_to_process_temp |grep $user |awk '{print $9}' > usersupermail_$user
# Find information from file
userCID=`cat /var/vmi_bin/security/account_security/userc_$user`
userPVID=`cat /var/vmi_bin/security/account_security/userpv_$user`
username=`cat /var/vmi_bin/security/account_security/username_$user`
usersuper=`cat /var/vmi_bin/security/account_security/usersuper_$user`
usersupermail=`cat /var/vmi_bin/security/account_security/usersupermail_$user`
print "user = $user"
print "userCID = $userCID"
print "userPVID = $userPVID"
print "username = $username"
print "usersuper = $usersuper"
print "usersupermail = $usersupermail"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 09:11 AM
05-16-2006 09:11 AM
Re: for loop problem
Using grep to match your users also has potential for failure (or perhaps I should say for working too well).
Consider the case where $user = "bob". That would match "bob","bobby","bubbabob","bob2" .... You can improve that using grep -E with anchored patterns but reallly awk is more than capable (and is a better choice) for doing the matching and the extraction.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 09:20 AM
05-16-2006 09:20 AM
Solutioncat userqty_temp | while read user
do
echo $USER
...
done
but reading a file produces all the lines
One advantage to using read is that you can extract multiple items from each line into separate variables. Suppose you are reading a file with the user, the date, a home directory and size:
cat userqty_temp | while read USER DATE HOME SIZE
do
echo $USER $DATE $HOME $SIZE
...
done
I like to use UPPERCASE for variable names for readability.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 09:24 AM
05-16-2006 09:24 AM
Re: for loop problem
user = 1
userCID = istest
dc
userPVID = istest
campbelldc
username = Calvin Hartless
Joe Blow
usersuper = Alan Campbell
Darrell Campbell
usersupermail = campbelldc@vmi.edu
campbelldc@vmi.edu
user = 2
userCID = dc
userPVID = campbelldc
username = Joe Blow
usersuper = Darrell Campbell
usersupermail = campbelldc@vmi.edu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 09:31 AM
05-16-2006 09:31 AM
Re: for loop problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 09:36 AM
05-16-2006 09:36 AM
Re: for loop problem
number|date|ColleagueuserID|PostViewuserID|username|supervisorname|superemail
1 051606 istest istest Calvin Hartless Alan Campbell campbelldc@vmi.edu
2 051606 dc campbelldc Joe Blow Darrell Campbell campbelldc@vmi.edu
3 051706 not_applicable webuser Big Boy Vern Beitzel campbelldc@vmi.edu
In an essense, I compare the date to tdays date and see how many people from this file should be processed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2006 11:05 AM
05-16-2006 11:05 AM
Re: for loop problem
for user in 1 051606 istest istest Calvin Hartless Alan Campbell campbelldc@vmi.edu
which means that the loop will start with 1, then 051606, then istest, etc in each loop iteration. That's where the cat/read form will work much better -- and the advantage that you have immediate access to each element in the line:
cat userqty_temp | while read LINE DATE USER GROUP RESTOFLINE
do
echo $DATE
...
done
The RESTOFLINE is ambiguous because the user and supervisor names might not always be two words.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2006 03:06 AM
05-17-2006 03:06 AM
Re: for loop problem
Bill is correct. The file cannot be parsed with the cat command. If each line is numbered, you can use grep to capture each line in a while loop using an array.
LINEN=$(wc -l file_name)
TMPVAR=1
while (TMPVAR < LINEN)
do
set -A FILELINE $(grep "^${TMPVAR} " file_name)
#run a command to extract ${FILELINE[0]}
#run a command to extract ${FILELINE[1]}
#run a command to extract ${FILELINE[2]}
#run a command to extract ${FILELINE[3]}
#run a command to extract ${FILELINE[4]}
#run a command to extract ${FILELINE[5]}
(( TMPVAR += 1 ))
done
This assumes that all names consist of the same number of words. ie. John Smith, Fred Jones. Note they all have two words.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2006 03:32 AM
05-17-2006 03:32 AM
Re: for loop problem
I think, you can preprocess your file before doing your real data processing.
Assuming a fixed datestring 051606 to compare with
awk -v dstr=051606 'NF>1 && ($2==dstr)' yourfile
will preselect all entries of the given datestring.
You can then do more processing in awk or use Bills 'while'-solution
awk .. | while read v1 v2 v3 restofline
do
...
done
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2006 04:33 AM
05-17-2006 04:33 AM
Re: for loop problem
From...
cat accounts_to_process_temp|grep $user |awk '{print $3}' > userc_$user
To...
cat accounts_to_process_temp |grep "^$user " |awk '{print $3}' > userc_$user
repeat for each grep pattern within your for loop, so it matches exactly one instance, thereby eliminating the discrepancy below...
> the first iteration puts thte entire list into the variables <
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2006 07:03 AM
05-24-2006 07:03 AM