views:

55

answers:

5

Hi!

I am making a shell script that will automate the install process of Arch Linux AUR packages. I need to list all dependencies of the package (to check if they are installed), they appear like this in 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 dependency count varies from package to package. So, how I output the names in quotes if the word count is varying?

Thanks in advance,

-skazhy

A: 

You could do something like:

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

Try this on for size:

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

echo ${depends[@]}

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

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

Note: In a real script you'd want to be more careful with the naming of the temporary file.

Sorpigal
Why creating a temporal file whtn you have "eval"? :) See my response.
Diego Sevilla
Because eval is just plain dangerous. Sourcing is less risky.
Sorpigal
Excuse me, but in this case, with the source ("."), you are just *evaluating* the file you're reading...
Diego Sevilla
Encouraging the use of a dangerous tool is a good way to wind up with your foot shot off.
Sorpigal
Again, the source just EVALUATES all the input... So "eval" is dangerous. "source" (.) is not, because...? (actually does the same, but...? )
Diego Sevilla
@Sorpigal: that's nonsense. Eval is a tool. If you learn to use it properly it's no more dangerous than any other tool. In fact, in this case "." is _more_ dangerous because you could in theory eval the wrong file if a race condition occurred and a malicious program rewrote the file between the time you create it and eval it.
Bryan Oakley
I'm not saying there is any difference functionally between eval and source, I am saying "I do not teach the use of eval." But I do sometimes teach the use of source.
Sorpigal
@Sorpigal: I think teaching someone to avoid a tool rather than teaching them to understand it is doing a disservice to the student.
Bryan Oakley
I disagree. I wouldn't teach GOTO either, for similar reasons. I will not hide its existence I will only say "Don't use it. When you think you really need to, think about it twice." and leave the rest up to the student. I would never *recommend* it and certainly would not without an elaborate warning about the dangers and proper uses.
Sorpigal
What are all the extra quotes for in the `printf` statement? This works fine: `printf '"$d"\n'` as does: `printf '"%s"' "$d"`.
Dennis Williamson
@Dennis Williamson: I don't know about you, but here (bash3.2.48, freebsd) the `printf '"$d"\n'` version just prints `"$d"` (which is what I would expect: bash does not interpolate $d). You're right, though, that the latter would be simpler and clearer--and arguably a more correct usage of printf.
Sorpigal
@Sorpigal: Oops, ignore the first version in my comment. Sorry.
Dennis Williamson
I've switched out my printf statement for yours. Chalk it up to too many years of echo. You can easily teach a man to printf instead, but changing his thinking is harder.
Sorpigal
+4  A: 

If the depends is just one line, one thing you may try is to evaluate the line in bash itself... This will lead to an array called "depends" that holds all the values. Seems tricky, but not with dynamic languages:

depend_line=`grep depends $PKGBUILD`
eval "${depend_line}"
echo ${depend[0]} # Will print sdl in your example
Diego Sevilla
+1 for avoiding use of a temporary file.
Bryan Oakley
A: 

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

glenn jackman
+2  A: 

You can avoid the security issues of using eval or sourcing a temporary file by using declare:

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

This will create an array called "depends" containing "sdl", "libvorbis" and "openal" based on the example data.

Dennis Williamson
+1 from me; this is what I was about to suggest in an edit of mine just to sidestep the eval/no eval question. A far better (and bashier) solution.
Sorpigal