File operator command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File operator command
# 8  
Old 06-12-2015
Quote:
Originally Posted by Scrutinizer
Yes, but it is a different command altogether, in fact [[..]] is part of the shell syntax, whereas [ .. ] is not and using a regular test command with single brackets would not work properly in this case. Even if the -n operators happen to have a similar meaning, better advise would be to use the man page of the shell..
Looks different here:
Code:
0 ~ $ [[ -n "$*" ]]

1 ~ $ [ -n "$*" ]

1 ~ $ set something

0 ~ $ [ -n "$*" ]

0 ~ $ [[ -n "$*" ]]

0 ~ $ [ -n $* ]

0 ~ $ [[ -n $* ]]

0 ~ $ set ""

0 ~ $ [ -n $* ]

0 ~ $ [[ -n $* ]]

1 ~ $ $SHELL --version
GNU bash, Version 4.3.39(1)-release (x86_64-unknown-linux-gnu)

The only difference i figured is that the double brackets handles unquoted variables, which is bad coding style, whereas single bracket requires them to be quoted.

Cheers
# 9  
Old 06-12-2015
Quote:
Originally Posted by MadeInGermany
IMHO one should quote $*, to avoid substitution by files and eventual "too many tokens"
Code:
[[ -n "$*" ]]

$* doesn't expand to a list of files; it expands to the list of positional parameters. If there is one positional parameter and it is an asterisk, then that would expand to a list of files in the current directory in the command:
Code:
[ -n $* ]

but in the command:
Code:
[[ -n $* ]]

it just expands to an asterisk. Try using:
Code:
set -- "*"
set -x
[ -n $* ]
[[ -n $* ]]

to see the difference.

That is the difference between being a parameter expanded by the shell when invoking a utility (even if it is a built-in) and recognizing a parameter when it is recognized as an argument to the of the shell [[ keyword.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 06-12-2015
Quote:
Originally Posted by sea
[..]

The only difference i figured is that the double brackets handles unquoted variables, which is bad coding style, whereas single bracket requires them to be quoted.

Cheers
Why is that bad coding style? I certainly think you should quote variable expansions, but only in cases where it is actually subject to field splitting and/or globbing, which is not the case here (it certainly is in the case of single brackets). It also is not needed for the word in a case statement, for example, or with variable assignments.

----

Quote:
Originally Posted by MadeInGermany
IMHO one should quote $*, to avoid substitution by file names and eventual "too many tokens"
Code:
[[ -n "$*" ]]

It does not make a difference in the case of double brackets..
Code:
$ set --  ;  [[ -n $* ]] && echo hello1 ; [[ -n "$*" ]] && echo hello2
$ set "2 3"  ;  [[ -n $* ]] && echo hello1 ; [[ -n "$*" ]] && echo hello2
hello1
hello2
$ set 1 "2 3"  ;  [[ -n $* ]] && echo hello1 ; [[ -n "$*" ]] && echo hello2
hello1
hello2
$ set 1 "*"  ;  [[ -n $* ]] && echo hello1 ; [[ -n "$*" ]] && echo hello2
hello1
hello2


Last edited by Scrutinizer; 06-12-2015 at 05:10 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 06-12-2015
Thanks Don and Scrutinizer for pointing that out; the bash man page was not clear for me.
Anyway, I would prefer [[ 0 -ne $# ]] or [ 0 -ne $# ] that would also recognize empty arguments:
Code:
set -- ""
[[ -n $* ]] && echo non-empty arg "$1"
[[ 0 -ne $# ]] && echo arg "$1"


Last edited by MadeInGermany; 06-12-2015 at 05:24 AM..
# 12  
Old 06-12-2015
Or alternatively, there is also numerical comparison:
Code:
(( $#>0 ))

or
Code:
(( $# ))

--
But I would also prefer:
Code:
[ $# -ne 0 ]

or
Code:
[ $# != 0 ]


Last edited by Scrutinizer; 06-12-2015 at 03:03 PM..
# 13  
Old 06-13-2015
Can anybody tell me more about this on linux operating systems (the why and the what) :

Code:
me@glitch:~$ which "["
/usr/bin/[
me@glitch:~$ ls -dl /usr/bin/[
-rwxr-xr-x 1 root root 39464 Mar 14 16:47 /usr/bin/[
me@glitch:~$ md5sum /usr/bin/test
5ff3a64fba15e925aefe88865efa1931  /usr/bin/test
me@glitch:~$ md5sum /usr/bin/[
cf8b60a5e80fb68e7ab58454f74b7a6d  /usr/bin/[
me@glitch:~$ man -f "[" && man -f "test"
[ (1)                - check file types and compare values
test (1)             - check file types and compare values

On HPUX or Solaris this does not exists.
# 14  
Old 06-13-2015
This may explain the difference:

From the GNU(linux) man test:
Code:
NOTE: [ honors the --help and --version options, but test does not.  test treats each of those as it treats any other nonempty STRING.

Code:
$ ls -l $(which test [)
-rwxr-xr-x. 1 root root 41448 Jun 10  2014 /usr/bin/[
-rwxr-xr-x. 1 root root 37288 Jun 10  2014 /usr/bin/test

Whereas on some systems these are hard links:
Code:
$ ls -li $(which test [)
288077518 -rwxr-xr-x  2 root  wheel  18480 Sep 10  2014 /bin/[
288077518 -rwxr-xr-x  2 root  wheel  18480 Sep 10  2014 /bin/test

or soft links:
Code:
ls -li $(which test [)
17108232 -rwxr-xr-x    1 root     sys        18688 Feb  7  2013 /sbin/test
33554580 lrwxr-xr-x    1 root     sys           15 Feb  6  2013 /usr/bin/[ -> ../../sbin/test

Anyway, it is unlikely that these commands are actually being used by your script, since in most shell these are shell builtins
Code:
$ type test [
test is a shell builtin
[ is a shell builtin

That's maybe why on some systems [ does not even exist:
Code:
$ type -a test [
test is a shell builtin
test is /usr/bin/test
[ is a shell builtin

$ which test [
/usr/bin/test
no [ in /usr/bin /usr/sbin


Last edited by Scrutinizer; 06-13-2015 at 03:26 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use pipe operator as simple character in text file?

Hello all, I have two files which are cmd and disk. `$cat cmd lsdev | grep -iw` `$cat disk hdisk2` Now I want to use the contents of both the files in a way such that `lsdev | grep -iw` command works for hdisk2 when I write the following script: `!#/bin/sh cmd1="$( sed -n... (4 Replies)
Discussion started by: ravi.trivedi
4 Replies

2. Shell Programming and Scripting

perl's substitution operator "s" with file contents?

Please show me how to make substitution over the contents of a file in a perl script. In a perl script, the core part of substitution operation is s/FINDPATTERN/REPLACEPATTERN/g; However, I cannot figure out how to make the substitution occur over the contents of a file. The following... (3 Replies)
Discussion started by: LessNux
3 Replies

3. Shell Programming and Scripting

Problem in test file operator on a ufsdump archive file mount nfs

Hi, I would like to ask if someone know how to test a files if exist the file is a nfs mount ufsdump archive file.. i used the test operator -f -a h almost all test operator but i failed file1=ufs_root_image.dump || echo "files doesn't exist && exit 1 the false file1 is working but... (0 Replies)
Discussion started by: jao_madn
0 Replies

4. UNIX for Dummies Questions & Answers

+= operator

im new to bash scripting and im just using online tutorials and trial and error. i wanted to write a script to read numbers from a file and find their sum: #!/bin/bash theSum=0 for line in $(cat numbers.txt) do let "theSum = theSum + $line" echo "$line" done echo "The sum is... (3 Replies)
Discussion started by: astrolux444
3 Replies

5. UNIX for Dummies Questions & Answers

su with << operator

All, THe below is my script , when i use this i am getting nothing . could any one help me to know what is the use of the << operator below su - $8 << supo echo "exportsph $2 $1 $3 $4" exportsph $2 $1 $3 $4 supo i also tried as individual command su - userid << supo , when i do... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

6. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

7. Shell Programming and Scripting

FIle (directory) test operator (bash)

I'm almost pulling out my hair trying to figure out what's wrong with this... there's no reason I can see that it shouldn't be working. It seems that the code acts as though the conditional statement is true no matter what - I've even tried removing the negation operator, but it always goes into... (5 Replies)
Discussion started by: wildbluefaerie
5 Replies

8. Shell Programming and Scripting

Checking if file exists using a NOT operator and shell variable

Hi, I'm new to UNIX, at least shell programming and am having trouble figuring out a problem i'm having. In one section in my nested if statement, i want the program to test if the file does not exist, based on an argument supplied at the command line by the user. What i have is elif ; then... (3 Replies)
Discussion started by: rowlf
3 Replies

9. UNIX for Dummies Questions & Answers

File already exists error while using '>' operator

hi i am using the below code grep -v '^$' file1.lst >file1.lst but it gives file1.lst already exists. And i want to over rite on the same file Whats the work around? (5 Replies)
Discussion started by: jathin12
5 Replies

10. Programming

new operator

Hi, Please clear the 2 questions, 2 Questions, 1) Why the new as a operator? Is there any special reason why it can't be a function like malloc? 2) How are we considering sizeof(),new are as a opearartors? I know + - * / -> , . etc.. are operators, which criteria satisfied by sizeof()... (4 Replies)
Discussion started by: Nagapandi
4 Replies
Login or Register to Ask a Question