Checking app status on CLI

i have code that will return either:
RUNNING
STOPPED
or return blank if the app does not exist, that code is

midclt call app.query '[["name", "=", "app_name"]]' | jq -r '.[] | .state'

my issue is, that when passing the app_name as a variable, the command always returns an empty response, but when i hard code the app name, it works as expected.

below is an example, when i load “chromium” into the variable “name” and pass it to the command, it returns nothing, but the hard coded worked

root@truenas[/mnt/volume1/logging]# name="chromium"; midclt call app.query '[["name", "=", "${name}"]]' | jq -r '.[] | .state'
root@truenas[/mnt/volume1/logging]# midclt call app.query '[["name", "=", "chromium"]]' | jq -r '.[] | .state'
RUNNING

any ideas on why the command does not seem to allow passing it a variable?

That is how bash works. String interpolation is disabled inside single quoted fragments.
It’s easy to see what happens if you use echo in front of your command.

$ name="chromium"; echo midclt call app.query '[["name", "=", "${name}"]]'
midclt call app.query [["name", "=", "${name}"]]

Notice how there is no interpolation taking place.

Here’s how to fix it:
midclt call app.query '[["name", "=", "'${name}'"]]'

1 Like

oh my god, i know about the string interpolation, it was staring me in the face and just did not see it!

thanks, i’m an idiot.

1 Like