在终端中运行文本文件 [英] Run text files in terminal
问题描述
有人知道是否有一种方法可以在shell中自动运行命令列表(来自文本文件)?
Does anyone know if there's a way to run automatically in shell a list of commands (from a text file)?
我需要运行很多脚本(大约1000个).脚本在python中,每个接受2个参数(dir_#和sample#)
I need to run a lot of scripts (around 1000). The scripts are in python and take 2 arguments each (dir_#, and 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...
预先感谢
peixe
推荐答案
这称为"shell脚本".
That's called a "shell script."
将此添加到文件顶部:
#!/bin/sh
然后执行以下命令:
chmod +x filename
然后像执行程序一样执行它:
Then execute it like a program:
./filename
或者,您可以直接执行Shell,告诉它执行文件中的命令:
Alternately, you can execute the shell directly, telling it to execute the commands in your file:
sh -e filename
这篇关于在终端中运行文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!