【问题标题】:How to read and print contents of a file in bash script?如何在 bash 脚本中读取和打印文件的内容?
【发布时间】:2021-12-20 13:30:03
【问题描述】:

我一直在尝试编写以下 bash 脚本

#!/bin/bash
python request.py $1 > output.txt
value=$(output.txt)
echo "$value"

python 代码用于执行 google 搜索并将 url 存储到输出文件。 我想打印 txt 文件,但在这里失败了。

【问题讨论】:

标签: python bash shell sh manjaro


【解决方案1】:

您可以使用 cat 来做到这一点

#!/bin/bash
python request.py $1 > output.txt
cat output.txt

【讨论】:

  • 有没有办法将 output.txt 作为参数传递给另一个 python 程序??
  • python myProgam.py output.txt 既然你创建了它就可以使用它
  • 有没有办法将文件的内容存储在一个变量中以备将来使用? value=$(cat output.txt) 给了我以下错误 cat: output.txt: No such file or directory 即使 output.txt 被 python 接受并且存在于同一目录中
  • 那你做错了;此代码肯定会在当前目录中创建一个具有该名称的文件。 (虽然更好的解决方案可能是output=$(python request.py "$1" | tee output.txt)
猜你喜欢
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 2021-08-02
  • 2016-11-11
  • 1970-01-01
  • 2017-12-20
  • 2014-10-26
  • 2016-10-29
相关资源
最近更新 更多