在bash shell中打印星号("*") [英] Printing asterisk ("*") in bash shell

查看:725
本文介绍了在bash shell中打印星号("*")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a=5
echo "*/$aMin * * * * bash /etc/init.d/ckDskCheck.sh"

当我尝试运行以下代码时,它会正确显示

When I try to run the following code, it displays properly

*/5 * * * * bash /etc/init.d/ckDskCheck.sh

但是当我尝试使用以下代码将结果分配给变量并将其打印出来时,它将显示为:

But when I try to assign the result using the following code to the variable and print it out, it displays as this:

a=5
cronSen=`echo "*/$a * * * * bash /etc/init.d/ckDskCheck.sh"`
echo $cronSen

结果:

所以我尝试通过

cronSen=`echo "\*/$a \* \* \* \* bash /etc/init.d/ckDskCheck.sh"`

但是它仍然不起作用.为什么?我该如何解决?

But it still doesn't work. Why? How can I fix this?

推荐答案

您有两个问题:

  1. 反引号中无用的回声

  1. Useless Use of Echo in Backticks

总是引用您的echo

所以固定代码是

a=5
cronSen="*/$a * * * * bash /etc/init.d/ckDskCheck.sh"
echo "$cronSen"

似乎您也可能对变量无用,但是cronSen在更大的上下文中很有用.

It appears you may also have a Useless Use of Variable, but perhaps cronSen is useful in a larger context.

简而言之,请在不需要外壳执行令牌拆分和通配符扩展的所有地方加上引号.

In short, quote everything where you do not require the shell to perform token splitting and wildcard expansion.

令牌拆分;

 words="foo bar baz"
 for word in $words; do
      :

(这将循环3次.引用$words只会在文字标记foo bar baz上循环一次.)

(This loops three times. Quoting $words would only loop once over the literal token foo bar baz.)

通配符扩展:

pattern='file*.txt'
ls $pattern

(引用$pattern会尝试列出名称实际上是file*.txt的单个文件.)

(Quoting $pattern would attempt to list a single file whose name is literally file*.txt.)

更具体地讲,凡是包含文件名的内容都应加引号.

In more concrete terms, anything containing a filename should usually be quoted.

包含一个要循环遍历的令牌列表或一个通配符进行扩展的变量的情况很少见,因此我们有时缩写为引用所有内容,除非您确切地知道自己在做什么".

A variable containing a list of tokens to loop over or a wildcard to expand is less frequently seen, so we sometimes abbreviate to "quote everything unless you know precisely what you are doing".

这篇关于在bash shell中打印星号("*")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆