![次のgit + sed関数に正確に一致フラグを追加するにはどうすればよいですか?](https://linux33.com/image/228926/%E6%AC%A1%E3%81%AEgit%20%2B%20sed%E9%96%A2%E6%95%B0%E3%81%AB%E6%AD%A3%E7%A2%BA%E3%81%AB%E4%B8%80%E8%87%B4%E3%83%95%E3%83%A9%E3%82%B0%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%EF%BC%9F.png)
を無視しながらsedの検索と置換を実行できるbash機能を作成したいと思います.gitignore
。また、コマンドにgit grepフラグを追加できるようにしたいです。たとえば、正確な一致のために-w
。
これが私が今まで持っているものです:
gs {
local grep_options=()
local search_pattern
local replacement
while [[ $1 =~ ^- ]]; do
grep_options+=("$1")
shift
done
search_pattern=$1
replacement=$2
git grep -l "${grep_options[@]}" "$search_pattern" | xargs sed -i "s/$search_pattern/$replacement/g"
}
gs pattern replacement
検索と置換が成功します。しかし、gs -w pattern replacement
何も起こらないでしょう。
なぜそんなことですか?どうやって解決しますか?
答え1
関数宣言に構文エラーがあります。
変える
w { ... }
以下を行う必要があります。
w() { ... }
必ずスクリプトを次に渡してください。https://shellcheck.net助けを求める前に:
$ shellcheck file
In file line 1:
gs {
^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
^-- SC1083 (warning): This { is literal. Check expression (missing ;/\n?) or quote it.
In file line 2:
local grep_options=()
^---^ SC2168 (error): 'local' is only valid in functions.
In file line 3:
local search_pattern
^---^ SC2168 (error): 'local' is only valid in functions.
In file line 4:
local replacement
^---^ SC2168 (error): 'local' is only valid in functions.
In file line 15:
}
^-- SC1089 (error): Parsing stopped here. Is this keyword correctly matched up?
For more information:
https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y...
https://www.shellcheck.net/wiki/SC2168 -- 'local' is only valid in functions.
https://www.shellcheck.net/wiki/SC1083 -- This { is literal. Check expressi...