made_in_india
made_in_india

Reputation: 2279

unable to get the complete list of file matching the regular expression in shell script

I have a directory with list of files abc_1.txt abc_2.txt. I am have to parse the file name and do some processing over the file

  #shellscript /tmp/log*
  file_list=`ls $1`
  for file_name in $file_list
  do
     # need to do some processing over the file name
     echo $file_name
  done

the script is not giving the proper output i.e script is not giving matching wildcard file name for the ls cmd. the usage of the above script is shellscript /tmp/log*

Upvotes: 2

Views: 40

Answers (3)

Walter A
Walter A

Reputation: 20002

When you want filenames without the dir you can use

if [ ! -d "$*" ]; then
        echo "Usage: $0 dirname"
        exit 1
fi
cd "$*"
find . -type f | cut -c3- | while read file_name; do
        echo "Found ${file_name}"
done

Upvotes: 1

NaN
NaN

Reputation: 8601

Bash expands shellscript /tmp/log/abc* to a list of files names as input to your script. ls is not needed. Just iterate over the input. Try it like this

for f in $*
do
 echo "Processing $f"
 # do something on $f
done

http://www.cyberciti.biz/faq/bash-loop-over-file/ even gives you some more examples.

Upvotes: 2

omuthu
omuthu

Reputation: 6333

Just remove the wildcard character "*" since it matches all the files in the given path. Removing it will just pass the directory path and your script will print each file

Upvotes: 0

Related Questions