シェルから文字列を連結する

シェルから文字列を連結する

config.iniファイルがあります。

repo_path=ssh://git@localhost:10022/root/
dir_path=/home/vagrant/workspace/

そして、そのファイルからエクスポートして接続するための「script.sh」は次のとおりです。

while read -r line; do export $line; done <config.ini
repo_name=$1
repo_path=$repo_path$repo_name'.git'
dir_path=$dir_path/$repo_name
echo $repo_path
echo $dir_path

したがって、スクリプトを実行するとき:

./script.sh sample

出力:

sample.gitlocalhost:10022/root/
/sampleagrant/workspace/

予想出力:

ssh://git@localhost:10022/root/sample.git
/home/vagrant/workspace/sample

ベストアンサー1

合理的な説明は、データにキャリッジリターンが含まれていることです。

sample.gitlocalhost:10022/root/
^^^^^^^^^^

つまり、文字列は次のようになります(C言語文字列リテラル表記を使用)。

"ssh://git@localhost:10022/root/\rsample.git"

注は\rキャリッジリターンを意味します。

これを端末に送信すると、キャリッジリターンはカーソルを行の先頭に移動してプレフィックスをsample.git上書きします。ssh://...

これらの「不思議な出力」問題をデバッグするには、コマンド出力をバイナリダンプユーティリティにパイプすることができますod。たとえば、次のようになります。

echo $strange | od -t c  # see characters with backslash notation, or -t x1 for hex

おすすめ記事