Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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
#!/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.
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?).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.