Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean Expressions in Shell Scripts

What's the "right" way to do the following as a boolean expression?

for i in `ls $1/resources`; do
    if [ $i != "database.db" ]
    then
        if [ $i != "tiles" ]
        then
            if [ $i != "map.pdf" ]
            then
                if [ $i != "map.png" ]
                then
                    svn export -q $1/resources/$i ../MyProject/Resources/$i
...
like image 484
Adam Ernst Avatar asked Sep 07 '08 20:09

Adam Ernst


1 Answers

The other solutions have a couple of common mistakes: http://www.pixelbeat.org/programming/shell_script_mistakes.html

  1. for i in $(ls ...) is redundant/problematic just do: for i in $1/resources*; do ...

  2. [ $i != file1 -a $1 != file2 ] This actually has 2 problems.

    a. The $i is not quoted, hence names with spaces will cause issues

    b. -a is inefficient if stating files as it doesn't short circuit (I know the above is not stating files).

So instead try:

for i in $1/resources/*; do
    if [ "$i" != "database.db" ] &&
       [ "$i" != "tiles" ] &&
       [ "$i" != "map.pdf" ] &&
       [ "$i" != "map.png" ]; then
        svn export -q "$i" "../MyProject/Resources/$(basename $i)"
    fi
done
like image 106
pixelbeat Avatar answered Nov 22 '22 11:11

pixelbeat