1

I want do a batch job with systemctl, something like:

systemctl status v2ray-haproxy@{1..5}

That's fine. But when i change 5 to a variable:

n=5;systemctl status v2ray-haproxy@{1..$n}

It's not working now, and error:

Invalid unit name "v2ray-haproxy@{1..5}" was escaped as "v2ray-haproxy@\x7b1..5\x7d" (maybe you should use systemd-escape?) Unit v2ray-haproxy@\x7b1..5\x7d.service could not be found.

How to get this variable code work?

Rampage
  • 11
  • 2
  • You might want to remove systemctl from the title - as this is a bash question. – davidgo Mar 24 '20 at 06:34
  • 2
    This is a cross-site duplicate of ["Brace expansion with variable?"](https://stackoverflow.com/questions/19432753/brace-expansion-with-variable) (and many others) on stackoverflow. – Gordon Davisson Mar 24 '20 at 17:35
  • @GordonDavisson Thanks, `n=5;for i in $(seq 1 $n); systemctl status v2ray-haproxy@$i; done` working! – Rampage Mar 25 '20 at 03:07

1 Answers1

0

try something like below

n=5;systemctl status v2ray-haproxy@{1..$(seq 1 $n)}
asktyagi
  • 2,401
  • 1
  • 5
  • 19
  • not work: `Failed to get unit for PID 2: PID 2 does not belong to any loaded unit. Failed to get unit for PID 3: PID 3 does not belong to any loaded unit. Failed to get unit for PID 4: PID 4 does not belong to any loaded unit. Invalid unit name "v2ray-haproxy@{1..1" was escaped as "v2ray-haproxy@\x7b1..1" (maybe you should use systemd-escape?) Invalid unit name "5}" was escaped as "5\x7d" (maybe you should use systemd-escape?) Unit 5\x7d.service could not be found.` – Rampage Mar 25 '20 at 03:03