ABC
ABC

Reputation: 1477

Disable wildcard expansion to Bash script not working

When I try to prevent the wildcard expansion to the following script it doesn't work. For example in the shell I type: (I have also tried \*.m, "*.m")

$ getLargeNumFiles.sh $(pwd) '*.m'

And I get

$ myDirectory
$ file1.m file2.m file3.m
$ find: paths must precede expression
$ Usage: find [-H] [-L] [-P] [path...] [expression]
$ 0 #The result of my function. Found 0 .m files. Should be 3.

getLargeNumFiles.sh

#!/bin/bash
# Syntax
# getLargeNumFiles [folderPath][ext]
# [folderPath] The path of the folder to read.
#   To use at folder to search: $(pwd)

function largeRead()
{   
    numFiles=0
    while read line1; do
        #echo $line1
        ((numFiles++))
    done
    echo $numFiles
}
folderPath=$1
ext=$2

echo $folderPath
echo $ext

find $folderPath -name $ext | largeRead

Upvotes: 0

Views: 578

Answers (1)

Barmar
Barmar

Reputation: 780994

Put your variables in quotes to prevent wildcard expansion after the variable is expanded:

find "$folderPath" -name "$ext" | largeRead

Upvotes: 2

Related Questions