标签: shell

等到 bash 脚本中满足条件

until [ $(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text) = *"InProgress"* ];
do
  echo "Automation is running......"
  sleep 1m
done
status=$(aws ssm get-automation-execution --automation-execution-id "$id" --query 'AutomationExecution.AutomationExecutionStatus' --output text)
if [ "$status" == "Success" ]; then
    echo "Automation $status"
elif [ "$status" == "Failed" -o "$status" == "Cancelled" -o "$status" == "Timed Out" ]; then
    echo "Automation $status"
fi
Run Code Online (Sandbox Code Playgroud)

这里循环永远不会退出,它继续打印“自动化正在运行......”即使在自动化已经执行并且状态不是进行中我想要做的是等到状态为“进行中”,打印“自动化是运行......”在屏幕上。完成后,如果失败或成功,我想在屏幕上打印自动化的状态。

bash shell aws-cli ssm aws-ssm

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

为什么我不能从另一个终端写入终端设备的标准输入

我在 Gnome 桌面管理器 Ubuntu 中打开了两个 pts 终端。

我想要做的是使用终端 /dev/pts/1 使用重定向向终端 /dev/pts/0 写入一些内容,例如:

##in pts/1
echo date > /dev/pts/0   
Run Code Online (Sandbox Code Playgroud)

但是在 pts/0 中,仅打印日期并且按 Enter 不会执行它。所以我猜 comamnd 不会进入 pts/0 的标准输入。所以我尝试将输出echo date传输到 /dev/pts/0 这给了我权限被拒绝的错误,我成为了 root 并更改了它的权限,但我仍然无法获得date在 pts/0 中运行的命令。

我正在尝试这些东西用于学习目的。所以我真的很困惑它是如何在这里工作的,以及我应该怎么做才能完成它。

unix linux bash shell pty

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

Github api 不解析多行 shell 变量

我在 jenkins 配置中有一个文本参数(来自 jenkins job dsl 插件),它允许您输入多行注释。从 shell 脚本向 github 存储库发布版本时,我使用该变量作为主体值。我收到这个错误,说解析 json 有问题,我找不到解决方法。我会在下面给你举个例子。请帮忙。

PERSONAL_ACCESS_TOKEN="random"
TAG_NAME="12.0.0"
VERSION_BUMP="major"
MIGRATION_DOCUMENT="This is first line
This is second line"

curl -i \
-H "Authorization: token ${PERSONAL_ACCESS_TOKEN}" \
-d '{"tag_name": "'"${TAG_NAME}"'", "name": "'"${VERSION_BUMP}"'", \
"body": "'"${MIGRATION_DOCUMENT}"'"}' \
https://github.deere.com/api/v3/repos/randomOrg/testRepo/releases
Run Code Online (Sandbox Code Playgroud)

shell json github-api

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

尽管交互式 shell 功能正常,但 Bash 功能不适用于远程 ssh 命令执行

我在.bash_aliases文件中定义了一个函数并将其包含在我的.bashrc文件中。

我的.bash_aliases文件:

function dmidecode() {
        if [[ $1 == -t && $2 == 1 ]]; then
                cat ~/some_file
        else
                command dmidecode "$@"
        fi
}
Run Code Online (Sandbox Code Playgroud)

作用是:当有人执行命令:时dmidecode -t 1,bash从中读取内容~/some_file并返回。

我的.bashrc文件:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or …
Run Code Online (Sandbox Code Playgroud)

unix linux bash shell sh

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

在 groovy 中执行 shell 命令会引发“意外字符”错误

我正在尝试执行 shell 命令 n groovy

def shellString = "s/\[\|]\|\s\|'\|(\|)//g"
def temp2 = "echo response| sed -e ${shellString}".execute()
Run Code Online (Sandbox Code Playgroud)

它抛出编译错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 33: unexpected char: '\' @ line 33, column 24.
     def shellString = "s/\[\|]\|\s\|'\|(\|)//g"
                          ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
    at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
    at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521) …
Run Code Online (Sandbox Code Playgroud)

shell groovy

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

Shell命令从变量更新pom文件

以前我使用下面的命令来获取 pom.xml 中的版本并将其从 1 递增。在增量快照版本之前,0.0.1

#!/bin/bash

version=$(grep -ri "<version>" pom.xml |head -n 1 | sed -e 's/^[ \t]* 
<version>\([^<]*\)<.*$/\1/' | sed 's/[-SNAPSHOT]//g')
var1=$(echo $version | cut -c1)
var2=$(echo $version | cut -c2)
var3=$(echo $version | cut -c3)
var4=$(echo $version | cut -c4)
var5=$(echo $version | cut -c5)
var5=$((var5+1))
incrementVer=$var1$var2$var3$var4$var5
echo $incrementVer
Run Code Online (Sandbox Code Playgroud)

输出 is 0.0.2

但我想将此输出推送到 pom 文件并更新为, <version>0.0.2</version>

我可以使用 sed 命令更新 pom 文件吗?

我的 pom 文件看起来像这样,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.mss.inven</groupId>
<artifactId>INVEN</artifactId>
<version>0.0.1-SNAPSHOT</version>
Run Code Online (Sandbox Code Playgroud)

linux bash shell maven

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

在构建 docker 镜像时运行 shell 脚本

我有一个带有install.sh脚本的自定义包,我想在构建./install.shdocker镜像时运行它(意思是 - 放在Dockerfile 中)。我可以将它与容器一起运行,但我想要一个包含所需包(install脚本中提到的包)的图像。

我试过的:

  1. RUN /bin/sh/ -c "./install.sh"
  2. RUN ./install.sh

它错误地说 -

/bin/sh install.sh not found

或者

/bin/sh ./install.sh not found

这可能是一个重复的问题,但我没有在任何地方找到答案。任何帮助,将不胜感激。

shell docker dockerfile docker-build

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

jq 具有来自不同来源的多个输入

使用 jq 时如何混合不同的输入源?对于特定用例,我想将文件中的一些数据添加到标准输出管道中的提要中。

$ echo '[{"a": 1}]' > /tmp/a1
$ echo '[{"a": 2}]' > /tmp/a2
$ jq --slurp '.[0] + .[1]' /tmp/a1 /tmp/a2
[
  {
    "a": 1
  },
  {
    "a": 2
  }
]
$ cat /tmp/a1 | jq --slurp '.[0] + .[1]' /tmp/a2  # Expecting the same result
[
  {
    "a": 2
  }
]
Run Code Online (Sandbox Code Playgroud)

如您所见,最后一个命令没有解释管道数据。

现在,我被迫将第一个操作的输出保存到一个临时文件中,以便我可以在将其发送回网络之前执行 jq 合并操作。拥有一个流会更有效率

shell jq

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

使用 Bash 检查另一个目录中是否存在相同的文件

我是 bash 新手,希望得到您的帮助;找不到这个案例的答案。我正在尝试检查一个目录中的文件是否存在于另一个目录中

假设我有路径 /home/public/folder/(这里我有几个文件),我想检查文件是否存在于 /home/private/folder2

我试过了

for file in $firstPath/*
do
   if [ -f $file ]; then
   (ask if to over write etc.. rest of the code) 
Run Code Online (Sandbox Code Playgroud)

并且

for file in $firstPath/*
do
   if [ -f $file/$secondPath ]; then
   (ask if to over write etc.. rest of the code) 
Run Code Online (Sandbox Code Playgroud)

两者都不起作用;似乎在第一种情况下,它比较第一个路径中的文件(因此它总是问我是否要覆盖,尽管它在第二个路径中不存在)而在第二种情况下,它不会进入if 语句。我怎么能解决这个问题?

unix linux bash shell if-statement

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

使用 JQ“包含”并在未找到密钥时抑制错误

我试图根据同一对象中另一个键的“包含”值来获取值

我已经尝试了一个代码,它可以工作并输出我想要的结果,但是 JSON 中的某些对象没有这个键,因此我得到:

jq: error (at <stdin>:1): null (null) and string ("BBC") cannot have their containment checked

或者这个错误的原因是其他键中的数组,我不确定

使用:

jq '.entries[] | select(.icon | contains("BBC")) | .uuid'

我希望找到的结果的 UUID 没有错误,并将其作为变量存储在 shell 中

"174501xxxxxxxxxxxxxe6342a03"

通过管道传输的输入文件

{  
   "entries":[  
      {  
         "uuid":"174501xxxxxxxxxxxxxe6342a03",
         "enabled":true,
         "autoname":true,
         "name":"BBC",
         "number":0,
         "icon":"file:///logos/BBC.png",
         "icon_public_url":"imagecache/1097",
         "epgauto":true,
         "epggrab":[  ],
         "dvr_pre_time":0,
         "dvr_pst_time":0,
         "epg_running":-1,
         "services":[  ],
         "tags":[  ],
         "bouquet":""
      },
      {  
         "uuid":"174501xxxxxxxxxxxxxe6342a04",
         "enabled":true,
         "autoname":true,
         "name":"ABC",
         "number":0,
         "icon_public_url":"imagecache/1098",
         "epgauto":true,
         "epggrab":[  ],
         "dvr_pre_time":0,
         "dvr_pst_time":0,
         "epg_running":-1,
         "services":[  ],
         "tags":[  ],
         "bouquet":""
      }...
Run Code Online (Sandbox Code Playgroud)

shell json try-catch jq

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

标签 统计

shell ×10

bash ×5

linux ×4

unix ×3

jq ×2

json ×2

aws-cli ×1

aws-ssm ×1

docker ×1

docker-build ×1

dockerfile ×1

github-api ×1

groovy ×1

if-statement ×1

maven ×1

pty ×1

sh ×1

ssm ×1

try-catch ×1