문제

I want to read a list of patterns that may (probably do) contain wildcards from a file

The patterns might look like this:

/vobs/of_app/unix/*
/vobs/of_app/bin/*

etc

My first attempt was to do this:

old_IFS=$IFS
IFS=$'\n'
array=($(cat $file))

This worked fine when the patterns did not match anything in the filesystem, but when they did match things in the filesystem, they got expanded so instead of containing the patterns, my array contained the directory listings of the specified directories. This was no good.

I next tried quoting like this

array=("$(cat $file)")

But this dumped the entire contents of the file into the 0th element of the array.

How can I prevent it from expanding the wildcards into directory listings while still putting each line of the file into a separate array element?

도움이 되었습니까?

해결책 2

array=()
while read line; do
    array+=("$line")
done < "$file"

다른 팁

Bash 4 introduced readarray:

readarray -t array < "$file"

and you're done.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top