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' ' -f2gets 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.
#!/bin/bash -xat the top so you can view a verbose output./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, sinceecho "message" | mail ...would work without needing a file. If you try the-xas 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.