我正在尝试在bash中编写一些代码,它使用内省来选择要调用的相应函数.
确定候选者需要知道定义了哪些功能.仅使用参数扩展在bash中列出已定义的变量很容易:
$ prefix_foo="one"
$ prefix_bar="two"
$ echo "${!prefix_*}"
prefix_bar prefix_foo
Run Code Online (Sandbox Code Playgroud)
但是,为函数执行此操作似乎需要过滤set的输出 - 这是一种更随意的方法.
有正确的方法吗?
在shell脚本中,我有以下代码:
if echo Mr.32 ; then
echo Success
else
echo Failed
exit
fi
Run Code Online (Sandbox Code Playgroud)
Windows批处理文件的等效语法是什么?
抱歉这么无辜的问题 - 我只是试着去...
例如 - 我有:
$ cat test.sh
#!/bin/bash
declare -f testfunct
testfunct () {
echo "I'm function"
}
testfunct
declare -a testarr
testarr=([1]=arr1 [2]=arr2 [3]=arr3)
echo ${testarr[@]}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
$ ./test.sh
I'm function
arr1 arr2 arr3
Run Code Online (Sandbox Code Playgroud)
所以这是一个问题 - 我必须(如果必须......)插入declare
这里?随着它 - 或没有它它的工作原理相同......
我可以理解例如declare -i var
或declare -r var
.但是对于什么是-f
(声明函数)和-a
(声明数组)?
感谢您的提示和链接.
我正在开发一个开源库,并希望将发布内容发送到内部工件服务器.我一直在使用maven distributionManagement
和overriding altDeploymentRepository
指向内部服务器(因此它永远不会在pom中).我这样做很好,deploy:deploy
但是当我使用maven release插件时,这似乎被忽略了.
mvn clean -DaltDeploymentRepository=central::default::internalhost deploy ## works
mvn clean -DaltDeploymentRepository=central::default::internalhost release:perform ## fails, doesn't pick up this setting
Run Code Online (Sandbox Code Playgroud)
当发布插件运行部署时,如何通过命令行覆盖存储库?
你能帮助我吗,为什么有时候会(50:50):
webkit_server.NoX11Error: Cannot connect to X. You can try running with xvfb-run.
Run Code Online (Sandbox Code Playgroud)
当我并行启动脚本时:
xvfb-run -a python script.py
Run Code Online (Sandbox Code Playgroud)
您可以像这样自己重现:
for ((i=0; i<10; i++)); do
xvfb-run -a xterm &
done
Run Code Online (Sandbox Code Playgroud)
在这个启动的10个xterm实例中,其中9个通常会失败,退出该消息Xvfb failed to start
.
我正在尝试用python中的lxml进行解析,这是我的输出
<td>
<span style="display:inline">text1</span>
<span style="display:none">text2</span>
<span>text3</span>
text4
</td>
Run Code Online (Sandbox Code Playgroud)
以为我足够聪明,可以使用以下内容
tree = tr.xpath("//*[contains(@style,'inline')]/text()")
Run Code Online (Sandbox Code Playgroud)
但后来我以为我只会看到text1.我想要的是看到text3和text4也是如此,以便输出
['text1','text3','text4']
有人能把我送到正确的方向吗?
我想打印名为"$ 1"的字符串.但是当我使用echo执行此操作时,它会输出等于"$ 1"变量的字符串.如何像字符串一样打印"$ 1"?
例如:
set -- "output" # this sets $1 to be "output"
echo $1 # ==> output
Run Code Online (Sandbox Code Playgroud)
但我想要这个:
echo $1 # ==> $1
Run Code Online (Sandbox Code Playgroud) 您可以使用以下代码从脚本内部启动交互式控制台:
import code
# do something here
vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
Run Code Online (Sandbox Code Playgroud)
当我像这样运行脚本时:
$ python my_script.py
Run Code Online (Sandbox Code Playgroud)
交互式控制台打开:
Python 2.7.2+ (default, Jul 20 2012, 22:12:53)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
Run Code Online (Sandbox Code Playgroud)
控制台有所有全局和本地加载,这很好,因为我可以轻松测试东西.
这里的问题是箭头在启动Python控制台时不像通常那样工作.他们只是将转义字符显示到控制台:
>>> ^[[A^[[B^[[C^[[D
Run Code Online (Sandbox Code Playgroud)
这意味着我无法使用向上/向下箭头键调用以前的命令,也无法用左/右箭头键编辑行.
有谁知道为什么会这样和/或如何避免这种情况?
此命令成功
$ PS1='$(date +%s) $ ' 1391380852 $
但是,如果我添加换行符,则会失败
$ PS1='$(date +%s)\n$ ' bash: command substitution: line 1: syntax error near unexpected token `)' bash: command substitution: line 1: `date +%s)'
如果我使用反引号就行了
$ PS1='`date +%s`\n$ ' 1391381008 $
但是反叛是沮丧的.那是什么导致了这个错误?
GNU bash, version 4.2.45(6)-release