0

I am trying to loop inside a text file containing commands to be executed:

    while IFS= read -r line
    do
      $line
    done < msmtp-cmds.txt

The commands inside the text file are:

  msmtp -t < message1.txt
  msmtp -t < message2.txt

After command substitutions the redirection sign seems to be ignored, as msmtp is trying to use message1.txt as a recipient, but I cannot figure why

1 Answer 1

1

< is not an argument to the command; it's shell syntax that is parsed before parameter expansion. If your intent is to execute arbitrary code read from a file, that's what eval is for.

while IFS= read -r line; do
  eval "$line"
done < msmtp-cmds.txt

However, at that point, you may as well just source the file rather than reading it line by line:

. smtp-cmds.txt
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your suggestion about using eval solves my problem. As for just sourcing the file, I will have to write a simple logic based on the result of each msmtp command: if the mail messages are not sent for any reason, the script must be terminated.

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.