ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 trying to run a script which basically runs a command.. and if that command returns two lines then it does nothing.. but if it returns one line then it runs another command..
In other words, basically the script runs a command if it sees only one line return, if it sees more than one, do nothing.. (normally it will return only two lines)
I could capture the return status of the command but then it will always be true.. so that method won't work here..
How do I capture the fact that it has returned two lines and not one.. and so how do I modify this script below (I know it is wrong in itself in terms of rules)..
Code:
#!/bin/bash
ps -ef | grep script
if [ "$?" != "0" ]
run command
You seem to be over-complicating things a bit. The if is testing for success, so if pgrep returns something it will execute your new command. As you actually want the opposite of this you will
need to negate the outcome supplied to if.
Also, as pgrep outputs to the console and you are not interested in the output (only if there is any), I would suggest redirecting the output to /dev/null
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.