There is no supported way to run elasticsearch-setup-passwords in batch mode. It is a convenience utility for manually configuring a node.
If you want to automate this, then you should use the change password API.
In which case you need to configure the bootstrap password to be a value that you do know.
Something roughly like this will work (but for production usage you'd want to build something more reliable)
# Configure Bootstrap Password
BootstrapPassword="$( generate-random-password )"
printf "%s" "$BootstrapPassword" \
| bin/elasticsearch-keystore add -x "bootstrap.password"
# Start ES & Wait for it to be available
bin/elasticsearch -d
while true
do
curl --fail -u "elastic:$BootstrapPassword" \
"http://localhost:9200/_cluster/health?wait_for_status=yellow" \
&& break
sleep 5
done
# Set passwords for various users
for User in "kibana" "logstash_system" "apm_system" "beats_system" "elastic"
do
UserPassword="$( generate-random-password )"
curl -u "elastic:${BootstrapPassword}" \
-XPOST "http://localhost:9200/_xpack/security/user/${User}/_password" \
-d'{"password":"'"${UserPassword}"'"}' -H "Content-Type: application/json"
printf "%s=%s\n" "$User" "$UserPassword" >> passwords.txt
done