问题描述
下面是我试图运行脚本
#!/bin/bash
charlist=$1 #possible input:cat
wordlength=$2 #possible input: 3
cat ./nounlist | egrep \b[${charlist}]\b{${wordlength}}
输出:无#应该抓到以下(使用命令行)
output: nothing.# should've caught the following(using command line)
$ cat nounlist | egrep "\b[cat]{3}\b"
act
cat's_cradle
cat
cat-o'-nine-tails
cat's-paw
Sno-cat
act
act
act
act
rat-a-tat-tat
cat
cat's-claw
cat's_foot
cat's-ear
cat's-tail
cat's-ear
cat's_eye
基本上我试图写一个脚本,需要两个参数(词库,字长),并在获取,在词表包括焦炭和具有可变字长的长度nounlist所有单词。我想知道的是如何把bash的变量的egrep的常规前pression为字符串?
basically I am trying to write a script that takes two arguments(wordlist, wordlength) and fetch all the words in the nounlist that consist of the char in the wordlist and has the length of wordlength variable. What I want to know is how to put bash variables in egrep's regular expression as strings?
谢谢!
推荐答案
您需要包装的说法在双引号,这样的Bash不会尝试处理反斜杠,并且移动第二 \\ b
来的之后的的 {$ {}字长}
:
You need to wrap the argument in double-quotes so that Bash doesn't try to handle the backslashes, and move the second \b
to after the {${wordlength}}
:
cat ./nounlist | egrep "\b[${charlist}]{${wordlength}}\b"
(如果你运行回声\\ b
,你会看到,它只是打印 B
,这是因为巴什假定 \\
是那里prevent b
的有什么特殊的意义,所以它删除。 )
(If you run echo \b
, you'll see that it just prints b
. This is because Bash assumes that the \
is there to prevent b
from having any special meaning; so it removes it.)
这篇关于在egrep的例行前pression管道bash的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!