Sponsored Content
Top Forums Shell Programming and Scripting Using variables created sequentially in a loop while still inside of the loop [bash] Post 302328195 by DeCoTwc on Tuesday 23rd of June 2009 04:59:10 PM
Old 06-23-2009
Quote:
Originally Posted by rakeshawasthi
can you put set -x in the second line of your file "question" and paste the output here...
This is the output with set -x command added:

Code:
(16:48:46\[deco@S.Man)
[~/bin]$ ./question 
++ listQpsk 40
++ grep -w '[1-4]'
+ tab=' 40 SMANHUBAQPSK1          1    1342      1194        88
 40 SMANHUBAQPSK1          2    1668      1464        87
 40 SMANHUBAQPSK1          3    1883      1535        81
 40 SMANHUBAQPSK1          4    1685      1399        83'
+ seq=1
+ num=4
+ [[ 1 -gt 4 ]]
++ echo ' 40 SMANHUBAQPSK1          1    1342      1194        88
 40 SMANHUBAQPSK1          2    1668      1464        87
 40 SMANHUBAQPSK1          3    1883      1535        81
 40 SMANHUBAQPSK1          4    1685      1399        83'
++ grep -w 1
++ awk '{print $5}'
+ eval count1=1194
++ count1=1194
+ seq=2
+ [[ 2 -gt 4 ]]
++ echo ' 40 SMANHUBAQPSK1          1    1342      1194        88
 40 SMANHUBAQPSK1          2    1668      1464        87
 40 SMANHUBAQPSK1          3    1883      1535        81
 40 SMANHUBAQPSK1          4    1685      1399        83'
++ grep -w 2
++ awk '{print $5}'
+ eval count2=1464
++ count2=1464
+ seq=3
+ [[ 3 -gt 4 ]]
++ echo ' 40 SMANHUBAQPSK1          1    1342      1194        88
 40 SMANHUBAQPSK1          2    1668      1464        87
 40 SMANHUBAQPSK1          3    1883      1535        81
 40 SMANHUBAQPSK1          4    1685      1399        83'
++ grep -w 3
++ awk '{print $5}'
+ eval count3=1535
++ count3=1535
+ seq=4
+ [[ 4 -gt 4 ]]
++ echo ' 40 SMANHUBAQPSK1          1    1342      1194        88
 40 SMANHUBAQPSK1          2    1668      1464        87
 40 SMANHUBAQPSK1          3    1883      1535        81
 40 SMANHUBAQPSK1          4    1685      1399        83'
++ grep -w 4
++ awk '{print $5}'
+ eval count4=1399
++ count4=1399
+ seq=5
+ [[ 5 -gt 4 ]]
+ echo 1194
1194
+ echo 1464
1464
+ echo 1535
1535
+ echo 1399
1399

However I was able to find a working solution to my problem:

Code:
(16:48:50\[deco@S.Man)
[~/bin]$ cat orig.question 
#!/usr/bin/bash
tab=`listQpsk 40|grep -w [1-4]`
seq=1
num=4
until [[ $seq -gt $num ]];do
var="count$seq"
eval var=`echo "$tab"|grep -w $seq|awk '{print $5}'`
echo $var
seq=$(($seq+1))
done

(16:50:12\[deco@S.Man)
[~/bin]$ ./orig.question 
1194
1464
1534
1399

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

2. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

3. Shell Programming and Scripting

Need help in assigning output of n commands to n variables automatically inside a for loop

Please help me to automatically assign the output of awk command to the variables cs3, cs4, cs5 and cs6 using a for loop. The below code is not working. for i in 3 4 5 6 do cs$i=`awk -F"|" 'BEGIN{sum=0}{sum=sum+$'$i'}END{printf("%d\n", sum)}' css` done echo $cs3 $cs4 $cs5 $cs6 (9 Replies)
Discussion started by: thulasidharan2k
9 Replies

4. Shell Programming and Scripting

For Loop inside For loop

I am new to unix and trying to make a script for writing all my command into another file and use that file to run all commands I am trying to use for loop with echo command to generate a command based script for writing the file with all the command sequentially w.r.t for loop. I want... (6 Replies)
Discussion started by: nnani
6 Replies

5. Shell Programming and Scripting

Bash for loop with two variables

Hi, I have the following folder structure: TST500-1000 TST500-2000 TST500-3000 TST700-1000 TST700-2000 TST700-3000 TST900-1000 TST900-2000 TST900-3000 I would like to store the numbers in the first column (considering "-" as column separator) into a variable then the numbers in... (3 Replies)
Discussion started by: alex2005
3 Replies

6. Shell Programming and Scripting

Bash for loop with awk and variables

I'm relatively new to bash scripting and am trying to use awk inside a bash for loop but am having a problem with the variables. for i in $(seq 30 5 60) do # Define variables up and down in AWK eval $(awk 'BEGIN{ print "up=$((i+1))"}' < /dev/null) eval $(awk 'BEGIN{ print... (2 Replies)
Discussion started by: lily-anne
2 Replies

7. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

8. Shell Programming and Scripting

Bash - How to do a "read -p" inside a while loop?

Hi there guys! I was trying to do: while read line; do if ; then read -p "Press Enter to continue..." cont=0 fi echo $line let cont++ done < file.txt However, I have read that the read -p would not work in a while loop... I was wondering if there is any other way to... (2 Replies)
Discussion started by: rplae
2 Replies

9. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

10. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies
0intro 0intro intro 0intro 2a 2a 6a 2a 8a 2a ka 2a va 2a xa 2a 2c
2c 6c 2c 8c 2c kc 2c vc 2c xc 2c 2l 2l 6l 2l 8l 2l kl 2l vl 2l xl
2l 81/2 81/2 label 81/2 window 81/2 wloc 81/2 acid acid acme acme
awd acme win acme 8al alef alef alef kal alef val alef ar ar  art
art  art2pic art ascii ascii unicode ascii awk awk basename base-
name bc bc bind bind mount bind unmount bind  bundle  bundle  c++
c++ c++/2c c++ c++/2l c++ c++/8c c++ c++/8l c++ c++/kc c++ c++/kl
c++ c++/vc c++ c++/vl c++ cal cal calendar calendar cat cat  read
cat  char  char  rschar char 1/2char char chgrp chgrp chmod chmod
cmp cmp comm comm con con cu con rx con telnet con  xmr  con  xms
con cp cp mv cp cpp cpp cpu cpu date date db db dc dc dd dd dela-
tex deroff deroff deroff diff diff doctype  doctype  du  du  echo
echo  ed  ed emacs emacs eqn eqn factor factor primes factor file
file fmt fmt fone fone fortune fortune	freq  freq  4s	games  5s
games  ana  games  catclock games clock games festoon games fire-
works games fsim games games games life games mandel games  plumb
games  smiley games swar games grap grap graph graph grep grep gs
gs hoc hoc hp hp join join broke kill kill kill ktrans ktrans lex
lex  look  look lp lp lc ls ls ls aliasmail mail edmail mail mail
mail seemail mail sendmail mail smtp mail smtpd mail to mail vis-
mon  mail  vwhois mail lookman man man man mc mc membername mk mk
mk mkdir mkdir mothra mothra netstat netstat news news nm nm p	p
page  page  netkey  passwd passwd passwd pcc pcc pic pic tpic pic
plot plot ppp ppp pppclient ppp pppserver ppp pr  pr  kprof  prof
prof  prof proof proof ps ps psu ps pbd pwd pwd pwd cd rc eval rc
exec rc exit rc flag rc rc rc rfork rc shift rc wait rc whatis rc
~  rc  rm rm B sam sam sam sam.save sam sed sed seq seq size size
sleep sleep sort sort spell spell sprog  spell	spin  spin  split
split start stop stop stop strings strings strip strip md5sum sum
sum sum syscall syscall tail  tail  32vfs  tapefs  cpiofs  tapefs
tapefs	tapefs tapfs tapefs tarfs tapefs tpfs tapefs v10fs tapefs
v6fs tapefs tar tar tbl tbl tcs tcs tee tee iwhois  tel  tel  tel
test test bibtex tex dvips tex dviselect tex latex tex mf tex tex
tex time time touch touch tr tr nroff  troff  troff  troff  tweak
tweak twig twig uniq uniq units units ki vi vi vi xi vi wc wc who
who whois who xd xd yacc yacc yesterday yesterday
All times are GMT -4. The time now is 12:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy