LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2018, 01:52 PM   #1
rcroyal88
LQ Newbie
 
Registered: Jun 2018
Posts: 11

Rep: Reputation: Disabled
Using boolean condition and variable declaration


I want to check the multiple process id and then with boolean condition if process are up

var1=$(pgrep -f "/home/huy")
var2=$(pgrep -f "/home/gyu")

if [-n $var1]&&[-n $var2]&&[-n $var3] ;
then

echo "Working"

elif "not working"

fi


Variable decalarition is not working for me ,i am getting error.
 
Old 10-08-2018, 02:17 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,873
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Please quote the problematic variable-declaration, and the error-message.
 
1 members found this post helpful.
Old 10-08-2018, 02:44 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Please use code tags when posting code or output. It will preserve spaces in the code.

Also, please post the error you are getting. It's hard to help when we don't know what the error is.

A space is required between the [ or ] and the enclosed condition to be tested.

Last edited by scasey; 10-08-2018 at 02:46 PM.
 
1 members found this post helpful.
Old 10-08-2018, 02:51 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
As mentioned by others, to preserve formatting place your code snippets inside [CODE]...[/CODE] tags. You may type those yourself or click the "#" button in the edit controls.

When asking for help troubleshooting your code it is always helpful to post a minimal working example of code which produces an error or incorrect result, along with the actual error message it produces. Only you are sitting at the keyboard and can interact with it, we can only base advice on what you tell us.
 
Old 10-08-2018, 02:59 PM   #5
rcroyal88
LQ Newbie
 
Registered: Jun 2018
Posts: 11

Original Poster
Rep: Reputation: Disabled
Is there any limit how many boolean operatores can be used ?

Here i have used two times ,Can i use it more times as well in unix ???
 
Old 10-08-2018, 03:34 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
this is not even workable code
Code:
if [-n $var1]&&[-n $var2]&&[-n $var3] ;
then

echo "Working"

elif "not working"

fi
it should be
Code:
if [[ condition ]] && [[ condition ]] && [[ condition ]] ; 
then

echo "yeppers"

else

echo "noppers"
fi
you're thinking is in a two conditional testing,
Code:
if [[ condition ]] ; then
echo "something"
elif [[ condition ]]
echo "something"
else
echo "something"
fi
http://www.tldp.org/LDP/Bash-Beginne...l/chap_07.html

Last edited by BW-userx; 10-08-2018 at 03:37 PM.
 
Old 10-08-2018, 03:40 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by rcroyal88 View Post
Is there any limit how many boolean operatores can be used ?

Here i have used two times ,Can i use it more times as well in unix ???
You can chain as many booleans as necessary to express the desired condition.

It would be helpful to others if you would respond to the questions already asked. LQ members are happy to help you solve problems, but doing so requires two-way communication and respect for the time they volunteer to provide that help.

Last edited by astrogeek; 10-08-2018 at 03:41 PM. Reason: Added quote to remove ambiguity
 
Old 10-11-2018, 09:28 AM   #8
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
it should be
Code:
if [[ condition ]] && [[ condition ]] && [[ condition ]] ; 
then

echo "yeppers"

else

echo "noppers"
fi
The op never mentioned that bash is being used so its worth pointing out that [[ condition ]] with the double brackets is not portable. Additionally its not required in this context.
 
1 members found this post helpful.
Old 10-11-2018, 09:42 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
bash is the only one I've ever seen using squared bracket conditionals so it is implied ,and not explicit.
 
Old 10-11-2018, 10:13 AM   #10
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
I'm not sure what you mean?

Code:
if [ "${foo}" ]; then
  bar
fi
The above is fully portable, shells that it will work in include true bourne shells (i.e. hsh), ash, dash, bash, ksh, loksh, mksh, pdksh, posh, yash and zsh.
 
Old 10-11-2018, 10:52 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by orbea View Post
I'm not sure what you mean?

Code:
if [ "${foo}" ]; then
  bar
fi
The above is fully portable, shells that it will work in include true bourne shells (i.e. hsh), ash, dash, bash, ksh, loksh, mksh, pdksh, posh, yash and zsh.
not arguing about the portability part, just expressing my knowledge of bash and bash and bash. cheers@!
 
  


Reply



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
Format Preservation in Variable Declaration gmconklin Programming 4 05-10-2013 03:09 PM
Regarding .sh and variable declaration rushikeshgaradade Linux - Newbie 22 04-05-2013 12:31 AM
Variable Declaration in shell scripts... vinaytp Linux - Newbie 4 02-04-2010 10:39 AM
[SOLVED] [bash] assign boolean expression to variable hashbang#! Programming 10 08-16-2009 08:44 AM
Need to extract name of C variable from C declaration. judgex Programming 3 09-22-2007 12:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:05 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
Open Source Consulting | Domain Registration