gpt4 book ai didi

python - 如何使用 Python 代码在 bash 中使用换行符进行多行输入

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:02 25 4
gpt4 key购买 nike

我正在尝试使用 Python 自动化我们在命令行上订购 SSL 证书的方式。但是我看到了如何格式化用户多行输入的问题。我怎样才能让 bash 等到输入完成并在单行输出中获得多行输入?

为了订购证书,我们生成了这种格式的 CSR

-----BEGIN CERTIFICATE REQUEST-----
MIICrjCCAZYCAQAwaTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCk5ldyBKZXJzZXkx
fjfawcwkefmFNaWamsdffjawfbjwnfkwmelfmw[wefkwaeifbhwfmamfawfoawfj
cmVjcnVpdGluZy1pYXQuYWRwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
AQoCggEBAMOf6uvCfqj9SbOg6b+IzO7YdaY3J59+Dkm3lIf9gXRZBohT4qpevoJt
kdfkdsnfjdsnewirubewfwfmoweuewfvwfwkfmlwfoerueyvwamladodfofehJDG
nJRl1sL32iIapTZABqA4e9ai+9HTOyV7bq5yMEqJiKFLh1VBHC+BlqXPIW6VjjXX
YX3jF2/Tp0ZAEG0yoHXr0D4JR+lFIIstsAuIbd529U5Oyl3js9Oxu0IDq//o2gkR
SnVby7uhuFy8rGEs70YP4dTbWs1Lu6QKATszX6V4CXzM9fwThVByeOH/1X/zPQYe
amCPR+c0nySBI+bQaISBxvyO/T8RKTMCAwEAAaAAMA0GCSqGSIb3DQEBCwUAA4IB
AQAxLFqyKABFeUIMTzWzOIWz8q9M0uWcMMdxA7+485F0m0QqSYIDHKjf7NnoIUZu
sPx+S9pQE3WZklaX1Kd/m7VEwhgI4ZVDJhnGAfxF4GDlLO8sVIZj2VcDcfypZ9U5
hWP1lCUx+2MIDhqZhMI/vEr0jipKdigMiPeHt/Dy3DpM5qdt2zjUZm9uZvP1/Azf
BTc6yaTwCTDuvGLgSJj5mqIxbDHjv/IQjxAnuzcN81pKEgXaYvGyQLuBTEDjq2qW
pxbKF8NfVOzcoK8afu/GPt4FrtxWoRg/jUUztyjVRKBrSwvmrzAfiV2yCNb0/sTK
Ffc3zbKLapk5/ZXrevrZJ2p4
-----END CERTIFICATE REQUEST-----

我将其作为用户输入,您可以看到它的格式,每次都使用换行符。但是,由于 bash 将换行符视为命令,因此它会给我错误。我的问题或多或少类似于这个问题:How to convert multiline file into a string in bash with newline character?

尝试了一个看起来像这样的 while 循环:

csr = ""
c = input("Enter CSR:")

while True:
if "\n" in c:
csr += c
csr.replace("\n", "")
break

但并不像说明的那样工作。

预期的结果是这样的:

-----BEGIN CERTIFICATE REQUEST-----MIICrjCCAZYCAQAwaTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCk5ldyBKZXJzZXkxfjfawcwkefmFNaWamsdffjawfbjwnfkwmelfmw[wefkwaeifbhwfmamfawfoawfjcmVjcnVpdGluZy1pYXQuYWRwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMOf6uvCfqj9SbOg6b+IzO7YdaY3J59+Dkm3lIf9gXRZBohT4qpevoJtkdfkdsnfjdsnewirubewfwfmoweuewfvwfwkfmlwfoerueyvwamladodfofehJDGnJRl1sL32iIapTZABqA4e9ai+9HTOyV7bq5yMEqJiKFLh1VBHC+BlqXPIW6VjjXXYX3jF2/Tp0ZAEG0yoHXr0D4JR+lFIIstsAuIbd529U5Oyl3js9Oxu0IDq//o2gkRSnVby7uhuFy8rGEs70YP4dTbWs1Lu6QKATszX6V4CXzM9fwThVByeOH/1X/zPQYeamCPR+c0nySBI+bQaISBxvyO/T8RKTMCAwEAAaAAMA0GCSqGSIb3DQEBCwUAA4IBAQAxLFqyKABFeUIMTzWzOIWz8q9M0uWcMMdxA7+485F0m0QqSYIDHKjf7NnoIUZusPx+S9pQE3WZklaX1Kd/m7VEwhgI4ZVDJhnGAfxF4GDlLO8sVIZj2VcDcfypZ9U5hWP1lCUx+2MIDhqZhMI/vEr0jipKdigMiPeHt/Dy3DpM5qdt2zjUZm9uZvP1/AzfBTc6yaTwCTDuvGLgSJj5mqIxbDHjv/IQjxAnuzcN81pKEgXaYvGyQLuBTEDjq2qWpxbKF8NfVOzcoK8afu/GPt4FrtxWoRg/jUUztyjVRKBrSwvmrzAfiV2yCNb0/sTKFfc3zbKLapk5/ZXrevrZJ2p4------END CERTIFICATE REQUEST-----

但是当我将 CSR 作为输入输入终端时,它会像这样出错:

$ MIICrjCCAZYCAQAwaTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCk5ldyBKZXJzZXkx
bash: MIICrjCCAZYCAQAwaTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCk5ldyBKZXJzZXkx: command not found
$ ETAPBgNVBAcMCFJvc2VsYW5kMREwDwYDVQQKDAhBRFAsIExMQzEfMB0GA1UEAwwW
bash: ETAPBgNVBAcMCFJvc2VsYW5kMREwDwYDVQQKDAhBRFAsIExMQzEfMB0GA1UEAwwW: command not found
$ cmVjcnVpdGluZy1pYXQuYWRwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
bash: cmVjcnVpdGluZy1pYXQuYWRwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC: command not found

谁能告诉我一种在 bash 中进行多行输入然后将 key 放在一行中的方法吗?

最佳答案

看看这个how-do-i-read-multiple-lines-from-stdin-into-a-variable

您可以使用类似 bash 命令读取多行

myVar=$(</dev/stdin)

并且需要在最后输入Ctrl+D关闭输入流。

如果您正在寻找 Python 代码,您可能需要将输入语句包含到循环中。像这样:

print("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)

关于python - 如何使用 Python 代码在 bash 中使用换行符进行多行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com