1

I have the following Linux command which gives metrics like CPU%, RAM%, and Hard disk percentage.

echo "CPU `LC_ALL=C top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'`% RAM `free -m | awk '/Mem:/ { printf("%3.1f%%", $3/$2*100) }'` HDD `df -h / | awk '/\// {print $(NF-1)}'`"

I want to store it in a variable as a string. But I'm not able to store due to the usage of `,",' in the Linux command. How can i make it work?

Note: I'm storing this as a string in a variable because I want to check metrics like CPU%, RAM%, and Hard disk percentage by doing ssh to a server and executing the above command with the help of pexpect/pxssh library in python.

rakesh kotian
  • 232
  • 1
  • 5
  • 30
  • 2
    Note that this command is very unnecessarily complicated. Just one `awk` invocation can do all the work you're relying on `grep` and `sed` for, and on most platforms `top` isn't a good choice for noninteractive use in the first place. – Charles Duffy Oct 10 '19 at 15:31
  • 2
    Also, using backticks adds a lot of complications you wouldn't have using the modern (published as part of the POSIX sh standard in 1991) `$(...)` command substitution syntax. – Charles Duffy Oct 10 '19 at 15:32
  • My easy tip is use triple quote on variable string: s = """ you comand " with any quote' " more command """ . Its used for multi line string – Wonka Oct 10 '19 at 15:33
  • 1
    @Wonka, not just *any* triple-quoted string works here; it has to be a *raw* triple-quoted string because the literal string contains backslash literals. – Charles Duffy Oct 10 '19 at 15:34
  • @CharlesDuffy Thanks for the better suggestion regarding the Linux command i have used. So as you have suggested to me, Can you please comment here with a single efficient Linux command which gives me usage percentage of CPU, RAM, and Hard disk. – rakesh kotian Oct 10 '19 at 15:43
  • @rakeshkotian, why don't you ask a question about how to do that at [unix.se], keeping your current code but also adding a full example of your expected output, specifying which distros compatibility is expected for, and otherwise asking the question in such a way as to be fully answerable? Feel free to @ me in, and I may take a shot at it there. – Charles Duffy Oct 10 '19 at 15:48
  • @CharlesDuffy please take a look here [Unix&Linux](https://unix.stackexchange.com/questions/546179/single-efficient-linux-command-to-check-the-cpu-ram-and-the-hard-disk-usage-per) – rakesh kotian Oct 10 '19 at 16:26
  • That's missing the current code (granted, I already have it from this question), and more importantly, it's missing an example of exact expected output. – Charles Duffy Oct 10 '19 at 16:29
  • @CharlesDuffy question is updated now. please check [Unix/Linux](https://unix.stackexchange.com/questions/546179/single-efficient-gnu-linux-command-to-check-the-cpu-ram-and-the-hard-disk-usage) – rakesh kotian Oct 11 '19 at 06:14

2 Answers2

1

Triple-quoted raw strings are perfect for this purpose

cmd = r'''
echo "CPU `LC_ALL=C top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'`% RAM `free -m | awk '/Mem:/ { printf("%3.1f%%", $3/$2*100) }'` HDD `df -h / | awk '/\// {print $(NF-1)}'`"
'''
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • This will add `\n`'s at the beginning and the end of the string, you should put the triple-quotes on the same line than the command. – Silveris Oct 10 '19 at 15:34
  • 2
    @Silveris, those newlines don't change the shell command's meaning; I actively recommend their use (as unlike a semicolon, they'll be treated as a command separator only when necessary and appropriate, so can be used when multiple shell commands may or may not be combined). – Charles Duffy Oct 10 '19 at 15:34
  • @CharlesDuffy True, but it could be useful to note it in case of a different purpose. – Silveris Oct 10 '19 at 15:35
0

You can just escape your quotes and double quotes with \

ymochurad
  • 941
  • 7
  • 15
  • 2
    Not actually sufficient for the OP's specific use case here. Inside backticks (unlike the modern replacement `$( ... )`), backslashes behave differently and need to be doubled-up. And of course the literal backslashes *also* need to be escaped at the Python-syntax level if you aren't using a raw string, above and beyond the shell-level escaping needed. – Charles Duffy Oct 10 '19 at 15:33
  • ...it's much more helpful, when answering a question, to build a specific tested implementation of your proposed approach -- testing your answer means you discover any details that are relevant to the OP but were missed. – Charles Duffy Oct 10 '19 at 15:42
  • @CharlesDuffy that is true because I tried to just escape quotes and double quotes with \ but that did not work in this case. – rakesh kotian Oct 10 '19 at 15:47