[英]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.