im having trouble getting a dynamically generated shell function to evaluate properly (by evaluate i mean "eval" command)
this is the desired statement to be evaluated when ran (except the output will be executed/evaluated by eval)
$ package_check_repo_printf
if [ $(printf '%s\n' "found ${treelistnew0[@]}" | grep -i -e ^"$1") ]
then
url=$POOL0$1
ftp=$ftpPOOL0$1
echo $1 found in $POOL0
elif [ $(printf '%s\n' "found ${treelistnew1[@]}" | grep -i -e ^"$1") ]
then
url=$POOL1$1
ftp=$ftpPOOL1$1
echo $1 found in $POOL1
else
echo $1 not found
fi
and its code:
package_check_repo_printf() {
for i in ${!REPOS[@]}
do
if [[ $ifgen == "" ]]
then
printf '%s' "if [ \$(printf '%s\n' \"found \${treelistnew0[@]}\" | grep -i -e ^\"\$1\") ]
then
url=\$POOL0\$1
ftp=\$ftpPOOL0\$1
echo \$1 found in \$POOL0
"
ifgen=gen
else
printf '%s' "elif [ \$(printf '%s\n' \"found \${treelistnew$i[@]}\" | grep -i -e ^\"\$1\") ]
then
url=\$POOL$i\$1
ftp=\$ftpPOOL$i\$1
echo \$1 found in \$POOL$i
"
fi
done
printf '%s' "else
echo \$1 not found
fi"
}
when i replace all the printf '%s' with eval i get this
package_check_repo_eval() {
set -v
for i in ${!REPOS[@]}
do
if [[ $ifgen == "" ]]
then
eval "if [ \$(printf '%s\n' \"found \${treelistnew0[@]}\" | grep -i -e ^\"\$1\") ]
then
url=\$POOL0\$1
ftp=\$ftpPOOL0\$1
echo \$1 found in \$POOL0
"
ifgen=gen
else
eval "elif [ \$(printf '%s\n' \"found \${treelistnew$i[@]}\" | grep -i -e ^\"\$1\") ]
then
url=\$POOL$i\$1
ftp=\$ftpPOOL$i\$1
echo \$1 found in \$POOL$i
"
fi
done
eval "else
echo \$1 not found
fi"
set +v
}
and its output ( note for this to be CORRECT it should look the same as the package_check_repo_printf's output as set -v will echo the lines executed as printf is not printing the lines) :
[chakra@chakra-pc UPM]$ unset ifgen
[chakra@chakra-pc UPM]$ package_check_repo_eval
if [ $(printf '%s\n' "found ${treelistnew0[@]}" | grep -i -e ^"$1") ]
then
url=$POOL0$1
ftp=$ftpPOOL0$1
echo $1 found in $POOL0
bash: syntax error: unexpected end of file
elif [ $(printf '%s\n' "found ${treelistnew1[@]}" | grep -i -e ^"$1") ]
bash: syntax error near unexpected token `elif'
else
bash: syntax error near unexpected token `else'
[chakra@chakra-pc UPM]$
how would i get this to work correctly?
note: just to be clear, my goal is to make this function EVAL based instead of PRINTF based
a better example would be this (although im not sure if this is acomplishable another way but still, it generates a usable if elif else statement)
dogs=(1 2 3)
dog=(pink blue white)
colour=(pink black white)
dogs_printf() {
unset ifgen
for i in ${!dogs[@]}
do
if [[ $ifgen == "" ]]
then
printf '%s' "for i in \${!dogs[@]}
do
if [[ \${dog[i]} == \"\${colour[$i]}\" ]]
then
echo dog$i is \"\${colour[$i]}\"
"
ifgen=gen
else
printf '%s' "elif [[ \${dog[i]} == \"\${colour[$i]}\" ]]
then
echo dog$i is \"\${colour[$i]}\"
"
fi
done
printf '%s' "else
echo dog \$i not found
fi
done
"
}
dogs_printf
its output:
[chakra@chakra-pc UPM]$ dogs=(1 2 3)
[chakra@chakra-pc UPM]$ dog=(pink blue white)
[chakra@chakra-pc UPM]$ colour=(pink black white)
[chakra@chakra-pc UPM]$ dogs_printf() {
> unset ifgen
> for i in ${!dogs[@]}
> do
> if [[ $ifgen == "" ]]
> then
> printf '%s' "for i in \${!dogs[@]}
> do
> if [[ \${dog[i]} == \"\${colour[$i]}\" ]]
> then
> echo dog$i is \"\${colour[$i]}\"
> "
> ifgen=gen
> else
> printf '%s' "elif [[ \${dog[i]} == \"\${colour[$i]}\" ]]
> then
> echo dog$i is \"\${colour[$i]}\"
> "
> fi
> done
> printf '%s' "else
> echo dog \$i not found
> fi
> done
> "
> }
[chakra@chakra-pc UPM]$ dogs_printf
for i in ${!dogs[@]}
do
if [[ ${dog[i]} == "${colour[0]}" ]]
then
echo dog0 is "${colour[0]}"
elif [[ ${dog[i]} == "${colour[1]}" ]]
then
echo dog1 is "${colour[1]}"
elif [[ ${dog[i]} == "${colour[2]}" ]]
then
echo dog2 is "${colour[2]}"
else
echo dog $i not found
fi
done
[chakra@chakra-pc UPM]$ for i in ${!dogs[@]}
> do
> if [[ ${dog[i]} == "${colour[0]}" ]]
> then
> echo dog0 is "${colour[0]}"
> elif [[ ${dog[i]} == "${colour[1]}" ]]
> then
> echo dog1 is "${colour[1]}"
> elif [[ ${dog[i]} == "${colour[2]}" ]]
> then
> echo dog2 is "${colour[2]}"
> else
> echo dog $i not found
> fi
> done
dog0 is pink
dog 1 not found
dog2 is white
[chakra@chakra-pc UPM]$