LinuxQuestions.org
Review your favorite Linux distribution.
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 08-14-2009, 05:37 PM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
[bash] if statement options.


I am reading right now Advanced Bash Scripting.

in there there is a code that looks like this:

Code:
if [ -z "$1" -o ! -r "$1" ]
then
  directory=.
else
  directory="$1"
fi
what i dont understand is the option "-r"

and when i look for "bash if tests" or "bash if comparison" I get nothing specific for that option.

Can you guys clarify the purpose of "-r" to me, and give me a list of the if options if possible.
 
Old 08-14-2009, 05:59 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
 -r FILE
	      FILE exists and read permission is granted
it's actually man test you want.

[ ] is a synonym for /usr/bin/test
 
Old 08-14-2009, 06:20 PM   #3
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by bigearsbilly View Post
it's actually man test you want.

[ ] is a synonym for /usr/bin/test
I never realized before that /usr/bin/test existed. But test is also a bash builtin. It is described in bash's man page under SHELL BUILTIN COMMANDS. The description for test describes how to combine "primaries" and further advises the reader:

Quote:
Expressions are composed of the primaries described above under CONDITIONAL EXPRESSIONS.
So in the bash man page you would need to look under CONDITIONAL EXPRESSIONS for the information you seek. Admittedly, there is so much information in the bash man page that it can be a real pain to navigate.

EDIT: I probably should have mentioned that bigearsbilly's answer to what -r means in this case is still correct.

Last edited by blackhole54; 08-14-2009 at 06:23 PM.
 
Old 08-14-2009, 06:31 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Actually, bash uses its own built-in test (try "type test" or "type ["). But the operation of it is almost exactly the same as the external tool, so the man page still works as a reference.
 
Old 08-14-2009, 07:21 PM   #5
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
As a builtin, you can also use: help test
 
Old 08-15-2009, 05:51 AM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Thanks guys for all your replays... I did read in abs that ____[dont remember what now.] was synonymous of "test" but I didnt realize that it was EVERYTHING between []...

now all makes sense!

Thanks for the help, now i have much to read.
 
Old 08-15-2009, 07:56 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
No....it is the "[" that is synonymous with "test". IIRC, the closing "]" is not required.

The stuff between "[]" is the arguments to test...

Yes, I am a nitpicker........
 
Old 08-15-2009, 08:09 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello pixellany

I'm glad you brought that up because it has been an unresolved uncertainty for me for a long time; unresolved because IIRC [[ ]] has advantages over [ ] leaving no reason to use [ ] and hence no reason to understand it. The pragmatism of a young man in a hurry!

The closing ] is necessary, though. According to the Gnu bash Reference for test, "When the [ form is used, the last argument to the command must be a ]". That's kludgy!
 
Old 08-15-2009, 06:07 PM   #9
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by catkin View Post
... unresolved because IIRC [[ ]] has advantages over [ ] leaving no reason to use [ ] and hence no reason to understand it.
I am not sure if there is anything [...] can do that [[...]] cannot or not (a lot of nuances that would need to be checked out). If you have something that works for you, fine. But people who read this thread, particularly those that are just starting out, should realize that [[...]] is not a drop-in replacement for [...]. Of the top of my head, the == operator is interpreted differently and the 'logical and" and "logical or" operators are expressed differently. There may be other differences.

Last edited by blackhole54; 08-15-2009 at 06:09 PM.
 
Old 08-17-2009, 12:30 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For [[ ]] vs [ ] see here: http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
The former is more robust/allows operators that will have syntax errs in the latter.
Handles null args better.
 
  


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
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 11:43 AM
BASH IF statement kinetik Programming 10 05-07-2006 02:48 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
bash statement os2 Programming 2 03-20-2005 10:13 PM

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

All times are GMT -5. The time now is 02:03 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