2

Here are my commands:

mail_recipient_location="$PWD/mail_config/myFile.txt" 
textVariable= [ -f "$mail_recipient_location" ] && `cat "$mail_recipient_location"`

My terminal shows that cat returns:

{the mail's adress's value in myFile.text}: command not found

How can I lead cat to just inject the file text's value in the textVariable?

2
  • I apologize, I have modified my code to make it more readable :).
    – Webwoman
    Jan 5 '20 at 23:40
  • No apology needed in this case, later edits are to be avoided, but the sooner you do an edit, the better in general. Jan 7 '20 at 0:54
4

If you debug your script using set -x or bash -x, it will print:

+ mail_recipient_location=/somepath/mail_config/myFile.txt
+ textVariable=
+ '[' -f /somepath/mail_config/myFile.txt ']'
++ cat /somepath/mail_config/myFile.txt
+ the mail's adress's value

After it evaluates

[ -f "$mail_recipient_location" ]

it expands your cat "$mail_recipient_location" and ignores textVariable=, as already mentioned by Quasimodo. So, it tries to execute the mail's adress's value which is not a command, obviously.

To achieve what you want, you can use this: (Also, you should avoid UUOC):

# oneliner
[ -f "$mail_recipient_location" ] && textVar=$(<"$mail_recipient_location")

# or
if [ -f "$mail_recipient_location" ]; then
   textVar=$(<"$mail_recipient_location")
else
   : # do something
fi

Non-POSIX, works on bash and zsh

2
  • @roaima True, I merely wanted him/her to know about it, and maybe mention it, which indeed he/she did. Deleting comment. Jan 7 '20 at 0:33
  • @LinuxSecurityFreak Yes, I wasn't aware about that before, thanks for pointing it out.
    – annahri
    Jan 7 '20 at 1:38
4

Error is on this line:

textVariable= [ -f "$mail_recipient_location" ] && `cat "$mail_recipient_location"`

the backticks evaluate the output of cat "$mail_recipient_location", which is the mail address. That's clearly not what you want. Remove the backticks. If you only remove the backticks, still the code will not work because there is a blank space after the equal sign, which means textVariable will always be set to the empty string.

Additionaly, backticks are not recommended. The following looks like cleaner code and does what you want:

if [ -f "$mail_recipient_location" ]; then
    textVariable=$(cat "$mail_recipient_location")
fi
3
  • and if this is bash, you can write: textVariable="$(< "$mail_recipient_location")" -- ref 3.5.4 Command Substitution Jan 6 '20 at 17:19
  • and pedantically, the backticks don't "evaluate the output" -- the backticks produce the output, and it's the shell's parsing of what follows && that does the evaluation (i.e. a command is expected there). Jan 6 '20 at 17:21
  • @glennjackman At least Q has pointed out actually POSIX-ly correct code. It's a nice bonus to the nice explanation he had provided. I advise not to recommend Bashisms if not needed for the case. Jan 7 '20 at 0:52
1

You're not far off. Try this

mail_recipient_location="$PWD/mail_config/myFile.txt"
[[ -f "$mail_recipient_location" ]] && textVariable=$(cat "$mail_recipient_location")

First check the file exists. Then assign the variable.

For POSIX environments the [[ ... ]] needs to be replaced with [ ... ].

0
-1

try this

[ -f "$mail_recipient_location" ] && textVariable=`cat "$mail_recipient_location"`
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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