Bash script which sorts files to folders by it'

2019-08-10 03:37发布

I'm scripting a little helper that's sorts files to different folders, but why?

The mechanic:

On my linux suse server runs a programm that creates zero to unlimited files like "file.1.1" the last number is increasing (file.1.1, file.1.2, file.1.3, and so on). In this files is variable text which contains a keyword, in this example are there fruits. Each kind of fruit is a order and is converted by another script to a e-mail. The problem is, this other script take all files and convert all of them to a mail and send them to one supplier. That's not good.

So I'll sort them to different directorys for converting them to mails for the correct supplier.

As little example:

file.1.1 is an order for apples, so my script searches for the keyword "apple". Contains this file "apple", then the script move this file to the folder "apple". If not, then it move the file to the correct "fruit folder".

The directory structure is like this:

/home/me/sandbox/          <- here are all the files  
/home/me/sandbox/apple/    
/home/me/sandbox/strawberry/    
/home/me/sandbox/cherry/    
/home/me/sandbox/plum/

Now my script:

#! /bin/bash -x

#Variablen Deklaration start
declare -a array;                               
array[key1]='strawberry'
array[key2]='apple'

VAR1="strawberry"
VAR2="apple"

XXSTRAWBERRY="/home/me/sandbox/strawberry/"
XXSTADTBIB="/home/me/sandbox/stadtbib/"
#Variablen Deklaration ende

find file.1.* > dateiliste.txt

for fn in `cat dateiliste.txt`; do
        for key1 in ${!array[@]}; do
            temp=$(find $fn | xargs grep $VAR1) #my problem
                if [ "$temp" = *"$VAR1"* ]; then
                   cp $fn $XXSTRAWBERRY
                else
                   cp $fn $XXAPPLES
                fi
        done
done

The bash output is this:

me@server=test:~/sandbox > ./new\ \ 1.sh
+ declare -a array
+ array[key1]=strawberry
+ array[key2]=apple
+ VAR1=strawberry
+ VAR2=apple
+ ORTstrawberry=/home/me/sandbox/strawberry/
+ ORTapple=/home/me/sandbox/apple/
+ find file.1.1 file.1.2 file.1.3 file.1.4 file.1.5
++ cat dateiliste.txt
+ for fn in '`cat dateiliste.txt`'
+ for key1 in '${!array[@]}'
++ xargs grep strawberry
++ find file.1.1
+ temp=
+ '[' '' = '*"$VAR1"*' ']'
+ cp file.1.1 /home/me/sandbox/apple/
+ for fn in '`cat dateiliste.txt`'
+ for key1 in '${!array[@]}'
++ xargs grep strawberry
++ find file.1.2
+ temp='Bestellung fuer strawberry'
+ '[' 'Bestellung fuer strawberry' = '*"$VAR1"*' ']'
+ cp file.1.2 /home/me/sandbox/apple/
+ for fn in '`cat dateiliste.txt`'
+ for key1 in '${!array[@]}'
++ xargs grep strawberry
++ find file.1.3
+ temp=
+ '[' '' = '*"$VAR1"*' ']'
+ cp file.1.3 /home/me/sandbox/apple/
+ for fn in '`cat dateiliste.txt`'
+ for key1 in '${!array[@]}'
++ xargs grep strawberry
++ find file.1.4
+ temp='Bestellung fuer strawberry'
+ '[' 'Bestellung fuer strawberry' = '*"$VAR1"*' ']'
+ cp file.1.4 /home/me/sandbox/apple/
+ for fn in '`cat dateiliste.txt`'
+ for key1 in '${!array[@]}'
++ xargs grep strawberry
++ find file.1.5
+ temp=
+ '[' '' = '*"$VAR1"*' ']'
+ cp file.1.5 /home/me/sandbox/apple/

So there is my problem.

The files contain a lot of content, so the keyword is not alone. With my temporary variable "$temp" I want the output from grep with the correct keyword. That works, but for the balance between the grep output in $temp and the keyword in $var1 I need a wildcard.

So if the grep output ($temp) contains apple ($var1) then move (in the example copy) the file.1.* ($fn) to apple/ ($XXAPPLE).

How can I realize this easily? I've not much experience in bash scripting

Thank you all :)

1条回答
甜甜的少女心
2楼-- · 2019-08-10 04:01
#!/bin/bash 

#Variablen Deklaration start
declare -a fruits;                               
fruits=(strawberry apple banana)

ls file.1.* | while read f
do
    for fruit in "${fruits[@]}"; do
       grep -i $fruit "$f" >/dev/null 2>&1
       if [ $? -eq 0 ]; then
          echo Copying $f to $fruit
          cp "$f" $fruit
       fi
    done
done
查看更多
登录 后发表回答