如何处理包含“;”的变量?
我有一个配置文件,其中包含诸如“hallo;welt;”之类的行我想对此文件执行 grep 操作。 每当我尝试诸如 grep "$1;$2" my.config
或 echo "$1;$2
of even line="$1;$2" 之类的操作时,我的脚本都会失败,并显示以下内容:
:未找到命令95:第 155 行:=hallo...
我怎样才能告诉 bash 忽略 ;在评估“...”块时?
编辑:我的代码的示例。
# find entry
$line=$(grep "$1;$2;" $PERMISSIONSFILE)
# splitt line
reads=$(echo $line | cut -d';' -f3)
writes=$(echo $line | cut -d';' -f4)
admins=$(echo $line | cut -d';' -f5)
# do some stuff on the permissions
# replace old line with new line
nline="$1;$2;$reads;$writes;$admins"
sed -i "s/$line/$nline/g" $TEMPPERM
我的脚本应该这样调用: sh script "table" "ab*.>"
编辑:另一个更简单的示例
$test=$(grep "$1;$2;" temp.authorization.config)
临时文件:
table;pattern;read;write;stuff
调用 sh test.sh 表模式导致: : command not findtable;pattern;read;write;stuff
I have a configuration file that contains lines like "hallo;welt;" and i want to do a grep on this file.
Whenever i try something like grep "$1;$2" my.config
or echo "$1;$2
of even line="$1;$2" my script fails with something like:
: command not found95: line 155: =hallo...
How can i tell bash to ignore ; while evaluating "..." blocks?
EDIT: an example of my code.
# find entry
$line=$(grep "$1;$2;" $PERMISSIONSFILE)
# splitt line
reads=$(echo $line | cut -d';' -f3)
writes=$(echo $line | cut -d';' -f4)
admins=$(echo $line | cut -d';' -f5)
# do some stuff on the permissions
# replace old line with new line
nline="$1;$2;$reads;$writes;$admins"
sed -i "s/$line/$nline/g" $TEMPPERM
my script should be called like this: sh script "table" "a.b.*.>"
EDIT: another, simpler example
$test=$(grep "$1;$2;" temp.authorization.config)
the temp file:
table;pattern;read;write;stuff
the call sh test.sh table pattern results in: : command not foundtable;pattern;read;write;stuff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要在 bash 中的赋值左侧使用
$
——如果这样做,它将替换变量的当前值而不是赋值给它。也就是说,使用:而不是:
编辑:此外,变量扩展应该用双引号引起来,除非有充分的理由。例如,使用:
代替:
这对于分号来说并不重要,但对于空格、通配符和其他一些东西来说很重要。
Don't use
$
on the left side of an assignment in bash -- if you do it'll substitute the current value of the variable rather than assigning to it. That is, use:instead of:
Edit: also, variable expansions should be in double-quotes unless there's a good reason otherwise. For example, use:
instead of:
This doesn't matter for semicolons, but does matter for spaces, wildcards, and a few other things.
引号内的
;
对于 bash 来说根本没有任何意义。但是,如果 $1 本身包含双引号,那么您最终将被 bash 解析为两个单独的命令:
Show 请准确显示您的脚本在发生错误的地方正在做什么,以及您的数据重新喂进去。
A
;
inside quotes has no meaning at all for bash. However, if $1 contains a doublequote itself, then you'll end up withwhich'll be parsed by bash as two separate commands:
Show please show exactly what your script is doing around the spot the error is occurring, and what data you're feeding into it.
反例:
你还没有准确分类你的问题。
请表明你的意思。使用与之前相同的数据文件并进行分配,这是我得到的输出:
似乎对我有用。它还说明了为什么这个问题需要一个明确的、完整的、可执行的、最小的例子,这样我们就可以看到提问者正在做的事情与回答问题的人认为正在发生的事情不同。
我看到您提供了一些示例代码:
$line=$(grep ...)
行是错误的。您应该省略line
之前的$
。尽管它在语法上是正确的,但它的意思是“将grep
命令的结果分配给名称存储在$line
中的变量”。这不太可能是你的想法。有时它是有用的。然而,这种情况很少见,而且只适合那些知道自己在做什么并且能够准确记录自己在做什么的人。为了安全起见,如果不出意外,我还会将
echo
行中的$line
值括在双引号中。这可能不是严格必要的,但它是简单的保护性编程。这些更改导致:
脚本的其余部分很好。
Counter-example:
You have not yet classified your problem accurately.
Please show what you mean. Using the same data file as before and doing an assignment, this is the output I get:
Seems to work for me. It also demonstrates why the question needs an explicit, complete, executable, minimal example so that we can see what the questioner is doing that is different from what people answering the question think is happening.
I see you've provided some sample code:
The line
$line=$(grep ...)
is wrong. You should omit the$
beforeline
. Although it is syntactically correct, it means 'assign to the variable whose name is stored in$line
the result of thegrep
command'. That is unlikely to be what you had in mind. It is, occasionally, useful. However, those occasions are few and far between, and only for people who know what they're doing and who can document accurately what they're doing.For safety if nothing else, I would also enclose the
$line
values in double quotes in theecho
lines. It may not strictly be necessary, but it is simple protective programming.The changes lead to:
The rest of your script was fine.
您似乎正在尝试读取以分号分隔的文件,识别以“table;pattern;”开头的行其中 table 是您指定的字符串,pettern 是 grep 能够理解的正则表达式。识别该行后,您希望用不同的数据替换第三、第四和第五字段,并将更新后的行写回到文件中。
这听起来正确吗?
如果是这样,请尝试我选择在此处按行号更新的代码
,以避免替换左侧出现未知字符的问题。
It seems like you are trying to read a semicolon-delimited file, identify a line starting with 'table;pattern;' where table is a string you specify and pettern is a regular expression
grep
will understand. Once the line is identified you wish to replaced the 3rd, 4th and 5th fields with different data and write the updated line back to the file.Does this sound correct?
If so, try this code
I chose to update by line number here to avoid problems of unknown characters in the left hand of the substitution.