内联python脚本的命令行参数?

问题描述 投票:0回答:3

我有一组命令行工具,我实现为bash函数,例如:

function sf
{
    sftp $(svr $1)
}

其中svr是另一个将短名称转换为完全限定域名的函数。我知道我想转换这个功能:

function irpt
{
    ~/tools/icinga_report $*
}

类似于:

function irpt
{
python <<!
import requests
...lots of python stuff...
!
}

除了一件事之外,这很有效:我需要在某处添加参数,但我看不到哪里。我试图将整个python块包含在{ }中,但它不起作用:

function irpt
{
python <<!
import requests
...lots of python stuff...
!
} 

shell不接受定义:

-bash: /etc/profile: line 152: syntax error near unexpected token `$*'
-bash: /etc/profile: line 152: `} $*'

有没有办法实现这个? ===编辑===受到我接受的答案的启发,这就是我所做的,也许对其他人有用:

function irpt
{
python <<!
import requests

param="$*".split(' ')

...lots of python stuff...
!
}

这很好用。

python bash
3个回答
2
投票

单程:

function irpt
{
python <<!
import requests
v1='$1'
print(v1)
!
}

运行功能:

$ irpt hello
hello
$

2
投票

它看起来有点奇怪,但您可以使用bash <(command)语法动态提供脚本文件(实际上是命名管道);其余的如下。

function demo {
    python <(echo 'import sys; print(sys.argv)') "$@"
}

0
投票

你可以使用这样的东西

foo() {
cmd=$(cat <<EOF
print("$1")
EOF
)
python -c "$cmd"
}

或者,

foo() {
python -c $(cat <<EOF
print("$1")
EOF
)
}

然后使用像这样的功能

foo test
© www.soinside.com 2019 - 2024. All rights reserved.