And*_*eas 0 git bash shell function
我有一个关于我正在编写的shell脚本的bash函数的问题.功能如下:
do_command() {
if [[ $DRY_RUN ]]; then
echo $@
else
$@
fi
}
Run Code Online (Sandbox Code Playgroud)
功能很简单,如果设置了DRY_RUN标志,我们只需打印方法,否则执行.这适用于大多数命令,除了git tag命令,我尝试过不同版本的:
do_command git tag -a $NEW_VERSION -m '$INPUT_COMMENT'
Run Code Online (Sandbox Code Playgroud)
这实际上执行了tag命令,但是给出了注释$ INPUT_COMMENT
我已经尝试了2个其他版本,它们提供了正确的echo输出,但是不允许我执行git tag命令.
do_command git tag -a $NEW_VERSION -m "$INPUT_COMMENT"
Run Code Online (Sandbox Code Playgroud)
和
do_command git tag -a $NEW_VERSION -m "\"$INPUT_COMMENT\""
Run Code Online (Sandbox Code Playgroud)
有没有办法让echo和git命令在这个调用中工作?或者我需要解析do_command版本?
"$@"与引号一起使用以正确处理具有空格的参数.如果你只是写,$@那么git当$INPUT_COMMENT包含空格时命令将不起作用.
do_command() {
if [[ $DRY_RUN ]]; then
echo "$@"
else
"$@"
fi
}
Run Code Online (Sandbox Code Playgroud)
用法:
do_command git tag -a $NEW_VERSION -m "$INPUT_COMMENT"
Run Code Online (Sandbox Code Playgroud)