简体   繁体   English

在 bash / python 脚本中组合几个命令

[英]Combining couple of commands inside the bash / python script

I am already doing my job with couple of commands, but I need manual effort and have to observe the job each time.我已经用几个命令完成了我的工作,但我需要手动操作并且每次都必须观察工作。 So I want everyhing to be done automatically and I should only be notified if desired result happens.因此,我希望一切都自动完成,并且只有在发生所需结果时才应通知我。 So my scenario is below;所以我的情况如下;

I create results with my script stv.sh我用我的脚本 stv.sh 创建结果

Cmd 1: >> timeout 15m./stv.sh -parameter1 -parameter2 -output file file1.txt Cmd 1: >> timeout 15m./stv.sh -parameter1 -parameter2 -output file file1.txt

than I take the necessary fields from my raw tex file to anoher file比我从原始 tex 文件中获取必要的字段到另一个文件

Cmd 2: >> sed file1.txt -parameter1 -parameter2 -output file cleanfile1.txt Cmd 2: >> sed file1.txt -parameter1 -parameter2 -output file cleanfile1.txt

than I am adding a test line to my text file比我在我的文本文件中添加一个测试行

Cmd 3: >> cat testline.txt >> cleanfile1.txt Cmd 3: >> cat testline.txt >> cleanfile1.txt

than I compare my new file with my existing results to find any matching lines比我将我的新文件与我现有的结果进行比较以找到任何匹配的行

Cmd 4: >> comm -12 myexistingresults.txt <(cat cleanfile1.txt | sort | uniq ) > matching.txt Cmd 4: >> comm -12 myexistingresults.txt <(cat cleanfile1.txt | sort | uniq ) > matching.txt

now, in the matching.txt, at least 1 result will appear in any case because we've included testline.txt with Cmd 2. But if there is more than one line, the command below needs to run;现在,在matching.txt中,无论如何都会出现至少1个结果,因为我们已经包含了testline.txt和Cmd 2。但是如果多于一行,则需要运行以下命令;

Cmd 5: >> grep -A 2 $secondlineofmatchingtxt file1.txt > victory.txt Cmd 5: >> grep -A 2 $secondlineofmatchingtxt file1.txt > victory.txt

that means I found a matching line.这意味着我找到了一条匹配线。 So, in the matching.txt, if there is more than one line I need to be notified somehow (by sending e-mail etc.), else if there is only one line in matching.txt, the script needs to start from Cmd 1 again.所以,在matching.txt中,如果多于一行需要以某种方式通知我(通过发送电子邮件等),否则如果matching.txt中只有一行,脚本需要从Cmd开始1 再次。

How can I establish this goal with a simple bash or python script?如何使用简单的 bash 或 python 脚本来建立这个目标?

Create a file to add the interpreter at the top of the file, #!/bin/bash and then enter all the commands into one file, allow executable permissions on the file and then run the file.创建一个文件,在文件顶部添加解释器, #!/bin/bash然后将所有命令输入到一个文件中,允许对该文件的可执行权限,然后运行该文件。

For the email part you might want to use the exit code of the script ./stv.sh file.对于 email 部分,您可能需要使用脚本./stv.sh文件的退出代码。 After running this script successfully, find the exit code, and create an if statement to test if the exit code matches and if does match, then the email will be sent.成功运行此脚本后,找到退出代码,并创建一个if语句来测试退出代码是否匹配,如果确实匹配,则会发送 email。 Otherwise another way would be to check if the file1.txt exists, but this might not be accurate.否则另一种方法是检查file1.txt是否存在,但这可能不准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM