user3749778 user3749778 - 1 year ago 58
Bash Question

Iteratively pass one file from a collection of files as command line parameters in shell script

I have x files: A, B, C... What I need to do is pass each of these files as the first command line argument to a python file and pass the others as the second command line argument until all files have been passed as $1 once. For example, on the first iteration A is $1 and B,C... is $2. On the second iteration, B is $1 and A,C... is $2. I've read about the shift command in shell but am not very sure if it will work in my case (I'm also relatively new to shell scripting). Also, is there a limit to the number of command line arguments I can pass to my python script? I would also like to create a variable to hold the list of file names before iterating through my files. Thank you!

Answer Source

Bash has arrays, and supports array slicing via ${array[@]:start:end} syntax, where start and end are optional indices. That's enough to get the job done.


# Store the master list of file names in an array called $files.    

for ((i = 0; i < ${#files[@]}; ++i)); do
    # Store the single item in $file and the rest in an array $others.
    others=("${files[@]:0:i}" "${files[@]:i+1}")

    # Run Use ${others[*]} to concatenate all the file names into one long
    # string, and override $IFS so they're joined with commas.
    (IFS=','; "${files[i]}" "${others[*]}")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download