如何将文件中的每一行作为参数传递给脚本?

时间:2017-02-07 10:24:32

标签: python linux ubuntu ssh scp

我正在开发一个连接远程和拉文件的项目,并对这些文件做一些工作。虽然拉动文件,但是远程有几个目录,所以拉每个文件夹是时间。所以有任何命令可以在下面编写的脚本中使用,这样它就可以自动提取文件夹中的所有文件。

#!/bin/bash

clear

ssh someName@192.168.X.X 'ls SomeFolder/SomeSubFolder' > folders.txt

cat folders.txt
echo "Enter the folder name "
read folder_name

scp -r someName@192.168.X.X:SomeFolder/SomeSubFolder/$folder_name/ $folder_name/

while IFS= read -r -d '' file; do
    # single filename is in $file
    python scanner_new.py /home/nsadmin/Kiran/bash_script_run_files/"$file"
done < <(find "$folder_name" -type f -print0)
rm -rf $folder_name

此处每次脚本都会询问folder_name以及用户何时输入folder_name。脚本会转到远程并带来文件并执行一些操作。现在第一次转到远程时它会带来folders.txt file。因此它包含所有文件夹名称。因此使用这些名称作为参数传递给python scanner.py $file_name

Folders.txt看起来像这样

2016_9_25
2016_9_26
2016_9_27
2016_9_28
2016_9_29
2016_9_30
2016_9_7
2016_9_9

1 个答案:

答案 0 :(得分:1)

您可以使用Python解析txt文件:

customer    txn_date    tag
A           1-Jan-17    1   
A           2-Jan-17    1 
A           3-Jan-17    0 (inserted)
A           4-Jan-17    1 
A           5-Jan-17    0  
B           1-Jan-17    0 (inserted)
B           2-Jan-17    0 (inserted)
B           3-Jan-17    1
B           4-Jan-17    0 (inserted)
B           5-Jan-17    0

循环现在在Python中,你只需要调用上面的脚本:

from os import path

def do_stuff_with_folder(folder_name):
  folder_name = folder_name.strip()
  script_name = path.join("/home/nsadmin/Kiran/bash_script_run_files/", folder_name)
  if path.exists(folder_name):
    print "Do stuff with folder %s" % folder_name
    # do stuff with folder_name here
  else:
    print "Warning : %s not here" % folder_name

with open('folders.txt') as folder_names:
  for folder_name in folder_names:
    do_stuff_with_folder(folder_name)