Ls | grep -v in script

I have a bash script where I want to do this

    (in pseudo)
    for each item in excludelist
        append to string: | grep -v $item

    for each file in directory 
    do
        stuff
    done

and now

   (in real code, the creation and concatenation of f works, the problem is not here I believe)
   for i in $EXCLUDE
   do
       echo $i
       f="$f | grep -v $i"
   done

   for d in `ls $AWSTATSCONF/awstats*conf $f`
   do
       echo $d
   done

Output

   ls: |: No such file or directory
   ls: grep: No such file or directory

Any help really appreciated to make it work.

Hi,

Shadow

+3
source share
5 answers

Since you are using bash, you can use pattern matching to exclude the EXCLUDE list:

pattern=$(awk ' BEGIN {OFS="|"; printf("!(")} 
                {$1=$1; printf("%s",$0)} 
                END {print ")"}
              ' <<< $EXCLUDE)
shopt -s extglob
for d in $pattern; do
  echo $d
done    
+2
source

There are many ways to handle this.

, , ls. , , (, , ), , (, POSIX, () , ., - _).

, , :

for i in $EXCLUDE
do
    echo $i
    f="$f | grep -v $i"
done

for d in `eval ls $AWSTATSCONF/awstats*conf $f`
do
    echo $d
done

eval, $f . : eval , , .

20 greps , 20 , . , , ... .

, egrep ( grep -E):

f="antidisestablishmentarianism"
for i in $EXCLUDE
do
    f="$f|$i"
done

ls $AWSTATSCONF/awstats*conf |
egrep -v "$f" |
while read d
do echo $d
done

, , "antidisestablishmentarianism", (, , ).

while read - for. , . while -, , , , — / for. eval:

for d in $(ls $AWSTATSCONF/awstat*conf | egrep -v "$f")
do
    echo $d
done

$(...) .

+2

* grep $f.

$f = "$f $(grep -v $i)"
0

, foo whatever whatever else ls (, grep ). eval - , bash

-

for d in `eval "ls $AWSTATSCONF/awstats*conf $f"`
do
   echo $d
done

, , FAQ,

0

for i in $(ls). globbing.

grep -v.

$EXCLUDE? .

for d in "$AWSTATSCONF"/awstats*conf
do
    for e in "${exclude[@]}"
    do
        if [[ $d != $e ]]
        then
            echo "$d"
        fi
    done
done
0

All Articles