有关为Bash命令shell编写的脚本的问题。对于有错误的shell脚本,请在发布此处之前使用shellcheck程序(或在https://shellcheck.net上的Web shellcheck服务器中)进行检查。关于交互式使用Bash的问题更可能是超级用户的主题,而不是Stack Overflow。
我想在我的 Dockerfile 中设置一个多行环境变量。 通过命令行工作 如果我通过 docker run 传入环境变量,一切正常。 配置=“端口:4466 数据库...
目前,当用户向 LLM 发送输入时,其格式必须为: <|im_start|>用户\ 用户在此输入。<|im_end|> 这非常麻烦并且使得即时工程非常繁琐......
我想使用 awk 和一些逻辑创建一个 oneliner。这是要使用的命令和输出: grep -v "local_address" --no-filename "/proc/net/tcp" "/proc/net/tcp...
ImageMagick 与 Bash/Zsh:使用 *.(format1|format2) 与 "*.{format1,format2}"
要检查我的图像的文件扩展名是否正确,即我的 JPEG 是否真的是 JPEG、PNG 是否真的是 PNG、WebP 是否真的是 WebP,我可以使用以下 ImageMagick 通讯...
如何调用 bash 脚本或函数在另一个 bash 脚本中返回非数字值
我有2个bash脚本: sc1.sh #!/bin/bash 函数 func_name() { var123=“abcd123” 返回 $var123 } 函数名 脚本2.sh #!/bin/bash 变量= <-here I want to have value abcd123 How to do...
对编码相当陌生,正在寻求将变量输出到本地 html 文件的一些帮助。脚本和html都在同一台机器上。脚本从调制解调器提取信号电平,我会...
我有一个使用共享链接(非公共链接)和curl从Dropbox下载的文件夹。它作为压缩文件夹下载。我需要在 bash shell 脚本中使用 unzip 来解压该文件夹。什...
在 Synology nas 上递归提取 zip 存档(概念)
我正在使用 Synology NAS。我有一个备份 ZIP 文件(一个包含另一个 ZIP 文件的 ZIP 文件)的概念。文件很大,所以我想编写解压缩脚本,所以首先提取外部 zip,然后...
如何启用 CLI 输出的颜色? 下面是在 Ubuntu 上运行的。 如果您看到屏幕截图,显然终端已启用颜色。而且,如果我调用 echo,它不会着色...
我的 while 循环对于这个 bash 脚本来说是无限的,遇到了问题
我只是想提示输入用户名和密码,并在三次错误尝试后让 while 循环结束。我认为这与我增加计数的方式有关。我的代码...
如何在 terraform 外部数据源中正确地使 bash 脚本失败
我有一个地形数据源: 数据“外部”src { 程序= [“bash”,“my_script.sh”] } 在 my_script.sh 中,我断言环境变量 SOME_ENV 已设置并且...
嗨,我想运行一个 Linux 命令,该命令可以自动执行 scp,其起始位置不同,目标位置保持不变。 例如 - scp 123.txt [email protected]:~/
我有以下内容: if [[ "${outfile}" == '/dev/stdout' ]];然后 “${mysql_cmd[@]}”<"${infile}" >“${outfile}” 别的 “${mysql_cmd[@]}”<&
无法使用python“问题不重复”运行.sh和.ps1文件[重复]
我了解到,您可以通过分别在 Bash 和 PowerShell 中执行 .sh 或 .ps1 文件来编写脚本并运行多个命令。但是,当我运行我的文件时,我收到相同的错误(在 Bash 和
我了解到,您可以通过分别在 Bash 和 PowerShell 中执行 .sh 或 .ps1 文件来编写脚本并运行多个命令。但是,当我运行我的文件时,我收到相同的错误(在 Bash 和
我发现很难理解如何在这样的 shell 脚本中启动进程 主脚本.sh 执行命令1 & 睡10 执行命令2 & 睡10 我理解 exec 命令...
使用“sleep infinity”时无法捕获 SIGINT 和 SIGTERM
我的示例代码: #!/bin/bash LOGFILE =“/tmp/example.log” 清理函数(){ echo "脚本被 SIGINT 中断,正在清理..." >> $LOGFILE 出口0 }
不确定标题是否清楚,但基本上我有一些如下所示的XML: 测试1 不确定标题是否清楚,但基本上我有一些如下所示的 XML: <details> <result id=1234567890> <name>Test1</name> </result> <result id=5345345433> <name>Test2</name> </result> <result id=9572385354> <name>Test3</name> </result> 我想要完成的是找到使用已知值的 id 属性 即测试1 > 1234567890,测试2 > 5345345433,测试3 > 9572385354 最好使用 xmllint,但 xmlstarlet 也是一个选项。 输入 首先,您的 XML 无效。你的id属性需要被qouted,并且详细信息没有关闭。这是修改后的输入: <details> <result id="1234567890"> <name>Test1</name> </result> <result id="5345345433"> <name>Test2</name> </result> <result id="9572385354"> <name>Test3</name> </result> </details> 结果 下面将使用 xmlstarlet 提取给定 name 属性的特定 id。 xmlstarlet sel -t -c "/details/result[name='Test1']" test.xml | grep -Po "(?<=id=\")[\d]*" 这会回来 1234567890 您也可以将命令中的 Test1 替换为变量。 var=Test1 xmlstarlet sel -t -c "/details/result[name='$var']" test.xml | grep -Po "(?<=id=\")[\d]*" 故障 xmlstarlet sel -t -c "/details/result[name='$var']" test.xml 选择结果中与 $var 匹配的所有名称标签。 | grep -Po "(?<=id=\")[\d]*" 使用 Perl Regex 将输出通过管道传输到 grep 以查找 id 属性并打印所有包含的数字。 您还可以使用xmllint: xmllint --xpath "string(/details/result[name='Test1']/@id)" yourfile.xml --xpath:告诉 xmllint 使用 xpath 语法进行选择。 xpath选择器的详细信息: string(/details/result[name='Test1']/@id) string():制作字符串 /details/result:选择result元素的details子元素 [name='Test1']:包含一个name节点,其值为Test1 /@id:id属性值(result元素) 也许一个简单的 grep 和 awk 解决方案适合您。 grep -B1 Test1 sample.xml | awk '/id=/{gsub(/[^0-9]+/, "", $0); print $0 }' 完整回答OP的问题, #/bin/bash # # how to use xmllint to get information from specific elements # REQUIRES libxml2 (sorry Snow Leopard!) mytestxml=' <details> <result id="1234567890"> <name>Test1</name> </result> <result id="5345345433"> <name>Test2</name> </result> <result id="9572385354"> <name>Test3</name> </result> </details> ' echo Test Document is :"$mytestxml" echo Get the contents of the \''id'\' attribute of a specific \''result'\' element query=\''string(/details/result[3]/@id)'\' echo xpath query is "$query" myresult=$(echo "$mytestxml" | xmllint --xpath 'string(/details/result[3]/@id)' - ) echo info returned is "$myresult" echo "" echo Get the specific \''result'\' node whose \''name'\' element is \"Test1\" query=\''/details/result[name="Test1"]'\' echo xpath query is "$query" myresult=$(echo "$mytestxml" | xmllint --xpath '/details/result[name="Test1"]' - ) echo info returned is "$myresult" echo "" echo Get the \''id'\' attribute of the specific \''result'\' node whose \''name'\' element is \"Test1\" query=\''string(/details/result[name="Test1"]/@id)'\' echo combined xpath query is "$query" myresult=$(echo "$mytestxml" | xmllint --xpath 'string(/details/result[name="Test1"]/@id)' - ) echo info returned is "$myresult" 获取特定“result”元素的“id”属性的内容。 xpath 查询是: 'string(/details/result[3]/@id)' 返回的信息是:9572385354 获取'name'元素为“Test1”的特定'result'节点 xpath 查询是: '/details/result[name="Test1"]' 返回的信息是: <result id="1234567890"> <name>Test1</name> </result> 获取'name'元素为“Test1”的特定'result'节点的'id'属性 组合的 xpath 查询是: 'string(/details/result[name="Test1"]/@id)' 返回的信息是1234567890 希望这对找到此页面的其他人有用。 :o) 这样的东西应该与 xmlstarlet 一起使用(对我有用): xmlstarlet sel --template --match "/details/result[name='Test1']" --value-of "@id" test.xml
我喜欢数字时钟。我想知道:一条街道有多少种不同的数字模式? 例如01:23:45 .. 23:54:10 在 bash 中提供一个很酷的实现,也带有实用程序! 我的出发点是: 我=0 夫...
我正在尝试将代码上传到我在 github 上创建的存储库,但出现错误。 假设存储库的 URL 是 https://github.com/a,我该如何将代码上传到远程