shell tips and trick, part 1
Instead of another ssh guide, I thought it was about time for a first shell-scripting guide. In my daily job I often notice scripts that do work, but are not as efficient as could be. Say that I want the process id of the ssh daemon on my system. Loads of people will come up with something like:
ps -ef | grep sshd | grep -v grep | awk '{print $2}'
This does get the process id of sshd, but the above could also be written as:
ps -ef | awk '/sshd$/ {print $2}
And even this can still be written more efficient. Think about what you want to achieve. I asked for the process id of sshd. Basically the following would suffice:
cat /var/run/sshd.pid
Hey, I never really asked for a running sshd. But if I had asked for the process id of a running ssh daemon, the most simple solution would be:
ps -C sshd -o pid=
Or:
pgrep 'sshd'
Both just use one command instead of the four of the first example.