私のテキストを解析すると、Catは文字列を返す出力の代わりに「コマンドが見つかりません」を返します。

私のテキストを解析すると、Catは文字列を返す出力の代わりに「コマンドが見つかりません」を返します。

これは私のコマンドです:

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

私の端末にcat返品が表示されます。

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

catファイルテキスト値をtextVariableに挿入するにはどうすればよいですか?

ベストアンサー1

set -xスクリプトを使用またはデバッグすると、次のようにbash -x印刷されます。

+ 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

評価後

[ -f "$mail_recipient_location" ]

Quasimodoがすでに述べたように、これはあなたを拡張してcat "$mail_recipient_location"無視します。したがって、実行したいコマンドではないようですtextVariable=the mail's adress's value

必要なものを達成するには、次のものを使用できます。 (また、以下を避けるべきです。ウルムチ大学):

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

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

非POSIX、次に適用bash可能zsh

おすすめ記事