Bash · Techniques
Ask many files at once
A function asks one question of one file. xargs -P 4 runs it on four files at a time.
refund.json, a file the script reads
{
"decide": "Does the customer ask for a refund?",
"true": "The customer asks for money back.",
"false": "Anything else, such as a question or a complaint."
}tickets/T-1.txt, a file the script reads
I was charged twice. Can you fix this?
tickets/T-2.txt, a file the script reads
I was charged twice this morning. What are my options?
tickets/T-3.txt, a file the script reads
I would like to return this and get my money back.
tickets/T-4.txt, a file the script reads
The jug arrived cracked. What happens now?
tickets/T-5.txt, a file the script reads
This is the second broken jug. I am done with this company.
Two tickets ask for money back: T-1 and T-3.
asks_for_refund() {
thinkthen decide @refund.json \
--quiet \
--input "$1" &&
echo "$1"
}
export -f asks_for_refund
ls tickets/*.txt |
xargs -P 4 -I {} bash -c 'asks_for_refund {}' |
sortOutput
tickets/T-1.txt tickets/T-3.txt
exit 0
On GitHub: github.com/botassembly/thinkthen