Why is this bash script failing?

Red Squirrel

No Lifer
May 24, 2003
69,722
13,341
126
www.betteroff.ca
Code:
#!/bin/bash

BASEMAILDIR="/localdata/mail/vhosts/"


function LearnMailBox()
{
        USER=$1
        DOMAIN=$2

        echo "Processing bayes for ${USER}@${DOMAIN}..."



        mkdir -p "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/new"
        mkdir -p "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/cur"
        mkdir -p "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/tmp"



#       sa-learn --spam "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes"

        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/new/*" "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/new" #2>/dev/null
        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/cur/*" "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/cur" #2>/dev/null
        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/tmp/*" "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/tmp" #2>/dev/null
}



LearnMailBox "user" "domain"


Specifically, this part:

Code:
        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/new/*" "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/new" 
        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/cur/*" "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/cur" 
        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/tmp/*" "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/tmp"


Keep getting error "no such file or directory" but yet if I type out the full path manually (even copying and pasting the path right from the error) in the console it works. The folders do exist. It only works when I do it manually and not when running the script.
 

Red Squirrel

No Lifer
May 24, 2003
69,722
13,341
126
www.betteroff.ca
Ended up just rewriting it to use rsync with option to delete source files after copy. mv seems to not like using the star inside a script, with quotes.
 

orenero

Senior member
Apr 12, 2005
302
0
76
Next time in your scripts / trouble shooting try running "set -x" it will show extra output which is valuable in troubleshooting.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,566
4,483
75
I keep running into this recently. Bash won't parse globs inside quotes (on its own). But if you can leave the glob outside the quotes, it should work.

Code:
        mv -v "${BASEMAILDIR}${DOMAIN}/${USER}/.spam-bayes/new/"* "${BASEMAILDIR}${DOMAIN}/${USER}/.spam/new/"

It's good practice to leave a trailing slash on the end of a mv destination when you want it to be a directory. Otherwise a file might someday accidentally get renamed to "new".
 

mv2devnull

Golden Member
Apr 13, 2010
1,519
154
106
First, you probably know the
mkdir -p somepath/{new,cur,tmp}
and
for DIR in new cur tmp
do mv src/${DIR}/* dst/${DIR}
done


Second, it does make a difference for
mv src/* dst
whether
* the src/* resolves to one name, many, or none
* the dst exist or not
* the dst is a directory or file

Do the ${BASEMAILDIR}${DOMAIN}/${USER}/.spam/{new,cur,tmp} exist before the move, i.e. will you add files to them, or will they be new? If new, then why not just move the entire directories, and not all of their contents?
 

Red Squirrel

No Lifer
May 24, 2003
69,722
13,341
126
www.betteroff.ca
I want the old folder to still exist. The folder at the destination will also already exist, but in cases where it doesn't I want to make sure it gets created.

I have it all working now though. The jist of it is I have a "spam-bayes" folder in every mailbox, when I get spam that did not get flagged I move it to that folder. A script runs to learn from the spam and then moves it when it's done with it.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |