git - 通过 xargs util 将配置参数传递给 git 命令
问题描述
我正在尝试构建一个xargs
用于传递配置参数的命令:user.name
和user.email
to git commit
。
要构建的命令xargs
:
git -c user.name=abc -c user.email=abc@mail.com commit
我尝试过的:
echo "-c user.name=abc -c user.email=abc@mail.com" | xargs -I % git % commit
但是,git
返回这个:
未知选项:-c user.name=abc -c user.email=abc@mail.com
即使xargs
冗长,该命令也可以正常工作。
echo "-c user.name=abc -c user.email=abc@mail.com" | xargs -tI % git % commit
这将打印要执行的命令,该命令git -c user.name=abc -c user.email=abc@mail.com commit
在复制粘贴到终端时起作用。
请注意,配置参数由空格分隔。
解决方案
根据评论,xargs
正在用单个参数替换%
单个参数-c user.name=abc -c user.email=abc@mail.com
;结果命令
git -c user.name=abc -c user.email=abc@mail.com commit
有两个参数,第一个是-c user.name=abc -c user.email=abc@mail.com
,显然是一个无效的选项。
我能想到的最便携的解决方法是让 shell 重新解释该行:
echo "-c user.name=abc -c user.email=abc@mail.com" | xargs -I % bash -c "git % commit"
这样,xargs
将bash
使用两个参数执行:-c
和git -c user.name=abc -c user.email=abc@mail.com commit
。bash -c command
执行该命令,其中包括bash
通常执行的完整命令行解析。这将导致使用五个参数bash
执行: 、、和。git
-c
user.name=abc
-c
user.email=abc@mail.com
commit
推荐阅读
- css - 使用选择器在 2 个全局 SCSS 之间切换
- javascript - 如何在 Select2 下拉列表中捕获滚动事件?
- etcd - 放置一些值时etcd“不是目录”的结果
- docker - 有没有办法在 Google 的 AI 平台中使用外部编译包进行数据处理?
- java - 使用java 8查找列表中不存在于另一个列表中的元素
- javascript - Javascript:字符串化不适用于对象数组
- sql-server - MSSQL:如何从不同的表中删除重复的列值?
- java - 为什么在 java 中递归实现合并排序时会发生这个 Stackoverflow 异常?
- c++ - 关于使用类型转换优化除法代码的问题
- ansible - 设置 Sensu-Go 集群 - 集群未同步