Backdoor Cipher Backdoor Cipher - 1 year ago 48
Python Question

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.



ssh [email protected] 'ls SomeFolder/SomeSubFolder' > folders.txt

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

scp -r [email protected]:SomeFolder/SomeSubFolder/$folder_name/ $folder_name/

while IFS= read -r -d '' file; do
# single filename is in $file
python /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
. So it contains all the folders names.So with using these names to pass as arguments to the
python $file_name

Folders.txt looks like this


Answer Source

You could parse the txt file with 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
    print "Warning : %s not here" % folder_name

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

The loop is now inside Python, you'd just need to call the above script :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download