I want to execute the following in a bash script:
num_total_keys=0x1FFFF
for ((i = 0; i <= $num_total_keys; i ))
do
redis-cli SET some_prefix:$i True > /dev/null
done
When I execute this, however, it takes a really long time. Is there a more efficient way to do this?
CodePudding user response:
Rule of thumb:
If your command accept pipelined data/instructions; do not run your command repeatedly in a shell loop, but build all of it before piping to your command as a single call like this:
#!/usr/bin/env sh
i=131071
while [ $i -ge 0 ]; do
printf 'SET some_prefix:%d True\n' $i
i=$((i - 1))
done | redis-cli --pipe
Alternatively using Bash's brace expansion:
printf 'SET some_prefix:%d True\n' {131071..0} | redis-cli --pipe