LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-24-2009, 02:40 PM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
[BASH] confused with test.


I am reading the Advanced Bash scripting tutorial and I am in the tests section...

at the beginning it mentions that the use of "-a" is deprecated and instead we should use "-e" instead which checks if a file or folder exists.

but then in the example they have something like this:

Code:
linkchk () {
    for element in $1/*; do
      [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
      [ -d "$element" ] && linkchk $element
    # Of course, '-h' tests for symbolic link, '-d' for directory.
    done
}
now im little bit confused on how the test is going to come here:
Code:
[ -h "$element" -a ! -e "$element" ] && echo \"$element\"
I know that firts it checks if $element is a link, then it checks if exists and then it denies and then checks if $element exists?? wtf??

or is it that it checks whether or not it exists.... in any case why is he using "-a" anyways...? (probably to make you note that it works the same...)

Can you throw a little bit of light in here, im little lost!
 
Old 08-24-2009, 02:49 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The usage of -a as file test operator is deprecated because it can be confused with the logical AND which is -a as well. The expression above check if $element is a symbolic link and it does not exist. This mean it is a symbolic link pointing to a non-existent file.

Last edited by colucix; 08-24-2009 at 02:53 PM.
 
Old 08-24-2009, 03:05 PM   #3
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Ahh.... now it makes sense!

But they did not mention that -a is also the "and" operator!...

Another question... can you bind several tests like -rwx or you have to always put them as -r -w -x...?
 
Old 08-24-2009, 03:15 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by RaptorX View Post
But they did not mention that -a is also the "and" operator!...
It is just at the end of the next section 7.3 under "compound operators".

Quote:
Originally Posted by RaptorX View Post
Another question... can you bind several tests like -rwx or you have to always put them as -r -w -x...?
Nope. Otherwise the result of the test would be ambiguous: what if two of them are true and only one is false? The result should be true or false? Indeed, if you try you will get something like:
Code:
$ test -rwx testfile
-bash: test: -rwx: unary operator expected
 
Old 08-24-2009, 03:21 PM   #5
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
understood, thanks for the info!
 
  


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
bash - syntax of if [ ] (test) bricedebrignaisplage Programming 2 06-04-2008 04:22 AM
Confused over a BASH command Couling Linux - Newbie 7 11-12-2007 08:18 AM
BASH - For with a if test command mago Programming 9 05-03-2007 02:15 PM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 07:47 AM
Confused about updating Fedora Core 2 Test 1 nutmeg Fedora 2 02-23-2004 02:12 AM

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

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