for loop - reverse reading


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop - reverse reading
# 8  
Old 02-09-2012
A more complicated Shell method for any number of items:

Code:
export CFGLIST="LIST1 LIST2 LIST3"
set ${CFGLIST}
items=$#
counter=$items
while [ $counter -ne 0 ]
do
        eval output=\$${counter}
        echo "${output}"
        counter=$((${counter} - 1))
done

./scriptname
LIST3
LIST2
LIST1

# 9  
Old 02-09-2012
Just for the fun of it since there are 18 ways to do most tasks (some better than others), how about using an array (ksh93 on solaris)?
Code:
#!/usr/dt/bin/dtksh

export CFGLIST="LIST1 LIST2 LIST3"

# Turn it into an array
set -A CFGLIST $CFGLIST

# Array is zero-based so start one less than the number of elements.
for (( i=$((${#CFGLIST[*]}-1));i>=0;i-- ))
do
  print "Array element ${i}: ${CFGLIST[${i}]}"
done

exit 0

Output:
Code:
$ efs
Array element 2: LIST3
Array element 1: LIST2
Array element 0: LIST1
$


Last edited by gary_w; 02-09-2012 at 12:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading line in while loop

Hello Team, i have to read line by line in a while loop, and the input file has:. # The script will start cppunit test application to run unit tests. -LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\ +LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\ $VOBTAG/SS_BFD/BFDSCLI/build:\ ... (7 Replies)
Discussion started by: chandana hs
7 Replies

2. Shell Programming and Scripting

Reverse the output using for loop

Good morning!! Im trying to ge tthe output in this for loop to be reversed. #!/usr/bin/perl $i = 1; for($i != 0 ; $i < 11 ; $i++){ print "$i\n"; } Ive tried changing the i++ to i--, but it makes the outputted numbers different also. Thanks bigben (4 Replies)
Discussion started by: bigben1220
4 Replies

3. Shell Programming and Scripting

Perl: Reading in reverse.

Is there a means of reading files in reverse? I just want to be able to read a file from the beginning and once I read a particular line, I start reading lines backward from there. Now, I could toss everything into a string array, but the file I'll be reading is roughly 100MB. I don't want... (3 Replies)
Discussion started by: mrwatkin
3 Replies

4. UNIX for Dummies Questions & Answers

Reading inside a loop

Hi I am trying read in side a do statement but it is not working it is just printing abc before read but not stopping in abc for user input Can anybody please help #!/usr/bin/ksh cat sample_file | while read ln_source3 do param=`echo $ln_source3 | nawk... (1 Reply)
Discussion started by: ssuresh1999
1 Replies

5. Shell Programming and Scripting

While loop reading a record

Hi, I am trying to create a while loop that will do the following: INFILE= list of new records that need to be added after last previous record while read record do find the last record processed create list of new records output to a file echo "$record">> $NEWFILE done ... (9 Replies)
Discussion started by: shortyball24
9 Replies

6. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

7. UNIX for Dummies Questions & Answers

reading more than one variable into a for loop

Hi, I have a file (details.txt) with 3 rows of variables ie... name postcode age john D fr25dd 25 mark W ab122aa 22 phil C cd343bb 33 What I want to do is read down the list with a loop and add each field into a one line piece of text... So I have a file (test1) which reads;... (3 Replies)
Discussion started by: starsky
3 Replies

8. Shell Programming and Scripting

reading from 2 files through while loop

hi i have two files cat input.txt 123456| 43256 456482|5893242 cat data.txt xv 123456 abcd dsk sd 123456 afsfn dd df 43256 asdf ff ss 456482 aa sf 5893242 ff ff aa 5893242 aa aa i need to read inputs from input.txt and find data for data.txt. then i need to print them as a... (2 Replies)
Discussion started by: windows
2 Replies

9. UNIX for Dummies Questions & Answers

Reading from while loop into an array

Hi I have something like cat $HOME/all_dirs | while read ln_old_dirs do if then echo "$ln_all_old_dirs" fi done As you know that the variable ln_all_old_dirs is not accessable from outside the... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

10. Shell Programming and Scripting

reading from within the loop --std i/p problem

i have a teat file having data like one 12/3 two 23/09 three 12/12 now from another script i want to read one line at a time ,cut field one and two in two separate variable ,compare field1 with another variable but outside the loop .If i found a match i want to take from user the value... (2 Replies)
Discussion started by: mobydick
2 Replies
Login or Register to Ask a Question