【问题标题】:Run text files in terminal在终端中运行文本文件
【发布时间】:2011-09-26 14:54:05
【问题描述】:

有谁知道是否有办法在 shell 中自动运行命令列表(来自文本文件)?

我需要运行很多脚本(大约 1000 个)。这些脚本在 python 中,每个都有 2 个参数(dir_# 和 sample#)

我制作的文本文件是这样的……

     python /home/name/scripts/get_info.py dir_1 sample1
     python /home/name/scripts/get_info.py dir_2 sample2
     python /home/name/scripts/get_info.py dir_3 sample3
     python /home/name/scripts/get_info.py dir_4 sample4
     ...

所以,我希望将此文本文件作为参数传递给终端中的命令,可以自动完成这项工作......

提前致谢,

佩克斯

【问题讨论】:

  • 您要查找的命令是source

标签: linux list command-line terminal


【解决方案1】:

另外,您可以通过以下方式运行 shell 文件:

source filename

【讨论】:

【解决方案2】:

这就是所谓的“shell脚本”。

将此添加到文件顶部:

#!/bin/sh

然后执行这个命令:

chmod +x filename

然后像程序一样执行:

./filename

或者,您可以直接执行 shell,告诉它执行文件中的命令:

sh -e filename

【讨论】:

    【解决方案3】:

    你可以写一个shell脚本:

    #! /bin/sh
    
    python /home/name/scripts/get_info.py dir_1 sample1
    python /home/name/scripts/get_info.py dir_2 sample2
    python /home/name/scripts/get_info.py dir_3 sample3
    python /home/name/scripts/get_info.py dir_4 sample4
    ...
    

    【讨论】:

      【解决方案4】:

      要么使文件可执行:

      chmod u+x thefile
      ./thefile
      

      或将其作为 sh 的参数运行:

      sh thefile
      

      【讨论】:

      • 仅仅让它可执行是行不通的,除非第一行包含#!/bin/sh
      • 这不是真的,至少 bash 不是这样。
      • 也许不使用 bash,但我认为我们不能假设每个人都在使用 bash——您自己的答案并不认为使用“sh thefile”:)
      • AFAIK,脚本只需要这个指令来确保使用特定的 shell,这绝不是强制性的。
      猜你喜欢
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2015-07-25
      • 2012-06-29
      • 1970-01-01
      相关资源
      最近更新 更多