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 [email protected], a string that is passed to backup_maildir is {[email protected]?} ?! How is that possible? How to solve it?

6
  • 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, 2011 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, 2011 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, 2011 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, 2011 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, 2011 at 10:49

3 Answers 3

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

10
  • 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, 2011 at 12:09
  • I've tried this, but results are exactly the same. The problem still exist. When I manually run "sudo ./backup_maildir [email protected]" it works as it should.
    – Boban P.
    Feb 24, 2011 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 -- Feb 24, 2011 at 12:54
  • 3
    then dos2unix it :) Feb 24, 2011 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, 2012 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.

4
  • quoting the var in the line did not solve the problem.
    – Boban P.
    Feb 24, 2011 at 12:56
  • When I put the sudo line between set -x and set + x, I see:<br> + sudo ./backup_maildir $'[email protected]\r' spawn maildirsync -rvvvv --rsh ssh -l root mail.domain.com:/srv/vmail/{z}[email protected]@domain.com ^C+ set +x
    – Boban P.
    Feb 24, 2011 at 12:57
  • maildirsync is called in backup_maildir script
    – Boban P.
    Feb 24, 2011 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, 2011 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.

1
  • sudo ./backup_maildir [email protected] works well when I run it manualy, so it's not sudoers problem.
    – Boban P.
    Feb 24, 2011 at 12:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .