LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2015, 10:22 AM   #1
linuxmantra
Member
 
Registered: Dec 2013
Posts: 111

Rep: Reputation: Disabled
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.
 
Old 03-03-2015, 10:46 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.
 
Old 03-03-2015, 12:39 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,937

Rep: Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825
Have you thought about checking to see if the file exists?
 
1 members found this post helpful.
Old 03-03-2015, 12:46 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,995

Rep: Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327
Quote:
Originally Posted by linuxmantra View Post
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.
 
Old 03-03-2015, 02:15 PM   #5
linuxmantra
Member
 
Registered: Dec 2013
Posts: 111

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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 View Post
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
 
Old 03-03-2015, 02:55 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,995

Rep: Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327Reputation: 4327
Quote:
Originally Posted by linuxmantra View Post
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.
 
Old 03-03-2015, 03:22 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,937

Rep: Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825Reputation: 1825
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.
 
Old 03-03-2015, 03:31 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,153

Rep: Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362
`` 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.
 
Old 03-03-2015, 03:59 PM   #9
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,488
Blog Entries: 11

Rep: Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386
Quote:
Originally Posted by linuxmantra View Post
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.
 
Old 03-03-2015, 04:02 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,153

Rep: Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362
Quote:
Originally Posted by Habitual View Post
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.
 
Old 03-03-2015, 04:35 PM   #11
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,488
Blog Entries: 11

Rep: Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386Reputation: 2386
Quote:
Originally Posted by ntubski View Post
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'.

Last edited by Habitual; 03-03-2015 at 04:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I check the server status? baesan Linux - Server 2 11-30-2014 07:49 PM
Check HD status ust Linux - Hardware 4 02-04-2013 10:56 AM
How to check RAID status? tikit Linux - Hardware 5 07-16-2009 03:37 PM
Check disk status diezjc Linux - Hardware 2 04-06-2006 03:58 PM
Check Printer Status Debby Linux - General 5 02-08-2002 08:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration