I have a following problem:

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

"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?

  • 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
  • 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

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

cat accounts | while read account 
    echo -e "\n Account: $account \n";
    echo sudo ./backup_maildir  "$account"; 

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

| improve this answer | |
  • 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
  • 3
    then dos2unix it :) – Tom O'Connor Feb 24 '11 at 15:29
  • 1
    UUoC urgh; while read -r a; do echo "Account: $a"; sudo /complete/path/to/backup_maildr "$a"; done < accounts – adaptr Apr 2 '12 at 13:28

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.

| improve this answer | |
  • 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:<br> + 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

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


man sudoers

and search there for default and parameters.

| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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