I'm having a problem here on a bash script I made. In a for loop, I iterate on all the arguments to construct a string variable that is later fed to an "eval" command:
for arg in "$*"
do
if [ $arg != $lastArg ]; then
findTarget+="-name $arg -o "
else
findTarget=$(echo $findTarget | sed 's/-o$//')
break
fi
done
The problem stems from the "$*". For example when I enter "*.c" in the arguments, and the current folder contains files that match that pattern, the *.c
argument is expanded into those files; I do not want that, I want findTarget
to be concatenated with -name *.c -o
, I have tried with and witout quotes, using eval, nothing seems to work. Any idea how to do this (simply if possible) ? Note: the total number of arguments can vary.
This is an example of how I run the script:
$ trouver.bash *.c *.f90 someString
At the end of my for loop, the variable findTarget
should read -name *.c -o -name *.f90
This does not work if the *.c
or *.f90
match files in the current folder...
"$*"
Since you double quoted this, it will not word split, and the loop will only run once"$*"
is the concatenation of the positional parameters, see How to use arguments like $1 $2 … in a for loop?