bash shell中:for循环返回的值不同 [英] Bash Shell: for loop returns different values

查看:158
本文介绍了bash shell中:for循环返回的值不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$值中包含这是我从一个配置文件的grep的字符串。此字符串可以eiter:?,!或只是一个字符串包含像富巴字样。

$values contains a string which I grep from a config file. This string can be eiter: ?, ! or just a string containing words like "foo bar".

values="?"
for value in ${values}
do
    echo "value is ${value}"
done

如果我执行本地主机上的脚本,结果是:

If I execute the script on local host, the result is:

value is ?

不过,如果我从远程主机执行脚本(使用ssh和须藤苏 - someUser所/tmp/foo.sh),其结果是:

However, if i execute the script from a remote host (using ssh and sudo su - someuser "/tmp/foo.sh"), the result is:

value is 2

到现在为止,我没能找出什么不同。谁能帮助吗?

Until now, I was not able to find out whats different. Could anyone help please?

推荐答案

您需要引用变量,否则Bash会试着尝试在换头,扩大$值。

You need to quote variables, otherwise bash will try to expand $values in the for-header.

values="?"
for value in "${values}"
do
    echo "value is ${value}"
done

本身在bash会扩大到当前目录中的任何单个字符的文件名。例如。

? by itself in bash tries to expand to any single character file names in the current directory. e.g.

$ mkdir somedir
$ cd somedir
$ echo ?
?
$ mkdir a
$ echo ?
a 
$ touch {b..z}
$ echo ? 
a b c d e f g h i j k l m n o p q r s t u v w x y z

所以,取决于你在每个系统上的东西,猜测你有一个文件 2 在远程主机上。

这篇关于bash shell中:for循环返回的值不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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