Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
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 –  Iain 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 ? –  Iain 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  
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 Answers 3

up vote 9 down vote accepted

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

share|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.

share|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
    
Thanks, that's good point! –  Boban P. Feb 24 '11 at 13:07

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.

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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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