私のbashスクリプトは、スペースやその他の奇妙な文字でファイル名を取得します。ファイル名にエスケープ文字をスペースで置き換えてbashスクリプトで端末に出力
これらのファイル名を正しい場所にエスケープしてターミナルに戻すにはどうすればいいですか?同じまたは別のスクリプトのパラメータとして再利用できるように、ファイル名をコピー&ペーストするだけです。
私は次のように読み込み、テストスクリプトを書きました:次のように
#! /bin/bash
echo ""
echo "testing the insertion of '\' in filenames having spaces"
echo "the parameter you gave was: '$1'"
echo "when printed directly the filename looks like: '$1'"
echo "when printed with echo \$(printf '%q' $x) it looks like: " $(printf '%q' $1)
は、スクリプトを実行するには、行ってきました:私は、スクリプトの農産物を見たいと思って何
bash test.sh this\ filename\ has\ spaces
testing the insertion of '\' in filenames having spaces
the parameter you gave was: 'this filename has spaces'
when printed directly the filename looks like: 'this filename has spaces'
when printed with echo $(printf '%q') it looks like: thisfilenamehasspaces
は次のとおりです。
when printed directly the filename looks like: 'this\ filename\ has\ spaces'
シンプルだと思われますが、Googleの問題は難しいです。すべての助けに感謝します。ありがとう。
パラメータをprintfに提示するときは、パラメータを二重引用符で囲む必要があります。 – potong
私は出力にエスケープ文字を挿入しようとしているので、ユーザがbashシェルのコマンドラインに出力をカットアンドペーストできるようにしています。 – rfreytag