Using cat in conky_parse doesn't work

0

I'm trying to show the name of my shell in conky. I can capture the name of the shell using this command:

cat /proc/$$/comm

But when I use this in my script.lua file, like this:

text = 'Shell: ' .. conky_parse('${execi 3600 cat /proc/$$/comm}')

It only shows the word cat:

enter image description here

How can I fix this?

Amir A. Shabani

Posted 2019-08-11T13:57:11.207

Reputation: 171

Answers

0

I think this is due to an optimisation done by the shell. Your code will eventually run

sh -c 'cat /proc/$$/comm'

and the shell will expand the $$ to the shell's pid, then decide that since there are no more commands to do, it will exec() the cat rather than forking to exec and waiting for the child.

You can see this if you run the above command, which replies cat. But if you add another command, eg

sh -c 'cat /proc/$$/comm; echo $$'

you will see sh and the second command. A quick fix is to use < to make the shell open the proc file before running cat:

'${execi 3600 cat </proc/$$/comm}'

If you want to see the difference, use strace -f -o /tmp/trace sh -c '...' and look through the trace output.


Unfortunately, conky will alway run commands using sh rather than any SHELL value you may have set in the environment. So you will always find sh unless it is a link to some other shell, such as bash. The easy answer is to:

 ${execi 3600 echo $SHELL}

or if you need to really execute the shell then:

 ${execi 3600 exec $SHELL -c 'cat /proc/$$/comm;echo -n'}

meuh

Posted 2019-08-11T13:57:11.207

Reputation: 4 273

Using cat < /proc/$$/comm will print sh, but my shell is zsh. – Amir A. Shabani – 2019-09-11T12:27:13.667

It seems conky always runs sh. See my update. – meuh – 2019-09-11T13:35:15.463

This works, but I can manually change the value of $SHELL, for example like SHELL="bash", to something else and the behavior changes. So I guess it doesn't differ from using $(execi 3600 echo $SHELL). – Amir A. Shabani – 2019-09-11T14:20:24.700