小编use*_*603的帖子

将参数传递给bash中if语句中的函数

我试图将参数传递给if语句中的函数,然后评估函数返回的内容(在bash中).我得到的结果是不正确的.你怎么做到这一点?

#!/bin/bash

foo() {
    if [ $1 = "zero" ]; then
        echo "0"
    else 
        echo "1"
fi
}

arg="zero"
if foo $arg -eq 0 ; then
    echo "entered 0"
else
    echo "entered something else"
fi

arg="blah"
if foo $arg -eq 0 ; then
    echo "entered 0"
else
    echo "entered something else"
fi
Run Code Online (Sandbox Code Playgroud)

不是预期的结果:

cknight@macbook:~/work$ ./test.sh
0
entered 0
1
entered 0
Run Code Online (Sandbox Code Playgroud)

先感谢您.〜克里斯

unix bash shell

6
推荐指数
1
解决办法
1万
查看次数

使用getopts的多个选项参数(bash)

我试图在bash中使用getopts处理命令行参数.其中一个要求是处理任意数量的选项参数(不使用引号).

第一个例子(只抓住第一个参数)

madcap:~/projects$ ./getoptz.sh -s a b c
-s was triggered
Argument: a
Run Code Online (Sandbox Code Playgroud)

第二个例子(我希望它表现得像这样但不需要引用参数"

madcap:~/projects$ ./getoptz.sh -s "a b c"
-s was triggered
Argument: a b c
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

这是我现在的代码:

#!/bin/bash
while getopts ":s:" opt; do
    case $opt in
    s) echo "-s was triggered" >&2
       args="$OPTARG"
       echo "Argument: $args"
       ;;
   \?) echo "Invalid option: -$OPTARG" >&2
       ;;
    :) echo "Option -$OPTARG requires an argument." >&2
       exit 1
       ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

unix linux bash shell getopts

6
推荐指数
3
解决办法
2万
查看次数

使用bash将行中的字符串分配给变量

我有以下文本文件,我需要比较每行的值,即项目2-4与项目5-7.我坚持使用bash/awk/sed.

样本数据:

[hartford tmp]$ cat flist
a1 1 2 3 x y z
b1 3 2 1 z y x
c1 1 2 3 1 2 3
d1 4 5 6 6 5 4
e1 a b c a b c
f1 x y z x y z
Run Code Online (Sandbox Code Playgroud)

它适用于以下脚本,但它只是无法忍受的缓慢,可能是因为所有的echos.

[hartford tmp]$ cat pdelta.sh
#!/bin/bash

cat flist |while read rec; do
    f1="$(echo $rec | awk '{ print $1 }')"
    f2="$(echo $rec | awk '{ print $2 }')"
    f3="$(echo $rec …
Run Code Online (Sandbox Code Playgroud)

bash awk sed

4
推荐指数
2
解决办法
1702
查看次数

新手Python/Regex:使用正则表达式在<a>标签之间拉取字符串

需要使用re模块在Python中的href属性标记之间拉取字符串.

我尝试了很多模式,例如:

patFinderLink = re.compile('\>"(CVE.*)"\<\/a>')
Run Code Online (Sandbox Code Playgroud)

示例:我需要从以下标签中拉出标签之间的内容(在本例中为" CVE-2010-3718 "):

<pre>
<a href="https://www.redhat.com/security/data/cve/CVE-2010-3718.html">CVE-2010-3718</a>
</pre>
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?任何意见是极大的赞赏.先感谢您.

太阳

python regex beautifulsoup

1
推荐指数
1
解决办法
2131
查看次数

使用变量访问哈希值

如何使用变量访问哈希值?这是我的不起作用:

DHASH = { 0 => -0.8,
          1 => -0.54,
          2 => -0.32,
          3 => -0.14,
          4 => 0,
          5 => 0.1 }

print "Enter number "
num = gets.chomp()
puts "Value: DHASH[#{num}]"
Run Code Online (Sandbox Code Playgroud)

输出:

Enter number 2
Value: DHASH[2]
Run Code Online (Sandbox Code Playgroud)

我想要的是:

Enter number 2
Value: -0.32
Run Code Online (Sandbox Code Playgroud)

ruby string-interpolation

0
推荐指数
1
解决办法
308
查看次数

标签 统计

bash ×3

shell ×2

unix ×2

awk ×1

beautifulsoup ×1

getopts ×1

linux ×1

python ×1

regex ×1

ruby ×1

sed ×1

string-interpolation ×1