【问题标题】:read multi word text from command line从命令行读取多字文本
【发布时间】:2013-06-04 13:12:30
【问题描述】:

当我编写这样的 shell 脚本时:

echo -n 'Enter description of new version: ';

read desc;

git commit -m $desc;

当我输入多词描述时,它只需要一个词进入 $desc 并给我错误:

Enter description of new version: hope it works
error: pathspec 'it' did not match any file(s) known to git.
error: pathspec 'works'' did not match any file(s) known to git.
fatal: too many params

有时它会给出这样的:

Enter description of new version: final check
error: pathspec 'check'' did not match any file(s) known to git.
fatal: Failed to resolve 'check'' as a valid ref.
Everything up-to-date

我的脚本有什么问题?

请提出从命令行读取多字描述到变量$desc的原因和解决方案

我尝试过使用:

echo -n 'Enter description of new version: ';

read text;

desc="'"$text"'";

git commit -m $desc;

但没用。

提前谢谢你

【问题讨论】:

  • 我尝试过使用git commit -m '$desc'git commit -m '"'$desc'"' 但它不起作用,为什么?

标签: git shell command-line stdin


【解决方案1】:

你需要引用:

git commit -m "$desc"

区别在于:

git commit -m hope it works

git commit -m "hope it works"

第一个尝试使用消息hope 提交文件itworks,而后者使用消息hope it works 提交索引。

【讨论】:

  • 是的,为此目的,我使用了单引号,即` git commit -m '$desc' ` 我也尝试使用git commit -m '"'$desc'"' 但它没有用,为什么?
  • 参数不在单引号内展开。在第二种情况下,您没有引用 $desc,因此它仍然会在 shell 甚至查看两个文字双引号之前扩展为单独的单词(它们分别由单引号引用)。
猜你喜欢
  • 1970-01-01
  • 2018-04-26
  • 2016-05-09
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多