How to use shell operators like `&&` and `;` in C code

209 views Asked by At

I'm trying to make my status bar of DWM window manager to update whenever I change audio volume and keyboard layout. After searching the net, I found a way for keyboard switching:

static const char *kblayoutnext[]  = { "pkill", "-HUP", "dwm_bar.sh", NULL };

When I press the key to switch layout, using pkill and HUP signal the status bar is reloaded presenting the new setting.

For audio, this works for changing volume:

static const char *alsatoggle[]  = { "amixer", "-q", "set", "Master", "toggle", NULL };

but no refreshing on the status bar.

So I tried,

static const char *alsatoggle[]  = { "amixer", "-q", "set", "Master", "toggle", "&&", "pkill", "-HUP", "dwm_bar.sh", NULL };

and

static const char *alsatoggle[]  = { "amixer", "-q", "set", "Master", "toggle", ";", "pkill", "-HUP", "dwm_bar.sh", NULL };

but nothing happens; the alsa volume level does not change, and no refreshing of course. I suspect that the bash && and ; are not passed as they should inside C code.

1

There are 1 answers

2
Mark On BEST ANSWER

I had the same idea as Eric. The issue with ; and && is that they are interpreted by the shell. So, as Eric suggests, run the shell.

Consider this:

static const char *alsatoggle[]  = { "sh", "-c" , "amixer -q set Master toggle && pkill -HUP dwm_bar.sh", NULL };