I am trying to write bash script which will read multiple filenames and a target directory, which is optional.
./myfile -t /home/users/ file1 file2
I have tried the following code, but I am not able to handle different scenarios mentioned below:
while getopts "t:" opt; do
case $opt in
t)
echo "-t was triggered, Parameter: $OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
But the code should handle different scenarios like:
./myfile file1 file2 -t /home/users/
,
./myfile file1 -t /home/users/ file2 file3
,
./myfile file1 file2 file3 file4
and should be able to read the files.