&& and || operators


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting && and || operators
# 1  
Old 09-26-2015
Question && and || operators

Hello all,

i am curious how we can implement the ternary operator with multiple commands in else part. in my case, only the first command in the "else" part is reachable and the rest is ignored.
Code:
[[ `$adb_path devices|wc -l` -gt 2 ]] && echo "there's a device connected" || (echo "please connect a device" && exit -1) 

[[ `$adb_path devices|wc -l` -gt 2 ]] && echo "there's a device connected" || (echo "please connect a device";exit -1)

in both the cases, the
Code:
exit

is not reachable always and the code continues without exiting.

please help me how to achieve this with this syntax. It can be easily rewritten using if-else construct. But, I would like to know how we can accomplish this using this syntax.

Thanks
# 2  
Old 09-26-2015
The && and || operators are not a replacement for a proper if - else - fi block".

While one can use subshells, as you did, to do additional tasks if a false has returned, being a subshell, you cannot exit from there, as it just exits the subshell, not the script.

Code:
echo main shell pid: $$
false || (echo "subshell pid: $$" ; exit 1 )
exit 0

Now try:
Code:
function myText() { # STRING
# Prints the STRING 
# then exits
	local pid="${1:-$$}"
	echo "Subshell pid: $pid"
	# to end the script, use 'return 0|1' to return from function to script
	exit 1 
}

echo "main shell pid: $$"
false  || myText "$$"

Check if of the if blocks exit status, by executing:
Code:
echo $?

After you ran the script.

hth

EDIT:
Simply said, the && and || operators are only good for SIMPLE checks, not for complex checking.
MAinly because one cannot use multiple instances of ||, if ANY previous condition fails, it'll go to the first ||, and be done after.
This User Gave Thanks to sea For This Post:
# 3  
Old 09-27-2015
As sea said, your exit commands are exiting the subshell in which they're located; not the parent shell... But, you can reverse the condition and use multiple && operators with a single || operator. Try:
Code:
[[ $($adb_path devices|wc -l) -le 2 ]] && echo "please connect a device" && exit -1 || echo "there's a device connected"

However, just because you CAN do it this way doesn't mean that something more like:
Code:
if [[ $($adb_path devices|wc -l) -le 2 ]]
then	echo 'please connect a device'
	exit -1
fi
echo "there's a device connected"

wouldn't be better. This form is easier to read for most code readers and should run as fast or a tiny bit faster than the single line version.

Despite what sea said, a long command line can have more than one meaningful || operator if conditional code follows the first ||. For example:
Code:
x=$((number % 3))
[[ x -eq 0 ]] && echo 0 || [[ x -eq 1 ]] && echo 1 || echo 2


Last edited by Don Cragun; 09-27-2015 at 02:05 AM.. Reason: Fix typo: s/you're/your/
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-27-2015
To add: you can simply replace the subshell construct with code grouping
Code:
[[ $($adb_path devices|wc -l) -gt 2 ]] && echo "there's a device connected" || { echo "please connect a device";exit -1;}

Note the space after the first curly brace and the semicolon before the second one..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-27-2015
Quote:
Originally Posted by Scrutinizer
To add: you can simply replace the subshell construct with code grouping
Code:
[[ $($adb_path devices|wc -l) -gt 2 ]] && echo "there's a device connected" || { echo "please connect a device";exit -1;}

Note the space after the first curly brace and the semicolon before the second one..

Awesome, Worked like a charm. Thanks a lot.
# 6  
Old 09-27-2015
NB if the echo "there's a device connected" is run then it is evaluated and must give a true (exit 0) status, otherwise the || part would run, too.

Well, a simple echo can hardly fail. But an if-then-else is cleaner.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

GNU & BSD Makefile Directives & Conditions Compatibility

Firstly, I would like to apologize if this is not the appropriate sub-forum to post about GNU/BSD makefile scripting. Though my code is in C++, because I am focusing on the makefile I thought it would go better in shell scripting. Please correct me if I am wrong. Secondly, I am not interested in... (0 Replies)
Discussion started by: AntumDeluge
0 Replies

3. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

4. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

5. Shell Programming and Scripting

replace & with & xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

8. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

9. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

10. Shell Programming and Scripting

creating & sending formatted (with bolds & colors) CSV

Hi , I have a situation. Need is to create & send a formatted file with header in BOLD & colored & some sequel results as a content. I know echo -e \033 command, but its scope is limited in PUTTY. How to retain the formatting out of Putty; say after someone opens a email attachment... (2 Replies)
Discussion started by: infaWorld
2 Replies
Login or Register to Ask a Question