Help Me With the Loop. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help Me With the Loop.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-14-2013
nirjhar17 nirjhar17 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 23 September 2013, 11:34 AM EDT
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Help Me With the Loop.

I am inputing three variables in the script where i am running loop for two times.so right now my output is coming like

Code:
A
B
C

A1
B1
C1

But i want output like

Code:
A
A1

B
B1

C
C1

Please suggest what i will do now ? Do i have to save the output in three different files or is there any way where we can accumulate same type of output together? Please suggest!!

Last edited by Scrutinizer; 09-14-2013 at 07:55 AM.. Reason: code tags
Sponsored Links
    #2  
Old 09-14-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Looks to me like you have something like this algorithm:
Code:
FOR loop in 1 2
   process A
   process B
   process C
END

What you need is something more like this one:
Code:
FOR X in A B C
   FOR loop in 1 2
      process X
   END
END

I hope this helps.

Andrew
Sponsored Links
    #3  
Old 09-14-2013
nirjhar17 nirjhar17 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 23 September 2013, 11:34 AM EDT
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Hello Andrew,

Thanks for the reply.. Please check the below code......

Code:
echo -n "enter the number of ports :"
read N-----------------------------"i/p value for the loop run"
for (( i=1;i<N+1;i++ ))
do
#############STEP 4:---Enter the director name##################
echo -n "enter the director name :"
read NAME;
############STEP 5:----Enter the port number####################
echo -n "enter the port num :"
read NUM;
echo -e "\n" >>/tmp/$hostname
echo "##Write Disable##">>/tmp/$hostname----taking o/p in external file.
echo -e "\n" >>/tmp/$hostname

cat /tmp/devices | awk '{print "/usr/symcli/bin/symdev -sid" "'" $sid "'" "write_disable " $1 " -SA" "'" $NAME "'" "-p "'" $NUM "'" " }' >> /tmp/$hostname

echo "##Device Unmap##">>/tmp/$hostname
echo -e "\n" >>/tmp/$hostname

cat  /tmp/devices | awk '{print "unmap dev " $1 " from dir " "'"$NAME"'"":""'"$NUM"'" ";"}'>> /tmp/$hostname

device=`cat /tmp/devices | sed ':a;N;$!ba;s/\n/,/g'`
#################enter the wwn associated with this ######################
echo -n "enter the wwn associated with the above director port :"
read wwn
echo "symmask -sid $sid -wwn  $wwn -dir "$NAME" -p "$NUM" remove devs $device" >>/tmp/$hostname

done


after running the above code i am getting below o/p like this ....




Code:
/usr/symcli/bin/symdev -sid 4002 write_disable 29ce -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ec8 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed1 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1eda -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed0 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 3aa1 -SA 4B -p 0
##Device Unmap##


unmap dev 29ce from dir 4B:0;
unmap dev 1ec8 from dir 4B:0;
unmap dev 1ed1 from dir 4B:0;
unmap dev 1eda from dir 4B:0;
unmap dev 1ed0 from dir 4B:0;
unmap dev 3aa1 from dir 4B:0;
symmask -sid 4002 -wwn  2100001b3283a261 -dir 4B -p 0 remove devs 29ce  0B7,1ec8        0B8,1ed1        0B9,1eda 0BA,1ed0 0BB,3aa1        0BC


##Write Disable##


/usr/symcli/bin/symdev -sid 4002 write_disable 29ce -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ec8 -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed1 -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1eda -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed0 -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 3aa1 -SA 13B -p 0
##Device Unmap##


unmap dev 29ce from dir 13B:0;
unmap dev 1ec8 from dir 13B:0;
unmap dev 1ed1 from dir 13B:0;
unmap dev 1eda from dir 13B:0;
unmap dev 1ed0 from dir 13B:0;
unmap dev 3aa1 from dir 13B:0;
symmask -sid 4002 -wwn  2100001b32832954 -dir 13B -p 0 remove devs 29ce 0B7,1ec8        0B8,1ed1        0B9,1eda 0BA,1ed0 0BB,3aa1        0BC


But i want my o/p look like this ,,,..i want all the commands o/p in one file and i m not sure how to apply your concept here.


Code:
/usr/symcli/bin/symdev -sid 4002 write_disable 29ce -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ec8 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed1 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1eda -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed0 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 3aa1 -SA 4B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 29ce -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ec8 -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed1 -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1eda -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 1ed0 -SA 13B -p 0
/usr/symcli/bin/symdev -sid 4002 write_disable 3aa1 -SA 13B -p 0
##Device Unmap##
unmap dev 29ce from dir 4B:0;
unmap dev 1ec8 from dir 4B:0;
unmap dev 1ed1 from dir 4B:0;
unmap dev 1eda from dir 4B:0;
unmap dev 1ed0 from dir 4B:0;
unmap dev 3aa1 from dir 4B:0;
unmap dev 29ce from dir 13B:0;
unmap dev 1ec8 from dir 13B:0;
unmap dev 1ed1 from dir 13B:0;
unmap dev 1eda from dir 13B:0;
unmap dev 1ed0 from dir 13B:0;
unmap dev 3aa1 from dir 13B:0;

symmask -sid 4002 -wwn  2100001b3283a261 -dir 4B -p 0 remove devs 29ce  0B7,1ec8        0B8,1ed1        0B9,1eda 0BA,1ed0 0BB,3aa1        0BC
symmask -sid 4002 -wwn  2100001b32832954 -dir 13B -p 0 remove devs 29ce 0B7,1ec8        0B8,1ed1        0B9,1eda 0BA,1ed0 0BB,3aa1        0BC


Last edited by Scrutinizer; 09-14-2013 at 07:56 AM.. Reason: code tags
    #4  
Old 09-14-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Please put your code into code blocks - it makes it easier to read.

If you read the directory and port numbers into arrays you could loop over each array:

Code:
declare -A dirs ports
for (( i=0;i<N;i++ ))
do
   printf "enter directory "
   read dirs[$i]
   printf "endter port number "
   read ports[$i]
done
for DIR in ${dirs[@]}
do
   ...
done
for PORT in ${ports[@]}
do
   ...
done

Andrew
Sponsored Links
    #5  
Old 09-14-2013
nirjhar17 nirjhar17 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 23 September 2013, 11:34 AM EDT
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Hello Andrew i have tried the below code

Code:
echo -n "enter the sid for DMX :"
read sid
declare -A dirs ports
echo -n "enter the number of ports :"
read N
for (( i=0;i<N;i++ ))
do
   echo "enter director "
   read dirs[$i]
   echo "enter port number "
   read ports[$i]
done
for DIR in ${dirs[@]}
do
cat /tmp/devices | awk '{print "/usr/symcli/bin/symdev -sid" "'" $sid "'" "write_disable " $1 " -SA" "'" $DIR "'" "-p "'" $NUM "'" "}'
cat  /tmp/devices | awk '{print "unmap dev " $1 " from dir " "'"$DIR"'"":""'"$NUM"'" ";"}'
done
for NUM in ${ports[@]}
do
cat /tmp/devices | awk '{print "/usr/symcli/bin/symdev -sid" "'" $sid "'" "write_disable " $1 " -SA" "'" $DIR "'" "-p "'" $NUM "'" "}'
cat  /tmp/devices | awk '{print "unmap dev " $1 " from dir " "'"$DIR"'"":""'"$NUM"'" ";"}'
done

Result


Code:
enter the sid for DMX :022
enter the number of ports :2
enter director
12a
enter port number
1
enter director
13a
enter port number
1
/usr/symcli/bin/symdev -sid 022 write_disable 29ce -SA 12a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1ec8 -SA 12a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1ed1 -SA 12a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1eda -SA 12a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1ed0 -SA 12a -p
/usr/symcli/bin/symdev -sid 022 write_disable 3aa1 -SA 12a -p
unmap dev 29ce from dir 12a:;
unmap dev 1ec8 from dir 12a:;
unmap dev 1ed1 from dir 12a:;
unmap dev 1eda from dir 12a:;
unmap dev 1ed0 from dir 12a:;
unmap dev 3aa1 from dir 12a:;
/usr/symcli/bin/symdev -sid 022 write_disable 29ce -SA 13a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1ec8 -SA 13a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1ed1 -SA 13a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1eda -SA 13a -p
/usr/symcli/bin/symdev -sid 022 write_disable 1ed0 -SA 13a -p
/usr/symcli/bin/symdev -sid 022 write_disable 3aa1 -SA 13a -p
unmap dev 29ce from dir 13a:;
unmap dev 1ec8 from dir 13a:;
unmap dev 1ed1 from dir 13a:;
unmap dev 1eda from dir 13a:;
unmap dev 1ed0 from dir 13a:;
unmap dev 3aa1 from dir 13a:;
/usr/symcli/bin/symdev -sid 022 write_disable 29ce -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1ec8 -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1ed1 -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1eda -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1ed0 -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 3aa1 -SA 13a -p 1
unmap dev 29ce from dir 13a:1;
unmap dev 1ec8 from dir 13a:1;
unmap dev 1ed1 from dir 13a:1;
unmap dev 1eda from dir 13a:1;
unmap dev 1ed0 from dir 13a:1;
unmap dev 3aa1 from dir 13a:1;
/usr/symcli/bin/symdev -sid 022 write_disable 29ce -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1ec8 -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1ed1 -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1eda -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 1ed0 -SA 13a -p 1
/usr/symcli/bin/symdev -sid 022 write_disable 3aa1 -SA 13a -p 1
unmap dev 29ce from dir 13a:1;
unmap dev 1ec8 from dir 13a:1;
unmap dev 1ed1 from dir 13a:1;
unmap dev 1eda from dir 13a:1;
unmap dev 1ed0 from dir 13a:1;
unmap dev 3aa1 from dir 13a:1;

My objective is to enter the inputs "dir" and "port" at the same time. Also, i want when i run loop number of times same set of commands will concat.

Please let me know if you did nt get me i will provide you examples...

Last edited by nirjhar17; 09-14-2013 at 10:27 AM..
Sponsored Links
    #6  
Old 09-14-2013
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
If you replace the
Code:
for var in ${array[@]}

form with the numeric for used earlier you can access both arrays in each loop:

Code:
var=${array[$index]}

(the names here are as an example) I hope this helps

Andrew
Sponsored Links
    #7  
Old 09-14-2013
nirjhar17 nirjhar17 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 23 September 2013, 11:34 AM EDT
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Thanks for your quick reply Andrew!! but, can you please elaborate it little more i have two inputs "dir" and "ports" and i am not sure how to use them with your example...

i googled little bit and got the below format ..but it is not working with the above code not sure what i am doing wrong?


Code:
for DIR,NUM in ${dirs[@]} ${ports[@]}

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array Variable being Assigned Values in Loop, But Gone when Loop Completes??? mrm5102 Shell Programming and Scripting 5 10-19-2012 10:00 AM
HELP PLS - calling function in a while loop ends the loop ?? newbie_01 Shell Programming and Scripting 3 05-29-2012 04:38 PM
BASH loop inside a loop question rethink Shell Programming and Scripting 4 09-15-2010 07:58 AM
Null Handling in Until loop. . .loop won't stop brandono66 Shell Programming and Scripting 4 11-24-2009 03:57 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 04:59 PM



All times are GMT -4. The time now is 08:20 AM.