Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I run this bash script in order to put all the files with the following format dated today in the same folder. for example here I want 2012-12-17_083213 to be my folder and then inside it I have the .log and .log_stats and etc with this name inside it?

But I am not able to get the script working. For example, if I try everything up until dir=$(echo $i | awk '{split($1,a,"_"); print a[1]}');
And then to test it, I did echo $dir, and that displays the following:
fwui.log
I searched and understood its about locks in the db but how to solve it?

This is the script:

#!/bin/bash  
tosend=""  
for i in *.log; do     
    dir=$(echo $i | awk '{split($1,a,"_"); print a[1]}');  
    if [ ! -d $dir ]; then  
        mkdir $dir;  
    fi;  
    cp $i $dir;  
    tosend="$tosend\n$dir"  
done  
for i in $(echo -e $tosend | uniq);do  
    echo Compressing $i  
    tar -zcvf $i.tar.gz $i  
    scp $i.tar.gz user@server  
done  

and this is the file formats:

2012-12-17_083213_1.log  
2012-12-17_083213_1.log_stats  
2012-12-17_083213_1.logaccount_ptr    
2012-12-17_083213_1.loginitial_ptr  
2012-12-17_083213_1.logptr  
2012-12-17_093049_2.log  
2012-12-17_093049_2.log_stats  
2012-12-17_093049_2.logaccount_ptr  
2012-12-17_093049_2.loginitial_ptr  
2012-12-17_093049_2.logptr  
share|improve this question
    
It is more than just user@server in your code, right? Because if there's no : (as in user@server: for the home of user on server) in the target spec of scp, it works like cp, only copying all your files to the file user@server. –  sr_ Jan 3 '13 at 17:29
    
@Renan I don't understand, Could you please give me an example? –  Jimmy A Jan 4 '13 at 14:12
    
@Renan ok here is the thing the problem is that i also have files in the fwui.log format that are being catched, these files can not be moved and therefore the error comes, what can i do to not catch them? –  Jimmy A Jan 7 '13 at 16:03
add comment

2 Answers

You should start with for i in *.log*; do instead of for i in *.log; do; otherwise you only get files that ends in .log. Beside the fact that it may possibile be rewritten simpler, I cannot see any other problem in you script.

share|improve this answer
    
yes you are right but this is independent of the error. I don't know how to solve the error. –  Jimmy A Jan 3 '13 at 16:45
    
ok here is the thing the problem is that i also have files in the fwui.log format that are being catched, these files can not be moved and therefore the error comes, what can i do to not catch them? –  Jimmy A Jan 7 '13 at 16:03
    
Inside your loop, check for the value for the i variable; if it is a name you want to skip, just call continue this way: if [ "$i" = "fwui.log" ]; then continue; fi –  eppesuig Jan 7 '13 at 20:55
add comment

first change that 1st awk line with:

dir="$(echo "$i" | awk '{split($0,a,"_"); printf "%s_%s",a[1],a[2]}')"

so that, in case they contain whitespaces, it will still work.

Then replace the "for i in" line with:

echo -e "$tosend" | grep -v '^$' | sort | uniq | while IFS= read i ;do

and add some "" around relevant places:

#!/bin/bash  
tosend=""  
for i in 20*.log*; do     
    dir="$(echo "$i" | awk '{split($1,a,"_"); printf "%s_%s",a[1],a[2]}')";  
    if [ ! -d "$dir" ]; then  
        mkdir "$dir" || { echo "ERROR during: mkdir '$dir'" ; exit 1 ; };  
    fi;  
    cp "$i" "$dir" || { echo "ERROR during: cp '$i' '$dir'" ; exit 2 ; };  
    tosend="${tosend}\n${dir}"  
done  
echo -e "$tosend" | grep -v '^$' | sort | uniq| while IFS= read i ;do  
    echo "Compressing $i"  
    echo REMOVE THIS ECHO ONCE TESTED tar -zcvf "${i}.tar.gz" "$i" \
    && { echo REMOVE THIS ECHO ONCE TESTED scp "${i}.tar.gz" user@server:/tmp/somedir/ \
         || { echo "ERROR during: scp '${i}.tar.gz' user@server:/tmp/somedir/" ; exit 4 ; };
       }
    || { echo "ERROR during: tar -zcvf '${i}.tar.gz' '$i'" ; exit 3 ; }; 
done  

you may want to take out the "v" part in the tar, to save some display (once you know it works)

another way for the above:

replace my

for i in 20*.log*; do 

with:

ls -1d *|grep '^[0-9]*-[0-9]*-[0-9]*_[0-9]*_[0-9]*\.log.*'| while IFS= read i ; do

Or simplify the grep :

ls -1d *|grep '^[0-9_-]*\.log' | while IFS= read i ; do

which should be enough (but could match a "./-_-1234.log.hahaha" file ...)

share|improve this answer
    
ok here is the thing the oroblem is that i also have files in the fwui.log format that are being catched, these files can not be moved and therefore the error comes, what can i do to not catch them? –  Jimmy A Jan 7 '13 at 16:02
    
I'll edit my solution to only loop on the relevant files –  Olivier Dulac Jan 7 '13 at 16:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.