简体   繁体   English

将多行 bash 脚本转换为单行

[英]Convert multiples line bash script to single line

I am trying to write a bash script to use in python code.我正在尝试编写 bash 脚本以在 python 代码中使用。

Multi-line bash command (this works perfectly when run directly from terminal)多行 bash 命令(直接从终端运行时完美运行)

mydatefile="/var/tmp/date"
while IFS= read line
do
    echo $line
        sh /opt/setup/Script/EPS.sh $(echo $line) | grep "WORD" | awk -F ',' '{print $6}'
    sleep 1
done <"$mydatefile"

My single line conversion我的单行转换

mydatefile="/var/tmp/date;" while IFS= read line do echo $line; sh /opt/setup/Script/EPS.sh $(echo $line) | grep "WORD" | awk -F ',' '{print $6}'; sleep 1; done <"$mydatefile";

ERROR错误

-bash: syntax error near unexpected token `done'

Missing a ;缺少一个; (fatal syntax error): (致命的语法错误):

while IFS= read line; do echo ...
#                   ^
#                 here

More in depth:更深入:

  • combined grep+awk in a single command在单个命令中组合grep+awk
mydatefile="/var/tmp/date"
while IFS= read line; do
    echo "$line"
    sh /opt/setup/Script/EPS.sh "$line" | 
        awk -F ',' '/WORD/{print $6}'
    sleep 1
done < "$mydatefile"
mydatefile="/var/tmp/date;" while IFS= read line; do echo $line; sh /opt/setup/Script/EPS.sh "$line" | awk -F ',' '/WORD/{print $6}'; sleep 1; done < "$mydatefile";

One way to do this conversion might be to paste the script onto the command-line, then look up in the history - though this might depend on the version of command-line editor you have.进行此转换的一种方法可能是将脚本粘贴到命令行,然后在历史记录中查找 - 尽管这可能取决于您拥有的命令行编辑器的版本。 Note that you do need a semicolon before do , but NOT after.请注意,在 do 之前do需要分号,但之后不需要。 You are punished for too many semicolons as well as too few.你会因为分号太多和太少而受到惩罚。

Another way would be to line-by-line fold each line in your script and keep testing it.另一种方法是逐行折叠脚本中的每一行并继续测试它。

The binary chop approach is do the first half, test and undo or continue.二进制斩波方法是做前半部分,测试并撤消或继续。

Once you have it down to 1 line that works you can pasted it into python.一旦你把它降到 1 行,你可以将它粘贴到 python 中。

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

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