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 :)