Check status
I am writing a script to check whether a "sysinfo" is installed in a system or not. sysinfo is a tool that gives information about the system. Its is installed in /usr/local/bin. so at CLI when I type sysinfo, it will display output.
#!/bin/bash sysinfo=`/usr/local/bin/sysinfo` echo "$sysinfo" if [ "$?" = 0 ];then echo "sysinfo installed" else echo "Please install sysinfo" fi Concept is, I run the command if it is present, it will show output and exit status of command is 0 else it is not installed. But it is not working as expected. Any thoughts , idea, suggestion. |
echo "$sysinfo" shows what's in sysinfo variable.
instead or along with you should run: Code:
"$sysinfo" Code:
if [ "$?" = 0 ];then |
Have you thought about checking to see if the file exists?
|
Quote:
Code:
if [ ! -f /tmp/foo.txt ]; then |
Quote:
Below is my script: #!/bin/bash /usr/local/bin/sysinfo > /dev/null ev=`/bin/echo $?` if [ "$ev" -eq 127 ]; then echo "Sysinfo NOT Installed" else echo "Sysinfo Installed" fi It works fine now.. Any thoughts or better way of scripting? ---------- Post added 03-03-15 at 03:15 PM ---------- Quote:
My script: #!/bin/bash /usr/local/bin/sysinfo > /dev/null ev=`/bin/echo $?` if [ "$ev" -eq 127 ]; then echo "Sysinfo NOT Installed" else echo "Sysinfo Installed" fi |
Quote:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html http://www.tldp.org/LDP/abs/html/ The short answer is, there is no 'best' way to write it...if it works and you're happy with it, then you're done. Otherwise, keep writing it...everyone would write a script differently, and they'd all do the same thing. No one is 'right' or 'wrong' in those instances. |
Quote:
|
`` is the inverse of echo, so instead of this
Code:
ev=`/bin/echo $?` Code:
ev=$? |
Quote:
Test the file's existence directly using the bash test "-e". What about a non 'default' (not where you expected) installation? How are you going to test for that condition? What else to test for since Directories are files to Linux? /usr/local/bin/sysinfo could be a directory, then what? What I'd test for is... Does the file exist? Is it a regular file? Is it an executable? using Code:
#!/bin/bash I'll leave as an exercise in "Personal Growth" (and mostly because I don't know how I'd do that part exactly?). Mileage may vary based on driving conditions. |
Quote:
|
Quote:
and "Yes, but..." It could exist and NOT be the binary for the program...? I love bash, makes me 'think'. :) |
All times are GMT -5. The time now is 07:00 AM. |