0

So I am trying to get sessions from tmux ls, the output is something like this:

mc: 1 windows (created Mon Jun 17 15:53:39 2019) [108x37]
test: 1 windows (created Mon Jun 17 15:53:55 2019) [108x37]

So I pass it to grep in my script but it tries to execute it I guess,

#!/bin/sh

tmux ls | while read x;
do
        TEST=$("$x" | grep -P '^.*?:')
        echo "$TEST"
done

The output is this:

root@server0:/home/mc# ./test.sh
./test.sh: 5: ./test.sh: mc: 1 windows (created Mon Jun 17 15:53:39 2019) [108x37]: not found

./test.sh: 5: ./test.sh: test: 1 windows (created Mon Jun 17 15:53:55 2019) [108x37]: not found

Can someone tell me the problem? I couldn't find any solution because I don't know what to search for that problem.

Guven Degirmenci
  • 684
  • 7
  • 16
  • 2
    You're getting that error message because you're trying to execute the line you read from the `tmux ls` output instead of printing it but you have more problems than that. What's the specific expected output from your script? [edit] your question to show it so we can best help you. – Ed Morton Jun 17 '19 at 17:31
  • 3
    Not sure but should it not be `$(echo "$x" | grep -P '^.*?:')` – Lawrence Cherone Jun 17 '19 at 17:32
  • 1
    @LawrenceCherone that would stop him from getting the current error message but it'd still leave him with the wrong approach to whatever he's trying to do. – Ed Morton Jun 17 '19 at 17:33
  • 2
    Use `tmux ls -F "#S"` to output *just* the session names; then you don't need to use `grep` at all. – chepner Jun 17 '19 at 17:43

1 Answers1

0

You're getting that error message because you're trying to execute the line you read from the tmux ls output instead of printing it but you have more problems than that.

It's not clear what you're trying to do but if it's to get mc and test from the tmux ls output you show then that'd be:

tmux ls | cut -d':' -f1

assuming tmux ls doesn't have an option to just output those values (check the man page).

If that's not all you need then edit your question to provide clearer requirements plus the expected output for your posted sample input.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185