1

I have a linux based command named as x2goterminate-session followed by username e.g

x2goterminate-session john

But there are numerous user sessions that I want to terminate in a single go.

slpusrs=`x2golistsessions_root | grep '|S|' | cut -d "|" -f 2`

Above variable slpusrs finds the list of sleeping users and stores them in the slpusrs variable.

Now I want to execute x2goterminate-session command one by one on the list of users, so that all sleeping users are terminated in a single go instead of typing the command followed by user one by one.

command=x2goterminate-session
for i in "${slpusrs[@]}"; do
"$command" "$i"
done

But It isnt working. Please help

2
  • Does your command allow multiple usernames, i.e. x2goterminate-session bill john steve sue ? Commented May 11, 2020 at 20:23
  • @muhammadowaisKhaleeq : You reference slpusrs as array, but you set it as scalar. To set an array variable, you need slpusrs=( $(....) ) Commented May 12, 2020 at 8:17

3 Answers 3

2

slpusrs is a string, not an array. Use

for i in $slpusrs; do
    "$command" $i"
done
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of iterating with Bash, use xargs to run your command for each user.

x2golistsessions_root |
  grep '|S|' |
    cut -d "|" -f 2 |
      xargs -n1 x2goterminate-session

2 Comments

Thanks!! This worked for me.. Can u please tell that -n1 means only one argument for the command?
xargs defaults to passing as much arguments as it fiits to a single command call. But since your command may accept only one argument, -n1 tells xargs to pass one argument at a time.
0

If x2goterminate-session realls can work only on one user at a time, you can do a

for user in $(x2golistsessions_root | grep '|S|' | cut -d "|" -f 2)
do
  echo "Murdering unser $user ..."
  x2goterminate-session "$user"
done

No need to introduce a separate variable, unless you need the list of users later on again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.