0

I'm trying to execute the following loop inside my script in order to send mail to all users in a certain file that I write in earlier in the script. I check if the file $eUsers is not empty I execute the following:

for name in `cat $eUsers | cut -d' ' -f2`
do
echo "message" > /letter
cat /letter | mail -s "message" $name
done
  • At the beginning of the script I define $eUsers= "/filename".
  • The code : cat $eUsers | cut -d' ' -f2 gets back the user name. I tried it out in the terminal, and it's working. I tried putting the above code in a separate file and replaced "$eUsers" with its path (/filename), and it worked without any problems.

However, when I use that code in my script, after writing in the file $eUsers, it goes into an infinite loop. I dont' understand why it's doing so. In the original file, the infinite loop seems to occur because of the line cat /letter | mail -s "message" $name.

2
  • 1
    try running your script with -x option #!/bin/bash -x at the top so you can view a verbose output. Commented Dec 28, 2010 at 23:12
  • 1
    There's nothing in the script as posted that should cause an infinite loop. I'm assuming that /letter (and /filename) represents a longer path since it would be unusual to put a file directly in the root directory. I'm also assuming that there's some reason that you output the message to a file, since echo "message" | mail ... would work without needing a file. If you try the -x as suggested it may show you where the problem is or if you can post more details we may be better able to help. I think it's probably some other part of the code that is actually causing the problem. Commented Dec 29, 2010 at 2:33

1 Answer 1

2
#!/bin/bash

while read junk name; do
  echo "message" | mail -s "message" "$name"
done < "$eUsers"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.