- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to group the output of a 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
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
07-14-2009 11:35 PM
07-14-2009 11:35 PM
First time user & new to scripting. :)
I have my shell script like this:
[CODE]
#!/usr/bin/sh
e_id=`sqlplus -s scott/tiger@DB<
select emp_id from employee;
quit
`
echo "Employee ID's $e_id"
group=GROUP1
# Getting the sss_no for each emp_id
for i in $e_id
do
sss_nos=`sqlplus -s scott/tiger@DB <
select sss_no from employee_bank where emp_id = $i;
quit
`
echo "List of SSS no's $sss_nos"
# Run a customize program that calls the sss_nos
# SSS_move
SSS_move $sss_nos $group
sleep 2
done
[/CODE]
The Output:
Employee ID's
4567
2231
1121
2233
4554
3243
1231
3311
List of SSS no's
45566
59589
55170
51530
33099
20234
87231
54192
SSS_move:
SSS_move 45566 GROUP1
SSS_move 59589 GROUP1
SSS_move 55170 GROUP1
SSS_move 51530 GROUP1
SSS_move 33099 GROUP1
SSS_move 20234 GROUP1
SSS_move 87231 GROUP1
SSS_move 54192 GROUP1
Now, how will I able to group the output of the loop so that I can assign GROUP1 only to the 1st two output of the loop then assign GROUP2 to the 2nd two output of the loop & so on. :confused:
Desired Output:
SSS_move 45566 GROUP1
SSS_move 59589 GROUP1
SSS_move 55170 GROUP2
SSS_move 51530 GROUP2
SSS_move 33099 GROUP3
SSS_move 20234 GROUP3
SSS_move 87231 GROUP4
SSS_move 54192 GROUP4
Also, how do I count the $sss_nos output so that I have this condition:
If my $sss_nos -gt 7 (w/c in my example above it is true) then assign to multiple GROUP#. If it is -lt 7 then group it only to GROUP1
Pls. help :D
Thanx in advance :)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2009 12:15 AM
07-15-2009 12:15 AM
Solutionemp_bk=`sqlplus -s scott/tiger....<
select e.emp_id||':'||sss_no from employee e, employee_bank eb
where e.emp_id=eb.emp_id;
quit;
output :
4567:45566
2231:59589
1121:55170
2233:51530
....
#
# count lines
if test `echo $emp_bk|wc -l` -lt 7
then echo "one group only"
multigroup=no
else echo "multiple groups"
multigroup=yes
fi
#
# set group number to 1
group_no=1
cnt=1
for x in $emp_bk
do
#
# get employee number, bank number
emp_no=`echo $x|cut -d':' -f1`
bank_no=`echo $x|cut -d':' -f2`
#assemble group name 'GROUP' and actual group number
w_grp="GROUP"$group
#
# execute ss_move
sss_move $bank_no $w_grp
#
# add 1 to counter. if > 2 then add 1 to group counter
# set counter to 1
cnt=$cnt+1
if test $cnt -eq 3
then cnt=1
group_no=$group_no+1
fi
#
# reset group counter to 1 in case we just have one group
if test "$multigroup" = "no"
then group_no=1
fi
done
--------------------------------------------
comment
cnt=$cnt+1 : you need to use expr to increase the number by 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2009 03:48 AM
07-15-2009 03:48 AM
Re: How to group the output of a loop
> cnt=$cnt+1 : you need to use expr to increase the number by 1.
Using 'expr' is slow. Let the shell do the arithmetic:
# cnt=$(($cnt+1))
For example:
# cnt=1;cnt=$(($cnt+1));echo ${cnt}
2
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2009 04:59 AM
07-15-2009 04:59 AM
Re: How to group the output of a loop
Or even better: (( cnt += 1 ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2009 04:52 PM
07-15-2009 04:52 PM
Re: How to group the output of a loop
@JRF, thanx
@Dennis Handly,
what does ((cnt += 1)) means (increment then equals to)????
Thanx Guys
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2009 05:02 PM
07-15-2009 05:02 PM
Re: How to group the output of a loop
> cnt=$(($cnt+1))
> (( cnt += 1 ))
These are equivalent operations. The second simply means increment the variable by one and destructively store the new value into the variable. The second form is shorthand for the first form. The first form would be used if you wanted to increment by a value other than one, as for example:
cnt=$(($cnt+2))
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2009 03:51 AM
07-16-2009 03:51 AM
Re: How to group the output of a loop
Perhaps you are confusing the operator ++ with +=. Note the scummy real shell doesn't support ++/--.
To increment by 2: (( cnt += 2 ))
Basically there isn't a need to use the first form if you are doing integer arithmetic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2009 03:54 AM
07-16-2009 03:54 AM
Re: How to group the output of a loop
More specifically, in C/C++ a compound assignment is the same as:
L O= R ==> L = L O R
Where O is any of the binary operators.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2009 04:02 AM
07-16-2009 04:02 AM
Re: How to group the output of a loop
> Dennis: Perhaps you are confusing the operator ++ with +=. Note the scummy real shell doesn't support ++/--.
Yes, indeed I was! I know that ++/-- aren't supported by the shell (a damn shame!) but I confess I wasn't aware that one could do '(( cnt += 2 ))'.
Dennis, thanks for the clarification!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2009 04:16 AM
07-16-2009 04:16 AM
Re: How to group the output of a loop
You can also do: (( cnt = aaa + bbb ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2009 06:06 AM
07-16-2009 06:06 AM
Re: How to group the output of a loop
This way you can execute the function within a variable and then you have a grouped output.
Something like this might be the key.
#!/usr/bin/sh
function do_dragon {
e_id=$(sqlplus -s scott/tiger@DB<
select emp_id from employee;
quit
)
group=GROUP1
# Getting the sss_no for each emp_id
for i in $e_id; do
sss_nos=$(sqlplus -s scott/tiger@DB <
select sss_no from employee_bank where emp_id = $i;
quit
)
echo "List of SSS no's $sss_nos"
# Run a customize program that calls the sss_nos
# SSS_move
SSS_move $sss_nos $group
sleep 2
done
}
echo "Employee ID's $e_id"
output=$(do_dragon)
echo $output
ps. haven't tested it :P so might need some tweaking ds.
Best regards
Fredrik Eriksson