Find: paths must precede expression

I am using the final version of CentOS 6.4 on two machines. I am executing a script. script contains a commandfind

path=$1
searchstring=$2 
echo `find $path -name $searchString`
for filename in `find $path -name $searchString`
do
echo "$filename"
echo
done

./findfiles.sh /var/log/ *.txt

The above script does fine and prints files. But in the second car I getusage error: find: paths must precede expression

The reason is that * .txt expanded in the find command. After changing the file name in, find $path -name "$searchString" it does a fine.

Why doesn't the syntax error happen on the first CentOS machine?

+4
source share
2 answers

Enter your shell arguments:

$ ./findfiles.sh /var/log/ '*.txt'
+7
source

You need to use the correct quotation or the shell will try to expand *.txtbefore it findsees it.

, a.txt b.txt, find /var/log -name a.txt b.txtfind, b.txt

. script :

path="$1"
searchstring="$2" 
echo $(find "$path" -name "$searchString")
for filename in $(find "$path" -name "$searchString")
do
    echo "$filename"
    echo
done

, ( glob , searchstring. , .

$()! , "" .

$() inline-eval back-ticks. , .

+6

Source: https://habr.com/ru/post/1523288/


All Articles