LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Check status (https://www.linuxquestions.org/questions/linux-newbie-8/check-status-4175535644/)

linuxmantra 03-03-2015 10:22 AM

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.

veerain 03-03-2015 10:46 AM

echo "$sysinfo" shows what's in sysinfo variable.

instead or along with you should run:

Code:

"$sysinfo"
or
"$sysinfo" --version

then check with:

Code:

if [ "$?" = 0 ];then
echo "sysinfo installed"
else
echo "Please install sysinfo"
fi

Note you should replace --version with an argument for which your sysinfo executable produces zero exit status. Checked with an immediate 'echo $?' command.

michaelk 03-03-2015 12:39 PM

Have you thought about checking to see if the file exists?

TB0ne 03-03-2015 12:46 PM

Quote:

Originally Posted by linuxmantra (Post 5326249)
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.

You need to perform a basic file-exists check. This is VERY well documented in many bash scripting tutorials..the best suggestion would be for you to go through those tutorials. In short, something like this:
Code:

if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

...works. Replace the /tmp/foo.txt with whatever file you want to look for. If it's not there, it returns that message. Modify as needed. Actually RUNNING the sysinfo program is fairly pointless in this context.

linuxmantra 03-03-2015 02:15 PM

Quote:

Originally Posted by TB0ne (Post 5326341)
You need to perform a basic file-exists check. This is VERY well documented in many bash scripting tutorials..the best suggestion would be for you to go through those tutorials. In short, something like this:
Code:

if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

...works. Replace the /tmp/foo.txt with whatever file you want to look for. If it's not there, it returns that message. Modify as needed. Actually RUNNING the sysinfo program is fairly pointless in this context.


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:

Originally Posted by michaelk (Post 5326335)
Have you thought about checking to see if the file exists?


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

TB0ne 03-03-2015 02:55 PM

Quote:

Originally Posted by linuxmantra (Post 5326403)
Below is my script:
Code:

#!/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?

Please use CODE tags when posting code, and there are MANY ways to write such a script. Since you have a 'certification', and you've been here two years now, I'm sure you've seen the MANY references to the bash scripting tutorials on TLDP. As you were advised before, you should REFERENCE those tutorials, and read up on how to write scripts.
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.

michaelk 03-03-2015 03:22 PM

Quote:

Any thoughts or better way of scripting?
As TB0ne posted checking if the file exists or not is simpler but there are many ways to accomplish the same thing.

ntubski 03-03-2015 03:31 PM

`` is the inverse of echo, so instead of this
Code:

ev=`/bin/echo $?`
just write
Code:

ev=$?
And you might also consider using $? directly instead of assigning it to another variable.

Habitual 03-03-2015 03:59 PM

Quote:

Originally Posted by linuxmantra (Post 5326403)
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

What's with the $ev assignment? What's that for?

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
SysInfoPath="/usr/local/bin/sysinfo"
if [[ -e $SysInfoPath ]]
then
  echo "$SysInfoPath exists."
else
        echo "$SysInfoPath does NOT exist."
fi

if [[ -f $SysInfoPath ]]
then
  echo "$SysInfoPath IS a regular file."
else
        echo "$SysInfoPath is NOT a regular file."
fi

if [[ -x $SysInfoPath ]]
then
  echo "$SysInfoPath IS an executable file."
else
        echo "$SysInfoPath is NOT an executable file."
fi

How to wrap all 3 tests up into one nice and neat little package?
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.

ntubski 03-03-2015 04:02 PM

Quote:

Originally Posted by Habitual (Post 5326454)
What I'd test for is...
Does the file exist?
Is it a regular file?
Is it an executable?
...
How to wrap all 3 tests up into one nice and neat little package?

Not needed, just test for executable since it implies the first 2.

Habitual 03-03-2015 04:35 PM

Quote:

Originally Posted by ntubski (Post 5326456)
Not needed, just test for executable since it implies the first 2.

I knew it was simple. Thanks!
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.