LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 04-21-2010, 04:46 PM   #1
Star_Gazer
LQ Newbie
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 28

Rep: Reputation: 15
Unhappy The special expression `-l STRING' for "test" command


I have searched and searched in regards to this documented 'special expression '-l STRING' for the 'test' command, and to no avail, have I found out why it does not work on my system.

The example always given is:
Code:
test -l abc -gt 1 && echo yes
And the returned result is:
Code:
bash: test: -l: unary operator expected
The documention is usually as follows:
Numeric tests
Numeric relationals. The arguments must be entirely numeric (possibly negative), or the special expression `-l STRING', which evaluates to the length of STRING.


Then examples are given, including the one I provided above.

Does anyone else have this issue of getting an error when trying this special expression of 'l STRING' ???

Regards,
Clifton
 
Old 04-21-2010, 05:10 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
You are looking at the documnetation for the test command (/bin/test) but running the shell bulitin test which behaves differently. Here's /bin/test working as you expect
Code:
c@CW8:~$ /bin/test -l abc -gt 1 && echo yes
yes
 
1 members found this post helpful.
Old 04-21-2010, 08:08 PM   #3
Star_Gazer
LQ Newbie
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 28

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by catkin View Post
You are looking at the documnetation for the test command (/bin/test) but running the shell bulitin test which behaves differently. Here's /bin/test working as you expect
Code:
c@CW8:~$ /bin/test -l abc -gt 1 && echo yes
yes
Thank you!

This issue for me is now solved.

I did not know that there was a builtin version and one that is external (not sure if that is the right word). I did a "whereis test" and found the 'test' command you are referring to.

Code:
~/Documents/Practice $ whereis test
test: /usr/bin/test /usr/bin/X11/test /usr/share/man/man1/test.1.gz

~/Documents/Practice $ /usr/bin/test -l abc -gt 1 && echo yes
yes

~/Documents/Practice $ /usr/bin/X11/test -l abc -gt 1 && echo yes
yes
Weird that there is two different 'test' commands, but at least now I know. I'll check out the "[" one as well (as I noted the file on disk in the same directories as 'test').

Thanks Again,
Clifton
 
Old 04-21-2010, 09:21 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
FYI: Just to add, if you do a man bash you will find there are a number of bash builtin equivalents:
ie test, [, echo (to name a few)
 
Old 04-22-2010, 01:40 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Quote:
Originally Posted by Star_Gazer View Post
I did not know that there was a builtin version and one that is external (not sure if that is the right word).

...

Weird that there is two different 'test' commands, but at least now I know. I'll check out the "[" one as well (as I noted the file on disk in the same directories as 'test').
Yes -- "builtin" and "external" are the terms used in the shell documentation.

The concept of a shell is of a command interpreter and originally the shell itself was relatively lightweight with few builtins except for the ones that had to be such as cd. Some external commands such as test were used so heavily in shell scripting that the resource cost of having them as externals (create new process by fork, load new program by exec) reduced performance. The solution was to implement them as builtins. echo is another good example.

The original externals were left in place for backward compatibility and for use from other languages, for example C can call /bin/test and others via the system call (even if there are better ways of implementing the same functionality in C!).

For current bash programming the Conditional Construct [[ ]] is preferred to builtins test and [ (and definitely to externals test and [ ) for reasons explained here.
 
3 members found this post helpful.
  


Reply

Tags
bash, expressions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to match with a string containing litteral dot "." with grep command ? coolloo_djack Programming 5 03-17-2010 10:07 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
Regular expression matching , match "error string" but not "other error" jmcmillan Programming 3 07-07-2008 09:50 AM
Regular expression to extract "y" from "abc/x.y.z" rag84dec Linux - Newbie 1 05-29-2008 02:47 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM


All times are GMT -5. The time now is 05:34 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration