Puzzle for Programmers

Need help with C, C++, perl, python, etc?

Postby Blyiss » 2007-02-24 21:30

Thank you, guys, you are great programmers.
Dri, I changed the line to
Code: Select all
for file in $( ls -1 $d/ | grep '^,[0-9][0-9]*$' )

It removes files OK. But some files in the trash were overwritten. The output should be 20 files:
Code: Select all
Current state:
drafts:
1  ,1  15  ,15  16  ,16

inbox:
1  ,1  7  ,7  8  ,8  9  ,9

outbox:
1  ,1  3  ,3  6  ,6

trash:
1  10  15  16  17  3  6  7  8  9


 Final contents of trash:
1  10  15  16  17  2  3  4  5  6  7  8  9


Grifters's version is really great!!!
I changed ${DEUX}/${i} to just $i,
and it works just fine, exactly like a charm. Thanks a lot!
Probably I misunderstood a bit when you said that trash folder should not be an existing mailbox that has to compete with 'real' mails. I made trash right in the folder which MH considers to be a real mailbox (I mean ~/Mail, it reads email from the spoolfolder, "inbox" in my case), and the script works just wonderful! Thank you, very much!!
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Postby Blyiss » 2007-02-24 21:50

Sorry, Grifter, after posting I notices that trash inside the mailbox doesn't work. But it is fine, when outside ~/Mail folder.
Code: Select all
$ ./movetotrash2
./movetotrash2: line 25: let: 10~: syntax error in expression (error token is "~")
./movetotrash2: line 50: let: 10~: syntax error in expression (error token is "~")
./movetotrash2: line 50: let: 10~: syntax error in expression (error token is "~")
./movetotrash2: line 50: let: 10~: syntax error in expression (error token is "~")

Is there a way to put the files to the trash that is inside the mailbox? Otherwise Mutt doesn't except the folder, if it didn't compiled it by itself. I didn't try Emacs yet.
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Postby Grifter » 2007-02-24 22:28

Right it should be a real valid mailbox, but it should only contain files that have been dumped there with the script, it shouldn't generate its own mails or move 'real' mails in there, only the dumped stuff should be there, otherwise when it performs VARCOUNT, it will probably do weird stuff


Blyiss wrote:Sorry, Grifter, after posting I notices that trash inside the mailbox doesn't work. But it is fine, when outside ~/Mail folder.
Code: Select all
$ ./movetotrash2
./movetotrash2: line 25: let: 10~: syntax error in expression (error token is "~")


Is there a way to put the files to the trash that is inside the mailbox? Otherwise Mutt doesn't except the folder, if it didn't compiled it by itself. I didn't try Emacs yet.


The error there seems to be the cause of a ~, maybe one of the mails has a ~ after it, when VARCOUNT gets the number, it strips the leading zeroes, but expects only digits in the filename, if a filename is called 00011~ it would probably error the same way you posted, if the reader is the one that puts ~ in the filename that character would have to be stripped away too, which is a pretty simple thing to do

I'm not really sure what you mean by trash files inside the mailbox, I guess it has to do with the the paths

for example, let's say you are in your ~/Mail dir, if you type there, find -name ',*' it would locate all the ,filenames with the relative path, ie it would say ./somedir/,1

but if you instead typed (in that same dir) find /home/luser/mail -name ',*', it would now print the full path

if you set UN=/home/luser/Mail/dumpdir and DEUX=/home/luser/Mail in the script, it should work fine, the beginning of the loop states, find $DEUX -name, so if DEUX has the full path, it would mean find /home/luser/Mail -name, and when i gets its value it should be the filename with the full path

but the errors you got, were line 25 and line 50, the "let" lines, where it's supposed to move the number one step higher, but assuming it thinks the number is 10~, it doesn't know how to change it up to 11, because of the ~ character which I assume exists on one (or more) file(s)

to bypass that, the line that says

VARCOUNT=`ls $UN | sed -r 's/(0*)(.*)/\2/' | tail -n 1`

change that to

VARCOUNT=`ls $UN | sed -r 's/(0*)(.*)/\2/' | tail -n 1 | sed 's/~//g'`

basically just piping it to a sed 's/~//g', to remove this tilde character, the filename with the tilde would remain unchanged but it wouldn't interfere with the variable that tries to determine which number we're at
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby drl » 2007-02-24 23:07

Hi.

This output:
Code: Select all
% ./s1
 total = 23
 trash = 10

 Current state:
drafts:
#1#  ,01  ,1  ,44fox  ?1

inbox:
,1  ,7  ,8  ,9  1  7  8  9

outbox:
,01  ,02  ,1  ,3  ,6  ,hello,world  1  3  6  list,3

trash:
1  10  15  16  17  3  6  7  8  9


 Final   state:
drafts:
#1#  ,44fox  ?1

inbox:
1  7  8  9

outbox:
,hello,world  1  3  6  list,3

trash:
1  10  11  12  13  14  15  16  17  18  19  2  20  21  3  4  5  6  7  8  9

 total = 12
 trash = 21

was created by this script:
Code: Select all
#!/bin/sh

# @(#) s1       Demonstrate move and rename.

# Provide audit trail for learning and debugging.
# Interchange the two debug lines to toggle printing on and off.

debug="echo"
debug=":"

# Set up the test situation.
# For production, comment out the remove and create calls.

./remove
./create

count() {
  local a b c d total
  a=$( ls -1 drafts | wc -l )
  b=$( ls -1 inbox | wc -l )
  c=$( ls -1 outbox | wc -l )
  d=$( ls -1 trash | wc -l )
  total=$( expr $a + $b + $c )

  echo " total = $total"
  echo " trash = $d"
}
count

echo
DIRS="drafts inbox outbox"
echo " Current state:"
ls -R $DIRS trash

echo
$debug " audit trail of operations:"
$debug

highest=0
for d in $DIRS
do
  for file in $( ls -1 $d/ | grep '^,[0-9][0-9]*$' )
  do
    $debug " Working on $d/$file"
    n=${file#,}
    $debug " extracted :$n: from :$file: in :$d:"
    m=$( expr $n + 0 )
    $debug " transformed $n into $m"
    xform=$m
    if [ -f trash/$xform ]
    then
      highest=$( expr $highest + 1 )
      $debug " set highest number to :$highest:"
      while [ -f trash/$highest ]
      do
        $debug " highest increased to $highest"
        highest=$( expr $highest + 1 )
      done
      mv $d/$file trash/$highest
      $debug " moved (bumped) $d/$file to trash/$highest"
    else
      # echo mv $d/$xform trash
      mv $d/$file trash/$xform
      $debug " moved $d/$file to trash/$xform"
      highest=$xform
      $debug " set highest number to :$highest:"
    fi
  done
done

echo
echo " Final   state:"
ls -R $DIRS trash

echo
count

exit 0

It does not over-write extant files in trash, it fills in the holes in the numeric sequence of files in trash, and it prints a count before and after so that you can see the "conservation of files" ... cheers, drl
["Sure, I can help you with that." -- USBank voice recognition system.
( Mn, 2.6.x, AMD-64 3000+, ASUS A8V Deluxe, 3 GB, SATA + IDE, NV34 )
Debian Wiki | Packages | Backports | Netinstall
User avatar
drl
 
Posts: 434
Joined: 2006-09-20 02:27
Location: Saint Paul, Minnesota, USA

Postby Blyiss » 2007-02-25 01:31

Thank you, guys, you are great people. Both scrips, dri's final reduction and Grifter's, are excellent.
I didn't have enough of emails and I made just empty files in metacity that obviously uses gedit as its default editor that leaves hidden backup files with the "~" ending. They are not seen in metacity and gnome-commander which I usually use, and I completely forgot about this gedit behavior. That's why there appeared errors. But now everything is great! Would you mind if at some point I will publish (though they are already published, but anyway) your scripts at my web-site so that other people could use them, since nothing like what you did is available for MH?
Can you also help me to make your script automatic so that I set and forget about it? I have a script from wiki for getmail to check mails every 30 sec. How to include your scrips into it?
Code: Select all
while sleep $SLEEP; do
        wait # Wait for previous fetch to complete

        cmd="getmail -d"
        for file in `find ~/.getmail -regex '.*/getmailrc-[a-zA-Z0-9-]*$'`; do
                 cmd="$cmd"" --rcfile ""$file"
        done
        echo $cmd
        $cmd &
        echo "Sleeping for $SLEEP seconds"
done
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Postby Grifter » 2007-02-25 02:22

go ahead and publish it on your site if you wish

I think it's generally a bad idea to make a script run indefinitely with a sleep, it's better to set the scheduler (cron) to run something, you can set per-user schedules with crontab -e, but I'm no cron master so you'll have to study the man page and docs yourself (:

for that matter, check out the docs (/usr/share/doc/programname) for getmail, maybe there's some examples on how to automate it there
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby Blyiss » 2007-02-25 02:38

Grifter wrote:you'll have to study the man page and docs yourself (:

Thank you anyway. I have already downloaded a free bash manual. I will start studying as soon as get some spare time.
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Postby Grifter » 2007-02-25 02:42

you already have the bash manual though,

man bash

(:

a tip however, the bash manual is gigantic, and at a first glance it's really tricky to find what you want, but it's formatted in a very specific way and in a specific order, so before you can learn bash by reading the manual, you have to learn to read the manual properly (if you want to get anything more than tidbits out of it) (:

but for your immediate need, just do "man crontab"
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby Blyiss » 2007-02-25 03:02

That's true. The manual is pretty huge. And there are a lot of other interesting languages to study. I feel overwhelmed. It will probably take decades to finish all that. But anyway, thanks a lot.
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Postby Grifter » 2007-02-25 03:14

if you want some place to begin to get to know shellscripting, read the scripts in /etc/init.d/, that's how i learned
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby Blyiss » 2007-02-25 04:25

Grifter wrote:if you want some place to begin to get to know shellscripting, read the scripts in /etc/init.d/, that's how i learned

Thank you for the prompt, but they look like a bunch of gibberish. Hope not for ever. Thanks again.
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Postby hcgtv » 2007-02-25 04:46

Here's a well written Bash tutorial: http://personal.riverusers.com/~thegren ... .2.tar.bz2
Bert Garcia - When all you have is a keyboard
User avatar
hcgtv
 
Posts: 518
Joined: 2006-11-17 23:03
Location: Charlotte, NC

Postby Blyiss » 2007-02-25 04:54

hcgtv wrote:Here's a well written Bash tutorial: http://personal.riverusers.com/~thegren ... .2.tar.bz2

Thank you, hcgtv, I have this manual only in PDF. It contains 695 pages.
Blyiss
 
Posts: 585
Joined: 2007-02-10 19:47
Location: Yakutia

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable