2

I have a following problem:

#!/bin/bash
NUM=`cat accounts | wc -l`;
for i in {1..$NUM}
do
   account=`awk "NR==$i" accounts`;
    echo -e "\nAccount: $account\n";
  sudo ./backup_maildir $account;
done

"accounts" is a file with regular e-mail addresses, one in each line.
backup_maildir is expect script.

When the main script is executed, the 6th line successfuly echo current mail address, but the following line is not passing that string to backup_maildir script. If the $account variable is user@domain.com, a string that is passed to backup_maildir is {user@domain.com?} ?! How is that possible? How to solve it?

rems
  • 2,260
  • 13
  • 11
Boban P.
  • 705
  • 1
  • 6
  • 20
  • 1
    Your problem is that bash doesn't support the use of variables in ranges so the `for i in {1..$NUM}` does not work as you expect. If you simply echo $i you get `{1..10}` for NUM=10 – user9517 Feb 24 '11 at 12:18
  • I've tried to put echo $i below the "do" line, and it works well, it prints the number of current iteration. – Boban P. Feb 24 '11 at 12:50
  • 1
    You must have a different bash to me then. Are you sure your /bin/bash is really bash ? – user9517 Feb 24 '11 at 13:01
  • 1
    Yeah, that's really weird. Bash doesn't normally work like that — brace expansion happens _before_ $ expansion, so normally you'd get what Iain says in the comment above. – mattdm Feb 24 '11 at 18:38
  • 1
    I just tested this in half a dozen versions of bash, and it acts the way Iain and I expect. Something doesn't add up here. – mattdm Feb 24 '11 at 19:13
  • 1
    Sorry. my mistake. I've put for actual numberin i in {1..10}, so it worked well. – Boban P. Feb 25 '11 at 10:49

3 Answers3

10

huh what a crazy script :) lets try like this :)

#!/bin/bash 
cat accounts | while read account 
do
    echo -e "\n Account: $account \n";
    echo sudo ./backup_maildir  "$account"; 
done

if everything looks fine and sudo line works out of script as expected drop the echo and voila

Hrvoje Špoljar
  • 5,245
  • 26
  • 42
  • 1
    Good point from Hrvoje, this is a way simpler way of accomplishing the same thing. This may not solve your problem with the sudo line, but it's a much cleaner script to work on debuging! – Caleb Feb 24 '11 at 12:09
  • I've tried this, but results are exactly the same. The problem still exist. When I manually run "sudo ./backup_maildir user@domain.com" it works as it should. – Boban P. Feb 24 '11 at 12:48
  • what does output of script say ? , also put $account in "" like this - sudo ./backup_maildir "$account"; --- also if you could edit your question and add sample of what is in the accounts file ; like output of command -- head accounts -- – Hrvoje Špoljar Feb 24 '11 at 12:54
  • Also what may be a problem is origin of accounts file it's it's DOS file with \r for new line.... if thats the case then convert your accounts file like this command: tr -d '\r' < accounts > accounts.2 – Hrvoje Špoljar Feb 24 '11 at 13:02
  • admin@mail2:~$ head accounts aleksandar.komanovic@domain.com azra011@domain.com belux@domain.com beravs@domain.com biljanates@domain.com boban.petrovic@domain.com bojan.loncar@domain.com bo&my@domain.com branislav.nikolic@domain.com cecen@domain.com – Boban P. Feb 24 '11 at 13:03
  • tr -d '\r' < accounts > accounts.2 ----- problem SOLVED :) thank you very much ! – Boban P. Feb 24 '11 at 13:05
  • yea dos file :/ you dont see it but it messes up the end of line... – Hrvoje Špoljar Feb 24 '11 at 13:06
  • 3
    then `dos2unix` it :) – Tom O'Connor Feb 24 '11 at 15:29
  • a note: it's actually "fromdos" command in ubuntu... – Boban P. Feb 25 '11 at 07:07
  • 1
    UUoC urgh; while read -r a; do echo "Account: $a"; sudo /complete/path/to/backup_maildr "$a"; done < accounts – adaptr Apr 02 '12 at 13:28
1

You can add a line like set -x in your script near the top or just before your sudo command to see a lot more information verbose information on the console about what variables look like and what commands are getting run. I suspect your variable IS getting passed, you just aren't handling it right.

You may also try quoting the var in the line like this: "$account" in case it has whitepace or other things that are unexpected, but if that was the case the real solution would be to clean up your data.

Caleb
  • 11,813
  • 4
  • 36
  • 49
  • quoting the var in the line did not solve the problem. – Boban P. Feb 24 '11 at 12:56
  • When I put the sudo line between set -x and set + x, I see:
    + sudo ./backup_maildir $'user@domain.com\r' spawn maildirsync -rvvvv --rsh ssh -l root mail.domain.com:/srv/vmail/{z}.user@domain.comr@domain.com ^C+ set +x
    – Boban P. Feb 24 '11 at 12:57
  • maildirsync is called in backup_maildir script – Boban P. Feb 24 '11 at 13:00
  • 1
    It looks like you have a newline (actually a line return) in your variable value. Since you didn't quote it, it might break your current script, and if you quote and it and pass it on it's likly to wreak havoc on your expect script. I suggest you use the code suggested by Hrvoje to more cleanly handle your data. It might be a good idea to clean up line endings in the input file. `cat accounts | dos2unix | while read account; do....` – Caleb Feb 24 '11 at 13:05
0

Your problem may be the sudo configuration in /etc/sudoers . for passing parameters you may need to configure sudo to allow it.

Read

man sudoers

and search there for default and parameters.

rems
  • 2,260
  • 13
  • 11
  • sudo ./backup_maildir user@domain.com works well when I run it manualy, so it's not sudoers problem. – Boban P. Feb 24 '11 at 12:51