Complex variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex variable
# 1  
Old 07-05-2013
Complex variable

If I need the output as below, what syntax I should use. I use only korn shell.
=========In file ===========================
Code:
#!/usr/bin/ksn
P1=cat
P2=dog
P3=bat

for i 1 2 3
do
echo $P$i      # <---------- This line is the problem I face. What I should use.
done

========= End of file =======================
#Expected Result
Code:
cat
dog
bat

#Result
Code:
P1
P2
P3


Last edited by Scrutinizer; 07-05-2013 at 03:41 AM.. Reason: code tags
# 2  
Old 07-05-2013
Code:
eval echo '$'P$i


Last edited by anbu23; 07-05-2013 at 03:46 AM.. Reason: Added quotes around $
This User Gave Thanks to anbu23 For This Post:
# 3  
Old 07-05-2013
Try:
Code:
#!/usr/bin/ksh
P[1]=cat
P[2]=dog
P[3]=bat

for i in 1 2 3
do
  echo "${P[i]}"      # <---------- This line is the problem I face. What I should use.
done

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 07-05-2013
Hello,

you can use array concept also in spite of using many variables as follows an example.


Code:
 
$ cat array.ksh

values="dog, cat, bat"
values_actual=`echo $values | sed 's/,//g'`
set -A array_values ${values_actual}
for i in ${array_values[@]}
do
echo $i
done
 
 
$ ksh array.ksh
dog
cat
bat



Hope this helps.


Thanks,
R. Singh
# 5  
Old 07-05-2013
From the command line:-
Code:
Last login: Fri Jul  5 07:32:28 on ttys000
AMIGA:barrywalker~> ksh
AMIGA:uw> P[1]=Barry
AMIGA:uw> P[2]=Walker
AMIGA:uw> P[3]=G0LCU
AMIGA:uw> for i in 1 2 3; do; printf "${P[i]}\n"; done   
Barry
Walker
G0LCU
AMIGA:uw> exit
AMIGA:barrywalker~> _

This User Gave Thanks to wisecracker For This Post:
# 6  
Old 07-05-2013
Quote:
Originally Posted by anbu23
Code:
eval echo '$'P$i

Thank you very much. This is exactly I looking for.

Quote:
Originally Posted by Scrutinizer
Try:
Code:
#!/usr/bin/ksh
P[1]=cat
P[2]=dog
P[3]=bat

for i in 1 2 3
do
  echo "${P[i]}"      # <---------- This line is the problem I face. What I should use.
done

Thank you very much. I jush know the shell script has arrry.

Quote:
Originally Posted by RavinderSingh13
Hello,

you can use array concept also in spite of using many variables as follows an example.


Code:
 
$ cat array.ksh

values="dog, cat, bat"
values_actual=`echo $values | sed 's/,//g'`
set -A array_values ${values_actual}
for i in ${array_values[@]}
do
echo $i
done
 
 
$ ksh array.ksh
dog
cat
bat

Thank you very much. This technique teachs me how to combine stream editor with array.


Hope this helps.


Thanks,
R. Singh
Thank you very much. The other technique to use array with stream editor.

Quote:
Originally Posted by wisecracker
From the command line:-
Code:
Last login: Fri Jul  5 07:32:28 on ttys000
AMIGA:barrywalker~> ksh
AMIGA:uw> P[1]=Barry
AMIGA:uw> P[2]=Walker
AMIGA:uw> P[3]=G0LCU
AMIGA:uw> for i in 1 2 3; do; printf "${P[i]}\n"; done   
Barry
Walker
G0LCU
AMIGA:uw> exit
AMIGA:barrywalker~> _

Thank you very much. printf, Another new command I didn't know before combine with array.

It very kind of you all to suggest solutions to me. Thank you.

Last edited by kkakarot; 07-05-2013 at 04:39 AM.. Reason: fill in some missing word
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Complex positioning

I got these entries in a file alias server.domain.com='ssh 192.168.1.1@user1 ' alias server1.domain.com='ssh user2 @192.168.1.1' alias server1.domain.com='ssh user3@192.168.1.1' In the above lines, last line is the correct format. The first is IP@username, has got a space after the... (9 Replies)
Discussion started by: anil510
9 Replies

2. Shell Programming and Scripting

Complex if then else

for i in $(condition ONE);do for e in $(CONDITION TWO);do if ; then command 1 command 2 command 3 else if ; then command 1 command 2 command 3 else if ; then ... (6 Replies)
Discussion started by: elilmal
6 Replies

3. Shell Programming and Scripting

complex requirement

i have a requirement to search a pattern1 and once the pattern1 is found i have to go up and search pattern2 and if pattern2 is found i have to search down for pattern3 and pattern4. once this cycle is compelted, again i have to search pattern1 and pattern2,3,4 i am able to do this by opening... (9 Replies)
Discussion started by: tnvanathy23
9 Replies

4. Shell Programming and Scripting

Complex Compare

Hi All, File1.txt File2.txt Above are the two input file's: Always I use to get three rows for File1.txt File1.txt (fixed width) 2nd line i will get date and 3rd record i will get a number. File2.txt (Pile delimeter) will contains 'n' number of records were header & footer is... (4 Replies)
Discussion started by: kmsekhar
4 Replies

5. Shell Programming and Scripting

Complex variable assignment

Hi, I have a requirement as follows, need to call the format of ${$var} form. For example, i am taking a variable. count=1, ((LIMIT_$count=$count + 1)) Now i have to echo this variable LIMIT_$count. (This is in a loop..) echo ${LIMIT_$count} - displays as a syntax... (3 Replies)
Discussion started by: abhisheksunkari
3 Replies

6. Shell Programming and Scripting

Complex Script

hey... i had a big problem with my professor i have 3 simple archives in.txt -> had all timestamps of users logon (100lines) ex. 111111 222222 333333 out.txt -> had all timestamps of users logof (100lines) ex. 111113 222225 333332 commands.txt... (9 Replies)
Discussion started by: beandj
9 Replies

7. UNIX for Dummies Questions & Answers

Complex Commands

I need to ideas for some complex commands for a script file. Any ideas would be greatly appricated (2 Replies)
Discussion started by: trob
2 Replies

8. Shell Programming and Scripting

Complex use with awk

Hi , I have file named docs.txt The content of the file look like this: DOC disk location Size ======= ===== ============= ========= TXT A /dev/dm-1 10 TXT B /dev/dm-2 10 BIN C ... (3 Replies)
Discussion started by: yoavbe
3 Replies
Login or Register to Ask a Question