-1

I have a list of files

  • shopping-list.txt
  • our-shopping-list.txt
  • test.txt
  • my-test.txt

I want to run myscript shopping and get the two files that have the word shopping. I want to run myscript our list and get just the one file.

at the moment I have this

if [[ $fs =~ .*${*}*.* ]]; then
        echo $fs
fi

It works a bit, but it would not give me our-shopping-list if each variable has a gap ie. myscript our list it would work if I typed myscript our - list

I have a big list of files and want to find the one I need by guessing the name


my attempt to apply @pacholik's code

snippetdir="~/my_snippets/"
    for filename in $snippetdir*; do
        file=`basename "$filename"`
        fs=${file%.*}
        for i in ${*}; do
    for j in *${i}*; do
        if [[ $fs =~ .*$j*.* ]]; then
                echo $fs
            fi      
        done
        done
    done
relidon
  • 2,142
  • 4
  • 21
  • 37

2 Answers2

1

Here's a simple brute-force loop.

for file in *; do
    t=true
    for word in "$@"; do
        case $file in
          *"$word"*) ;; 
          *) t=false; break;;
        esac
    done
    $t && echo "$file"
done

I believe this should be portable to any POSIX shell, and potentially beyond (old Solaris etc).

tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

Using just bash expansion (only you have to evaluate twice). Bash join from here.

files=`IFS=* eval 'echo *"${*}"*'`

Then you can iterate over $files

for i in $files; do
    echo $i
done
Community
  • 1
  • 1
pacholik
  • 8,607
  • 9
  • 43
  • 55