Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Bash variables storing the output of commands run them at each variable statement, not just at some eventual invocation
Tags bash
Meaning that a script line such as:
nowstatus=`mpc | grep '#' | cut -d '/' -f1`
Is running mpc, grep and cut at its very statement, not only from an eventual "echo $nowstatus".
Therefore, in scripts that parse differently the same output of a given command, it may be preferable to first store this output in a temporary file, and then parse this file rather than running the same command several times. Such as in the lines added below their equivalent commented lines:
The command seems to run regardless of the variable storing the output in the format of var=`command`, or var=$(command), or yet var=(`command`).
nowstatus=`mpc | grep '#' | cut -d '/' -f1`
Is running mpc, grep and cut at its very statement, not only from an eventual "echo $nowstatus".
Therefore, in scripts that parse differently the same output of a given command, it may be preferable to first store this output in a temporary file, and then parse this file rather than running the same command several times. Such as in the lines added below their equivalent commented lines:
Code:
mpc > /dev/shm/mpcout # nowwhat=`mpc | head -n 1 | sed -e 's/\&/&/g;s/_//g;s/(//g;s/)//g;s/\#/ /g' ` nowwhat=`head -n 1 /dev/shm/mpcout | sed -e 's/\&/&/g;s/_//g;s/(//g;s/)//g;s/\#/ /g' ` # nowstatus=`mpc | grep '#' | cut -d '/' -f1` nowstatus=`sed -n '2p' /dev/shm/mpcout | cut -d '/' -f1 | sed 's|\[playing\]| ▶ |;s|\[paused\]| ▮▮ |' ` # whatnow=`mpc | tail -n 1 | sed 's|single.*||;s/\#/ /g'` whatnow=`tail -n 1 /dev/shm/mpcout | sed 's|single.*||;s/\#/ /g'`
Total Comments 0