Visit Our UNIX and Linux User Community


Using a variable as a for loop expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using a variable as a for loop expression
# 1  
Old 09-21-2012
Using a variable as a for loop expression

I'm writing a script to merge the xkcd webcomic tiles for comic 1110. So far, I have written about 100 lines, and instead of doing each quadrant of the image separately, I've decided to use functions to do this, repeating for every quadrant and using variables for each quadrant to make the function act differently each time. My problem is, I'm having trouble using variables for the for loop expressions, it goes "line 8: ``eval "$a"`': not a valid identifier". This is what I have so far.

PS: I know I'm designated this as PHP, but that's only to get syntax highlighting, since php appears to highlight it the same way.

PHP Code:
#!/bin/bash
# Date Created: September 20th, 2012
# Created by Jonathan Bondhus - https://github.com/jbondhus
# Credit to Andreas Reichinger for analysis of image tile placement, xkcd.com for comic. Used ImageMagick code from Antonio Frascarelli.
#
#  This script will merge the files from xkcd comic number 1110, "Click and Drag".

function function_join() {
    echo 
"#";
    if [[ 
quadrant -eq 1 ]]; then
    
echo "# First Quadrant:"# Join together the first quadrant
    
quadrantName="first"
    
function_quadrant
    
else
        if [[ 
quadrant -eq 2 ]]; then
        
echo "# Second Quadrant:"# Join together the second quadrant
        
quadrantName="second"
        
function_quadrant
        
else
            if [[ 
quadrant -eq 3 ]]; then
            
echo "# Third Quadrant:"# Join together the third quadrant
            
quadrantName="third"
            
function_quadrant
            
else
                if [[ 
quadrant -eq 4 ]]; then
                
echo "# Fourth Quadrant:"# Join together the fourth quadrant
                
quadrantName="fourth"
                
function_quadrant
                fi
            fi
        fi
    fi
    
echo "#";
    for `
eval "$a"`;
    do
        echo 
"# row number "$n" ...";
        
convert $b;
        for `
eval "$c"`;
        do
            if [ 
$d ];      # If the tile file doesn't exist, an empty square will be used.
                
then convert $e;
                else 
convert $f;
            
fi;
        
done;
        
convert $g;    # Append the image onto the current quadrant
        
rm -f $h;
        echo 
"# ... OK";
        echo 
"#";
    
done;
}

function 
function_quadrant() {
    if [[ 
quadrant -eq 1 ]]; then # Set the variables for the first quadrant
    
a='((n=$north; n>=1)); n--'
    
b='-size 0x1 xc:white resultn$n''w.png'
    
c='((w=$west; w>=1; w--))'
    
d='-e $n''n$w''w.png'
    
e='+append resultn$n''w.png $n''n$w''w.png resultn$n''w.png'
    
f='+append resultn$n''w.png _blank.png resultn$n''w.png'
    
g='-append $quadrantNameQuadrant.png resultn$n''w.png $quadrantNameQuadrant.png'
    
h='resultn$n''w.png'
    
else
        if [[ 
quadrant -eq 2 ]]; then
        a
='n=$north; n>=1; n--'
        
b='-size 0x1 xc:white resultn$n''e.png'
        
c='e=$east; e<=20; e++'
        
d='-e $n''n$e''e.png'
        
e='+append resultn$n''e.png $n''n$e''e.png resultn$n''e.png'
        
f='+append resultn$n''e.png _blank.png resultn$n''e.png'
        
g='-append secondQuadrant.png resultn$n''e.png secondQuadrant.png'
        
h='resultn$n''e.png'
        
else
            if [[ 
quadrant -eq 3 ]]; then
            a
='s=1; s<=$south; s++'
            
b='-size 0x1 xc:black results$s''w.png'
            
c='w=$west; w>=1; w--'
            
d='-e $s''s$w''w.png'
            
e='+append results$s''w.png $s''s$w''w.png results$s''w.png'
            
f='+append results$s''w.png _black.png results$s''w.png'
            
g='-append thirdQuadrant.png results$s''w.png thirdQuadrant.png'
            
h='results$s''w.png'
            
else
                if [[ 
quadrant -eq 4 ]]; then
                a
='s=1; s<=$south; s++'
                
b='-size 0x1 xc:black results$s''e.png'
                
c='e=1; e<=20; e++'
                
d='-e $s''s$e''e.png'
                
e='+append results$s''e.png $s''s$e''e.png results$s''e.png'
                
f='+append results$s''e.png _black.png results$s''e.png'
                
g='-append fourthQuadrant.png results$s''e.png fourthQuadrant.png'
                
h='results$s''e1.png'
                
else # If more than 4 quadrants, just to be safe, exit
                    
exit 1
                fi
            fi
        fi
    fi
}

for (( 
quadrant 0quadrant 4quadrant++ )); do
    
function_join
done

echo "#################################"
echo "Joining completed!"
echo "#################################"

exit 
# 2  
Old 09-21-2012
Why it's not working really depends on what $a is.

I'm not sure assembling a big giant command then feeding it into eval is a good idea.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-21-2012
Code:
((n=$north; n>=1; n--))

is what $a is supposed to be. That way,
Code:
for ${a}

actually should be
Code:
for ((n=$north; n>=1; n--))

This function has to run multiple times in slightly different ways. Normal commands and command options can easily be written as variables, but I'm not sure whether it's possible to do that for a for loop's expression.
# 4  
Old 09-21-2012
You can't put part of a command inside an eval. It has to be a whole one.

I can't help thinking there must be a simpler way to do this.
# 5  
Old 09-21-2012
Quote:
Originally Posted by Corona688
You can't put part of a command inside an eval. It has to be a whole one.

I can't help thinking there must be a simpler way to do this.
How am I supposed to do that, other than evaluating a variable for a for loop expression? Any ideas?
# 6  
Old 09-21-2012
A normal for-loop? I don't see why you've taken this approach at all.

I'm trying to work on an expanded version of this:

Code:
for Y in 19n 18n 17n ... 17s 18s 19s
do
        ARGS=""
        for X in 19w 18w 17w ... 17e 18e 19e
        do
                ARGS="$ARGS +append $Y$X.png"
        done

        convert $ARGS $Y.png
done

...but running into memory limitations. I may have to shrink all the images before I begin. Which makes sense since I'd have to shrink them severely to view the entire image at once anyway.
# 7  
Old 09-21-2012
That is some comic Smilie

Managed to compose a quarter-scale image of it, I think, but it was the .png equivalent of a zip-bomb, a 300K file which would cause things to run out of memory when they opened it. Trying a sixteenth-scale...

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression as a variable

I'm trying to use a series of regular expressions as variables but can't get it to behave properly. You can see below what I'm trying to do. Here with lowercase a-z and the same with uppercase, numbers 0-9 and again with a set of special characters, without having to type out every single... (3 Replies)
Discussion started by: 3therk1ll
3 Replies

2. Shell Programming and Scripting

Trying to execute a expression in a variable

Hi i tried to execute a below script but it is giving execution error rec=ABC,1234,55.00 Colno=2 coldel=, fd='"'$coldel'"' fprint="'"'{print$'$colno'}'"'" colsyn=`echo "echo "$rec "| awk -F"$fd $fprint` echo column syntax is $colsyn colrec=`colsyn` echo column is $colrec (5 Replies)
Discussion started by: ragu.selvaraj
5 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

assigning value of a expression to a variable

eval echo \$tts_space_name$count i m getting output of this stmnt as 'TBS_ADOX_EXTR3' but, I m not able to assign this value to a variable . i tried export j=`eval echo \$tts_space_name$count` eval j= `eval echo \$tts_space_name$count` and when i do echo $j ... i get o/p as 1 or 2... (1 Reply)
Discussion started by: Gl@)!aTor
1 Replies

6. Shell Programming and Scripting

Variable expression and while

hi, i'm reading a file "LISTE_FILE" like : # $LOGCOM * 5 $PRCCOM * 10 and i want to use the file with "while" and having the fields splitted into new variables for treatment : while read LINE do # Ignorer les commentaires un # en premiere position if then... (3 Replies)
Discussion started by: Nicol
3 Replies

7. Shell Programming and Scripting

using regular expression in for loop

Hi, i want to use regular expression in a for loop like this. for var in "/Mar/2010" do echo "Usage for the date $var" -----and want to grep $var partten from a file-------- done and it is not working. I am new to shell scripting, any one has any idea how to do it. (5 Replies)
Discussion started by: tarakant
5 Replies

8. Shell Programming and Scripting

Assigne an expression to a variable

I hope this is not a duplicate thread, but i didn't find anything similar... I had this script: filename1=/swkgr/bin/risk/GF2KGR/arch/GF2KGR_M_ filename2=/swkgr/bin/risk/GF2KGR/arch/GF2KGR_D_ day='date +%m%d' echo $filename1$day echo $filename2$day and i want this output: ... (7 Replies)
Discussion started by: raffaelesergi
7 Replies

9. Shell Programming and Scripting

[sh] While loop -> Expression recursion level exceeded

Hi! I wanted to use a while loop, like the one below, for checking words extracted by awk to terminate when a specific word appears. Unfortunately whenever I put my code inside the loop I get an error "Expression recursion level exceeded". What does it mean? What recursion? I don't have any... (4 Replies)
Discussion started by: machinogodzilla
4 Replies

10. Shell Programming and Scripting

regular expression using a variable

hello, I use AIX with ISM PILOT, I want to match something with a varible like this : $variable = 10 #this variable is the number of the job "$variable STARTED" # the pattern how can use this variable to match it with the word STARTED Tanks (0 Replies)
Discussion started by: barribar
0 Replies

Featured Tech Videos