我有许多bash脚本,每个脚本都快乐地做自己的事情。请注意,虽然我使用其他语言进行编程,但我只使用Bash来自动化,并不是很擅长。在bash中解析命令输出到变量
我现在正在尝试将其中的一些脚本组合起来,以创建“元”脚本,如果您愿意的话,可以使用其他脚本作为步骤。问题是我需要解析每个步骤的输出,以便能够将其一部分作为参数传递给下一步。
一个例子:
stepA.sh
[...does stuff here...]
echo "Task complete successfuly"
echo "Files available at: $d1/$1"
echo "Logs available at: $d2/$1"
上述两个都是路径,例如/ var /网络/ thisisatest和/ var /日志/ thisisatest(注意,文件总是用/ VAR开始/ www和日志始终以/ var/log开头)。我只对文件路径感兴趣。
steB.sh
[...does stuff here...]
echo "Creation of $d1 complete."
echo "Access with username $usr and password $pass"
所有变量这里有简单的字符串,可以包含特殊字符(无空格)
我试图建立是运行stepA.sh
,然后stepB.sh
和使用脚本每个人的输出做自己的东西。什么我目前做的(上述两种脚本符号链接到/ usr/local/bin目录,而不.sh
部分并提出可执行文件):
#!/bin/bash
stepA $1 | while read -r line; do
# Create the container, and grab the file location
# then pass it to then next pipe
if [[ "$line" == *:* ]]
then
POS=`expr index "$line" "/"`
PTH="/${line:$POS}"
if [[ "$PTH" == *www* ]]
then
#OK, have what I need here, now what?
echo $PTH;
fi
fi
done
# Somehow get $PTH here
stepB $1 | while read -r line; do
...
done
#somehow have the required strings here
我被困在经过PTH
下一个步骤。我知道这是因为管道在子shell中运行它,但是我所见过的所有示例都涉及到文件而不是命令,而且我无法使其工作。我试图通过管道将echo
到“下一步”,如
stepA | while ...
echo $PTH
done | while ...
#Got my var here, but cannot run stuff
done
如何运行stepA
并有PTH
变量可供以后? 是否有一种“更好的方式”从嵌套if
s中提取我需要的路径?
在此先感谢!
+1对于格式化良好的问题以及对您的问题很明显的工作。但是......很难理解我们如何提供帮助。当前流程的一些实际输出可能会有所帮助,另外还有一些预期输出,既可以作为最终输出,也可以作为步骤A和步骤B的中间输出中所看到的内容。另外我还不清楚,如果你只是想让PTH的值在stepA-> B之间通过,或者你是否希望步骤B有一个$ PTH变量(带有一个值),那么它已经从stepA“继承”了。祝你好运! – shellter 2013-03-09 14:06:44