Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval

Status
Not open for further replies.

krava

Programmer
Jun 4, 2007
48
0
0
YU
Hi,

I am trying to use awk in bash and have no idea why this not works

for fl in `find -name "*.jpg"`;do
echo $fl;
eval $x=\"$(awk -v name=$fl 'BEGIN{print match(name,/less/)}')\";
echo $x;
done

I would use $x differently afterwards, depending on result ($x = 0 or not) but first to figure out what is wrong here.

p.
 
You don't need to use eval ;
Code:
for fl in `find -name "*.jpg"`;do
 x=$(awk -v name=$fl 'BEGIN{print match(name,/less/)}')
 if [ $x -eq 0 ]
 then
    echo "No match for file $fl"
 else
    echo "Match at position $x for file $fl"
 fi
done
Example:
Code:
$ [blue]ls *.jpg[/blue]
file_less.jpg  file_more.jpg
$ [blue]krava.sh[/blue]
Match at position 8 for file ./file_less.jpg
No match for file ./file_more.jpg
$
If the variable x contains the name of the variable that must be set with the result value :
Code:
eval $x=\\$(awk -v name=$fl 'BEGIN{print match(name,/less/)}')




Jean-Pierre.
 
thanx Pierre

I wanted to use this what you wrote but something is wrong

This program should give back 1 if the patern "dat" is within name "less_filx22dat.txt" and 0 otherwise BUT is not good

#usage: matchBool "less_filx22dat.txt" "dat" b
matchBool(){
x=$3;
eval $x=\\$(awk -v name=$1 -v patern=$2 'BEGIN{print match(name,/patern/)}');
if [ $x -eq 0 ]
then
$x=0
else
$x=1
fi
}

 
Code:
#usage: matchBool "less_filx22dat.txt" "dat" b
matchBool(){
    x=$3;
    eval $x=\\$(awk -v name=$1 -v patern=$2 'BEGIN{print match(name,patern)}');
    if eval [ \$$x -eq 0 ]
     then
       eval $x=0
    else
       eval $x=1
     fi
}
Can be simplified:
Code:
#usage: matchBool "less_filx22dat.txt" "dat" b
matchBool(){
    local x
    x=$(awk -v name=$1 -v patern=$2 'BEGIN{print match(name,patern)}');
    if [ $x -eq 0 ]
     then
       eval $3=1
    else
       eval $3=0
     fi
}
More...
Code:
#usage: matchBool "less_filx22dat.txt" "dat" b
matchBool(){
    eval $3=\\$(awk -v name=$1 -v pattern=$2 'BEGIN{print (match(name,pattern) ? 1 : 0)}');
}



Jean-Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top