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

Det
  • 3
  • 1

1 Answers1

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 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. – Det Oct 04 '19 at 00:45