January 29th, 2017, 07:37 PM
This Bash file seems inefficient
Rank amateur here. Good to see you all.
This Bash script does what I want it to do, but a quick look at it shows that it is doing the
same thing 3 times. This seems a bit inelegant and wasteful. I'm sure there is an easier
way to do this, but I have no idea what that might be.
Can some of you point me in the right direction? I would appreciate it.
Thank you for your time and attention.
# See if the application in question is running and dump output into a text file
ps aux|grep gkrellm > gkrellm_poll.txt
# Define some variables representing the ip addresses of the machines I'm testing for
# Check the text file for presence of the ip addresses expected.
# If present, move on, else, open up a gkrellm instance for that ip address.
# Do this for each ip address I'm looking for.
grep -Fq "$ADAMSMDK" gkrellm_poll.txt # Is the ip address found?
then echo "found $ADAMSMDK ADAMSMDK" # If so, move on, if not start a gkrellm instance
else ssh -p2222 -f -L 19150:192.168.1.100:19150 192.168.1.100 "gkrellm -f -s 127.0.0.1 -P 19150"
grep -Fq "$PVR" gkrellm_poll.txt
then echo "found $PVR PVR"
else ssh -p2222 -f -L 19150:192.168.1.103:19150 192.168.1.103 "gkrellm -f -s 127.0.0.1 -P 19150"
grep -Fq "$SHUTTLE" gkrellm_poll.txt
then echo "found $SHUTTLE SHUTTLE"
else ssh -p2222 -f -L 19150:192.168.1.105:19150 192.168.1.105 "gkrellm -f -s 127.0.0.1 -P 19150"
# Clean up the no longer needed text file.
rm -f gkrellm_poll.txt