Score:0

Output of `top` command not saved to variable

fr flag

I created the follow bash script, called ./notify-me.sh, in Ubuntu:

#!/bin/bash

emailmessage=$(top -b -o %CPU)
echo $emailmessage

But when I run this script by doing ./notify-me.sh and pressing the Enter key, the command prompt just skips to the next line and does nothing. It just stays on the empty line until I press Ctrl+C to break the process.

How do I store the output of the top command to a variable?

hr flag
Without a `-n` value, `top` will keep running even in batch mode (`-b`) - if you want a single-shot of output add `-n 1`
fr flag
that worked! ``` #!/bin/bash emailmessage=$(top -n 1 -o %CPU) echo "$emailmessage" ```
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.