binにファイルを挿入するPerlスクリプト

binにファイルを挿入するPerlスクリプト

スクリプトがあります~/bin/script

$ cat ~/bin/script
#!/bin/bash

perl -pe 's/loremipsum/`cat ~/foo/bar/file.txt`/ge' -i ~/path/to/target.txt

スクリプトは、loremipsuminの各インスタンスをのコンテンツに置き換える必要があります。ただし、返品発行target.txtfile.txtscript

Backticks found where operator expected at -e line 1, at end of line
    (Missing semicolon on previous line?)
Can't find string terminator "`" anywhere before EOF at -e line 1.

私のスクリプトに問題がありますか?

ベストアンサー1

バックティック結果をスクリプト変数に保存し、Perl呼び出しに使用する必要があります。

REPL = `cat ~/foo/bar/file.txt`
perl -pe "s/loremipsum/$REPL/ge" -i ~/path/to/target.txt

しかし、foo/bar/file.txtの内容がコマンドを破損する可能性があるので、Perlスクリプトを使用して文字列をファイルの内容に置き換える方が良いと思います。

おすすめ記事