私はこの(テスト)スクリプトを持っています:
#!/bin/bash
my_cmd_bad_ ( ) {
cmd="$@"
$cmd
}
my_cmd_good_ ( ) {
"$@"
}
my_cmd_bad_ ls -l "file with space"
my_cmd_good_ ls -l "file with space"
出力は次のとおりです(ファイルは存在しません。これはこの質問のポイントではありません):
» ~/test.sh
ls: cannot access file: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access space: No such file or directory
ls: cannot access file with space: No such file or directory
最初のバージョンが期待どおりに機能しないことに驚いています。パラメーターは引用符で囲まれておらず、1 つのファイルを処理する代わりに 3 つのファイルを処理しています。なんで?
実行したいコマンドを適切に引用して保存するにはどうすればよいですか? 後で実行する必要がありますが、もうあり"$@"
ません。
このテスト スクリプトを簡単に書き直していただければ幸いです。