简体   繁体   English

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

[英]How to pass each line in a file as argument to a script?

Iam working on a project which connect between a remote and pull files and do some work with these files.While pulling the files there are several directories in the remote,so pulling each folder is time taking.So is there any command which can be used in the script written below so it can automatically pull all files in the folders.我正在开发一个连接远程和拉文件的项目,并使用这些文件做一些工作。拉文件时,远程有几个目录,所以拉每个文件夹都需要时间。所以有没有可以使用的命令在下面编写的脚本中,以便它可以自动拉取文件夹中的所有文件。

#!/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

Here everytime the script asks for the folder_name and when user enters the folder_name.The script goes to remote and bring files and do some operation.Now First time when it goes to remote it brings folders.txt file .在这里,每次脚本要求输入文件夹名称时,当用户输入文件folders.txt file时,脚本会转到远程并带上文件并执行一些操作。现在第一次去远程时,它会带上folders.txt file So it contains all the folders names.So with using these names to pass as arguments to the python scanner.py $file_name .所以它包含所有文件夹名称。所以使用这些名称作为参数传递给python scanner.py $file_name

Folders.txt looks like this 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

You could parse the txt file with Python :您可以使用 Python 解析 txt 文件:

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)

The loop is now inside Python, you'd just need to call the above script :循环现在在 Python 中,你只需要调用上面的脚本:

python scanner_new.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字典作为命令行参数传递给 Python 脚本? - How to pass dictionary as command line argument to Python script? 如何将列表作为命令行参数传递给 python 脚本 - how to pass a list as a command line argument to a python script Linux:如何将命令行参数传递给正在传递给脚本的命令行参数? - Linux:How to pass command line argument to an command line argument that is being passed to a script? 将列表作为命令行参数传递给Python脚本 - Pass a list to a Python script as a command line argument 将目录作为命令行参数传递给 python 脚本 - Pass directory to python script as command line argument 如何使用 argparse 模块在 python 脚本中将文件作为参数传递? - how to pass file as argument in python script using argparse module? 如何将文件作为参数传递并使用 argparse 模块在 python 脚本中修改它? - how to pass file as argument & modify it in python script using argparse module? 如何使用python逐行调用文件以在文件的每一行中传递命令? - How to call a file line by line to pass commands over each line in the file using python? 如何从脚本将命令行参数传递给python文件 - How to pass command line arguments to a python file from a script &#39;&gt;&#39;不能识别为命令行参数吗? 如何将其作为命令行参数传递? - '>' is not recognising as a command line Argument ? How to Pass this as a command Line Argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM