Sample Header Ad - 728x90

Pass multiple files as a single option

5 votes
4 answers
882 views
I'm trying to build a wrapper to execute a tool multiple times, then concatenate some of the results. I'd like to pass two sets of files to my wrapper script, then run the tool for each pair of files. I'd like it to behave like this: multitool.sh -a a*.txt -b b*.txt (expanding the wildcards to match all files available) Then inside multitool.sh, I run the tool on a1.txt b1.txt, a2.txt b1.txt, a1.txt b2.txt, a2.txt b2.txt, etc, with variable numbers of a and b files. I followed [this](https://www.redhat.com/en/blog/arguments-options-bash-scripts) tutorial explaining the basics of option handling, and I'm able to use getops to handle a -h, but nothing else. Here is where I'm at now:
#!/bin/bash


while getopts ":hpg:" option; do
	case $option in 
		h) # display help
			echo "-h to print this help and exit, -p is peak files, -g is bedgraph files."
			exit;;
		p) # get list of peak files to process
			l_peaks=$OPTARG;;
		g) # get list of bedgraph files to process
			l_bgraphs=$OPTARG;;
		\?) # Invalid option
			echo "Error! Invalid option!"
			echo "-h to print this help and exit, -p is peak files, -g is bedgraph files."
			exit;;
	esac
done

echo "$l_peaks"
echo "$l_bgraphs"
I'm working with a team who aren't particularly computer literate, so if I can keep the wrapper to a simple one line execution, that would be best. How can I pass these lists of files as one option each?
Asked by Whitehot (245 rep)
Feb 24, 2025, 01:09 PM
Last activity: Feb 25, 2025, 07:08 AM