set -e当通过扩展调用这些函数时,Bash 中的选项似乎会在函数内部重置$(...)。
这让我很惊讶,我不确定这是否是一个错误。我无法在(通常相当详尽的)Bash 联机帮助页中找到此行为的描述。
注意:这里还有一些其他类似的帖子:
但它们都没有涉及$(...),这在联机帮助页中也没有真正讨论。
我在优秀的Bash FAQ 105中也找不到对此问题的参考。
这是一个小程序来演示这个问题:
echo "Initial: $-"
set -eu
echo "After set: $-"
function foo() {
echo "Inside foo: $-"
}
foo
function bar() {
false # I'd expect this to immediately fail
echo "Inside bar: $-"
}
# When a $(...) construct is involved, 'bar' runs to completion!
x=$(bar)
echo "We should never …Run Code Online (Sandbox Code Playgroud) 假设我在父目录中有一些图像:../imagea/a.png 和 ../images/b.png。
当我做:
ls ../images
# I get
../images/a.png
../images/b.png
Run Code Online (Sandbox Code Playgroud)
如何为所有这些输出添加前缀
我试过:
!ls ../images/*.png | cat
# am not sure what to do next
Run Code Online (Sandbox Code Playgroud)


Run Code Online (Sandbox Code Playgroud)
感谢帮助。
我想与某人共享一个脚本,但不确定 MacOS 上首选哪个。哪一个最好支持?
这?
#!/bin/sh
echo "Here goes my simple script that changes some settings."
# ... do some stuff
Run Code Online (Sandbox Code Playgroud)
或这个?
#!/bin/bash
echo "Here goes my simple script that changes some settings."
# ... do some stuff
Run Code Online (Sandbox Code Playgroud) 我想从 jenkins 作业向我在 Linux 服务器中编写的 shell 脚本之一发送一些参数。以下是我的詹金斯管道工作:
def MY_VAR
def BUILD_NUMBER
pipeline {
agent any
stages {
stage('Stage One') {
steps {
script {
BUILD_NUMBER={currentBuild.number}
MY_VAR ='abc'
}
}
}
stage('Stage Two') {
steps {
sh '''
cd /scripts/
./my_scripts.sh $BUILD_NUMBER $MY_VAR'''
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我可以发送 的值BUILD_NUMBER,但不能发送 的值MY_VAR。在我看来,自从MY_VAR被设置为管道以来,这就是它发生的原因。有人可以帮忙解决吗
像这样的东西。目标是作为笑话设置的一部分启动和停止我的测试服务器,以便我可以使用单个命令进行端到端测试。
"jest": {
"setupFiles": ["<rootDir>/TestScript.sh"]
}
Run Code Online (Sandbox Code Playgroud) 我可以通过以下命令在 git 存储库的单个分支中找到包含“我的字符串”模式的所有行
git grep "my string" my_branch
Run Code Online (Sandbox Code Playgroud)
说,结果如下
my_branch: file1:file1 什么是“我的字符串”?
my_branch: file2:file2 你好,“我的字符串”只是“我的字符串”!
我们在两个文件的两行中看到 3 次出现。我可以通过以下方式计算这些行
git grep "my string" my_branch | wc -l
Run Code Online (Sandbox Code Playgroud)
这将导致
2
问题是如何获取给定分支中所有文件的所有行中字符串出现的确切数量?是否可以运行一些命令或脚本,在我的示例中给出3而不是2?
我正在尝试在ansible playbook中传递这个curl命令:
shell: 'curl -k -u {{ AMBARI_USER }}:{{ AMBARI_PASSWORD }} -H 'X-Requested-By: ambari' -X POST -d '[{"Event": {"specs": [{"principal_type": "users", "sync_type": "all"}, {"principal_type": "groups", "sync_type": "all"}]}}]' https://{{AMBARI_SERVER_HOST}}:8083/api/v1/ldap_sync_events
Run Code Online (Sandbox Code Playgroud)
这是我的 ansible 剧本:
- hosts: adm
tasks:
- name: sync ldap
shell: "curl -k -u {{ AMBARI_USER }}:{{ AMBARI_PASSWORD }} -H 'X-Requested-By: ambari' -X POST -d '[{"Event": {"specs": [{"principal_type": "users", "sync_type": "all"}, {"principal_type": "groups", "sync_type": "all"}]}}]' https://{{AMBARI_SERVER_HOST}}:8083/api/v1/ldap_sync_events"
Run Code Online (Sandbox Code Playgroud)
问题是这个命令有多个 double 和 simple cotes,所以它不起作用,有没有办法在这里传递它或者我应该为它创建一个 shell 脚本?谢谢
我正在 python 脚本下运行,该脚本最终运行 shell 脚本,该脚本为我提供了 k8s 命名空间中正在运行的版本列表。得到结果,但需要时间 > 3 秒。因此,这导致了松弛的“操作超时”。我是Python新手,已经阅读了有关延迟的各种文档,但这并没有帮助,因为这些文档非常复杂。
from subprocess import Popen, PIPE
from subprocess import check_output
from flask import Flask
def get_shell_script_output_using_communicate():
session = subprocess.Popen(['./version.sh'], stdout=PIPE, stderr=PIPE)
stdout, stderr = session.communicate()
if stderr:
raise Exception("Error "+str(stderr))
return stdout.decode('utf-8')
def get_shell_script_output_using_check_output():
stdout = check_output(['./version.sh']).decode('utf-8')
return stdout
app = Flask(__name__)
@app.route('/test',methods=['POST'])
def home():
return '`Version List` ```'+get_shell_script_output_using_check_output()+'```'
app.run(host='0.0.0.0', port=5002, debug=True)
Run Code Online (Sandbox Code Playgroud)
即使命令花费超过 10 秒,是否有办法获得响应?谢谢!
我正在创建一个 Singularity 图像,并且想要一个自定义的 bash 提示符,就像我可以通过PS1在我的~/.bash_profile.
但是,不幸的是,Singularity 做了一些事情来强制执行自己的提示,如下所示:Singularity>。这不是一个很有帮助的提示。
我尝试在定义文件中设置 PS1,如下例所示:
# File prompt-test1.def
Bootstrap: shub
From: alpine:latest
%environment
export PS1="[my-container]\w\$ "
Run Code Online (Sandbox Code Playgroud)
我构建并运行了该图像,但无济于事:
sudo singularity build prompt-test.sif prompt-test.def
singularity shell prompt-test.sif
# shows Singularity>
Run Code Online (Sandbox Code Playgroud)
使用envshell 中的命令,我注意到已PROMPT_COMMAND设置:
echo $PROMPT_COMMAND
# PS1="Singularity> "; unset PROMPT_COMMAND
Run Code Online (Sandbox Code Playgroud)
...所以我试图改变这一点:
# File prompt-test2.def
Bootstrap: shub
From: alpine:latest
%environment
export PROMPT_COMMAND='PS1="[my-container]\w\$ "; unset PROMPT_COMMAND'
Run Code Online (Sandbox Code Playgroud)
...但提示保持不变!但现在,PROMPT_COMMAND容器内部看起来像
echo $PROMPT_COMMAND
# PS1="[my-container]\w\$ "; unset PROMPT_COMMAND; PROMPT_COMMAND="${PROMPT_COMMAND%%; PROMPT_COMMAND=*}";PS1="Singularity> "
Run Code Online (Sandbox Code Playgroud)
编辑以替换为更好的信息/.singularity.d/env …
我知道如何在笔记本中运行脚本(例如这篇文章)。现在的问题是如何在python中读取shell脚本的结果?比如我们有一个echo "hi"命令,或者我们调用一个shell脚本,我们想用python读取控制台上打印的消息。