Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LinuxShell脚本,用于使用不同参数迭代调用同一Python程序_Python_Linux_Bash_Python 2.7 - Fatal编程技术网

LinuxShell脚本,用于使用不同参数迭代调用同一Python程序

LinuxShell脚本,用于使用不同参数迭代调用同一Python程序,python,linux,bash,python-2.7,Python,Linux,Bash,Python 2.7,我有一个Python程序,每次都需要不同的参数。这些参数是存储在名为URLs.txt的文件中的URL。例如,如果urls.txt包含: abc.com xyz.com def.com mno.com 我想执行: python myProgram.py -u 'abc.com' -f output.txt python myProgram.py -u 'xyz.com' -f output.txt python myProgram.py -u 'def.com' -f output.txt py

我有一个Python程序,每次都需要不同的参数。这些参数是存储在名为
URLs.txt
的文件中的URL。例如,如果
urls.txt
包含:

abc.com
xyz.com
def.com
mno.com
我想执行:

python myProgram.py -u 'abc.com' -f output.txt
python myProgram.py -u 'xyz.com' -f output.txt
python myProgram.py -u 'def.com' -f output.txt
python myProgram.py -u 'mno.com' -f output.txt
这是怎么做到的

for url in `cat urls.txt`
do
    python myProgram.py -u "$url" -f output.txt
done
也可以通过while循环和
读取来完成:

while read url
do
    python myProgram.py -u "$url" -f output.txt
done <urls.txt
读取url时
做
python myProgram.py-u“$url”-f output.txt

我闻到猫没用了吗;-)
cat
方法的优点是,如果文件的最后一行没有以换行符终止,则不会删除文件的最后一行。这也可以用可读性较差的while…read、
while read url | |[[-n$url]]来处理;执行python myProgram.py-u“$url”-f output.txt;完成
。猫似乎没有那么无用:)甚至
xargs-n1-ifoopython-myProgram.py-u“foo”-f output.txt
…这个问题一点也不“太宽泛”。答案很简单,只有几个。