Linux 在终端中运行文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6545968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:52:07  来源:igfitidea点击:

Run text files in terminal

linuxlistcommand-lineterminal

提问by peixe

Does anyone know if there's a way to run automatically in shell a list of commands (from a text file)?

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

I need to run a lot of scripts (around 1000). The scripts are in python and take 2 arguments each (dir_#, and sample#)

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

The text file I've made looks like this...

我制作的文本文件看起来像这样......

     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
     ...

So, I would expect that passing this text file as argument to a command in terminal, could do the job automatically...

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

Thanks in advance,

提前致谢,

peixe

佩谢

采纳答案by Flimzy

That's called a "shell script."

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

Add this to the top of your file:

将此添加到文件的顶部:

#!/bin/sh

Then execute this command:

然后执行这个命令:

chmod +x filename

Then execute it like a program:

然后像程序一样执行它:

./filename

Alternately, you can execute the shell directly, telling it to execute the commands in your file:

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

sh -e filename

回答by Flimzy

Either make the file executable:

要么使文件可执行:

chmod u+x thefile
./thefile

or run it as an argument of sh:

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

sh thefile

回答by Giann

You can write a shell script:

你可以写一个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
...

回答by Alasmari

Also, you can run a shell file by:

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

source filename