I am working on a shell script to fetch data from DB and return results based on the input provided by user.
However I would like to limit the user to pass Maximum of a specific number (for eg. 10) comma separated values. Even if user has passed more than 10 values, my shell script should restrict the user and execute only 10 from the user provided list.
I'm able to fetch results for all user provided values, But not able to make out how to limit the same to a specific number.
My Shell Script
IDs=a1,b2,c3,d4,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14
# Above are User provided sample alphanumeric values
for i in $(echo $IDs | sed "s/,/ /g")
do
echo "Fetching results for $i by running sql against DB"
done
Expecting Results
I except shell script to fetch only first 10 comma separated values by ignore rest (In above eg. script need to pick values until from "a1" to "j10" and ignore rest) and execute the same against DB.
Any help would be greatly appreciated. Thanks in advance!
counter=0before starting your loop, putcounter=$(( counter + 1 ))inside the loop, and then[ "$counter" -gt 10 ] && breakor such in the loop to exit at the appropriate time. (Unlike the formal answer I provided, the above all works in baseline-POSIX shells).IFS=,earlier in your script,for i in $IDswill split on commas (though it has other bugs unless you turn off globbing).