简体   繁体   中英

How to have the list of files represented by a regex given in input to a bash script

I'm creating a code for the automatic extraction of bib records from scientific papers.

In an old version of the script i gave in input the name of the folder where all the pdfs were stored, now I want to give a regex. Eg before:

./AutoBib.sh Papers/

Now:

./Autobib.sh Papers/*.pdf

In the folder there are, for example 3 pdf files: Shrek.pdf, Fiona.pdf, Donkey.pdf, using my script I should be able to retrieve the doi from all files creating a file where all doi are listed but executing my script it returns the doi of the first file and nothing more.

Here there is my code:

for i in $1; do
    doi $i
done

doi is a function that extract the doi from a pdf and puts it in a txt file. When i run the script it returns me only the doi of the first file.

How can I feed a regex in my script and being able to iterate though all files that matches that regex?

It's important to understand that Papers/*.pdf is not a regular expression, it's a wildcard pattern that causes bash to perform filename expansion, or globbing .

$1 represents the first argument to your script, so your for loop is only ever iterating over that one argument.

Use $@ to represent all arguments:

for i in "$@"; do
    doi "$i"
done

If you want to filter files within directory by pattern, you can pass this pattern as second script parameter and search for matching files using find .

Here is the code. It's additionally resistant to filenames containing spaces:

find "$1" -maxdepth 1 -name "$2" -exec doi {} \;

Usage example: ./Autobib.sh Papers/ *.pdf

You can just run the ls command in loop and it will solve your problem.

for x in $(ls $@/*.pdf)
do
echo $x  ## if you want only file name you can change this line to echo `basename $x`
done

I have created the same scenario as you mentioned above, refer the snapshot.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM