Extract quoted words in shell script

I am creating a shell script that automates the process of installing Arch Linux AUR packages . I need to list all the package dependencies (to check if they are installed), they look like this: install script:

depends=('sdl' 'libvorbis' 'openal')

The easiest way (or the only idea) that I could come up with is something like this:

grep "depends" PKGBUILD | awk -F"'" '{print $2 "\n" $4 "\n" $6;}'

But the number of dependencies varies from package to package. So, how am I putting names in quotation marks if the number of words changes?

Thanks in advance,

-skazhy

+3
source share
5 answers

Try this for size:

grep "depends" PKGBUILD > /tmp/depends
. /tmp/depends

echo ${depends[@]}

Hey look, is this an array? Yes it is.

for d in "${depends[@]}" ; do
    printf '"%s"' "$d"
done

. script .

+1

depends - , bash ... "", . , :

depend_line=`grep depends $PKGBUILD`
eval "${depend_line}"
echo ${depend[0]} # Will print sdl in your example
+4

eval declare:

declare -a "$(grep "depends" PKGBUILD)"

"", "sdl", "libvorbis" "openal" .

+3

- :

grep "depends" PKGBUILD | perl -e "while(<>){print \"\$1\\n\" while m/'.{-}'/g;}"
0

awk -F"'" '{for(i=2;i<=NF;i+=2) print($i)}'

0

Source: https://habr.com/ru/post/1772098/


All Articles