• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Why is this bash script failing?

Red Squirrel

No Lifer
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.
 
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.
 
Next time in your scripts / trouble shooting try running "set -x" it will show extra output which is valuable in troubleshooting.
 
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".
 
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?
 
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.
 
Back
Top