Given that this is Bash, we can use mapfile
to create an array from the file contents:
mapfile server </tmp/server-list.txt
Then we can loop over these servers:
for s in "${server[@]}"
do
#something
done
Now we need to use the value of $s
when we write the code where I put the placeholder "#something
" (this can be a little tricky, because the "command" arguments to ssh
are combined into a string that's passed to sh -c
on the server):
ssh "root@$s" "userdel -f '${USERID//\'/\'\\\'\'}''"
(Assuming $USERID
is set somewhere outside the script; that tricky substition is to deal with '
characters that would otherwise confuse the remote shell)
Putting it all together:
read -p "Delete which user?" userid
mapfile server </tmp/server-list.txt
for s in "${server[@]}"
do
ssh "root@$s" "userdel -f '${userid//\'/\'\\\'\'}''"
done
$USERID
being set? You should share your whole script and tell us the whole picture. This is not answerable.