LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-15-2018, 10:46 AM   #1
Iostera
LQ Newbie
 
Registered: Apr 2006
Posts: 4

Rep: Reputation: 0
Is TEST possible without brackets ?


Hi,

I am a newby in shell scripting and this is probably a basic question but I will just put it out there.
As far as I am aware TEST in shell scripting requires brackets i.e. `[`.
I have come across this function and I cannot make sense of it as it appears to test an expression without surrounding brackets(see line 2 of the pathmunge().
Can you help me understand this ?

Thanks,
Code:
pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
        fi
}

# Path manipulation
if [ `id -u` = 0 ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
fi

pathmunge /usr/X11R6/bin after

unset pathmunge
 
Old 09-15-2018, 11:02 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,287

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
see man bash:
Code:
      if list; then list; [ elif list; then list; ] ... [ else list; ] fi
              The  if  list is executed.  If its exit status is zero, the then
              list is executed.  Otherwise, each  elif  list  is  executed  in
              turn,  and  if  its  exit status is zero, the corresponding then
              list is executed and the command completes.  Otherwise, the else
              list  is executed, if present.  The exit status is the exit sta‐
              tus of the last command executed, or zero if no condition tested
              true.
Code:
      A list is a sequence of one or more pipelines separated by one  of  the
       operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
       <newline>.
in short you write a command after the keyword if, and if will check the result of the command. The result is the exit code.
If will check if the exit code was 0 (= successful execution, true) or nonzero (= some error, false).
[ is just a command which will evaluate an expression and returns 0 or 1 as exit code.

Last edited by pan64; 09-15-2018 at 11:42 AM.
 
3 members found this post helpful.
Old 09-15-2018, 11:19 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 9,413
Blog Entries: 7

Rep: Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334
Quote:
Originally Posted by Iostera View Post
I cannot make sense of it as it appears to test an expression without surrounding brackets(see line 2 of the pathmunge().
Code:
pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
why do you think the brackets are not surrounding? they are!
Code:
[ "$2" = "after" ]
 
Old 09-15-2018, 12:27 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,668

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
yes
Code:
e=4
if test $e == 0 ; then echo "e is $e" ; else echo "no" ; fi
or 

if [[ "$e" -eq '0' ]] ; then ...

or 
if [ "$e" -eq '0' ] ; then ...
where the two different types of [[ brackets ]] and [ brackets ] have different implementations.

Last edited by BW-userx; 09-15-2018 at 12:33 PM.
 
2 members found this post helpful.
Old 09-15-2018, 12:33 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,563

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Well, 'test' and '[' are the same program/command except for a syntactical difference:
Code:
test "$1" = 'str' && echo 'equals'
[ "$1" = 'str' ] && echo 'equals'
 
1 members found this post helpful.
Old 09-15-2018, 03:28 PM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: High Sierra
Posts: 9,222
Blog Entries: 37

Rep: Reputation: Disabled
Code:
man test
never gets any attention.
 
2 members found this post helpful.
Old 09-15-2018, 05:40 PM   #7
Iostera
LQ Newbie
 
Registered: Apr 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
see man bash:
Code:
      if list; then list; [ elif list; then list; ] ... [ else list; ] fi
              The  if  list is executed.  If its exit status is zero, the then
              list is executed.  Otherwise, each  elif  list  is  executed  in
              turn,  and  if  its  exit status is zero, the corresponding then
              list is executed and the command completes.  Otherwise, the else
              list  is executed, if present.  The exit status is the exit sta‐
              tus of the last command executed, or zero if no condition tested
              true.
Code:
      A list is a sequence of one or more pipelines separated by one  of  the
       operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
       <newline>.
in short you write a command after the keyword if, and if will check the result of the command. The result is the exit code.
If will check if the exit code was 0 (= successful execution, true) or nonzero (= some error, false).
[ is just a command which will evaluate an expression and returns 0 or 1 as exit code.
Thank you.
This is the explanation I was looking for.
 
Old 09-15-2018, 05:42 PM   #8
Iostera
LQ Newbie
 
Registered: Apr 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ondoho View Post
why do you think the brackets are not surrounding? they are!
Code:
[ "$2" = "after" ]
I was expecting brackets around
Code:
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
 
Old 09-16-2018, 01:46 AM   #9
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 879

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
BTW the quick hack
Code:
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
  ...
fi
has missing quotes "$PATH", and
can be optimized (a builtin replaces /bin/egrep)
Code:
case :$PATH: in (*:$1:*);;(*)
  ...
esac
 
Old 09-17-2018, 12:10 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 9,413
Blog Entries: 7

Rep: Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334Reputation: 2334
Quote:
Originally Posted by Iostera View Post
I was expecting brackets around
Code:
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
ah, i see.
but there's no brackets at all, so your thread title is off.
and it's not a test in the sense of using "test" or "[" or "[[".
without it if just checks true or false.
the ! negates the check.
so "if NOT TRUE return value of some command; then" etc.
 
1 members found this post helpful.
Old 09-17-2018, 03:47 AM   #11
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 879

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
More general, if is expecting a command list, normally one command.
echo is a command and test is a command.
[ is an alias for test. In historical Unix system /usr/bin/[ was a link to /usr/bin/test, where test when called as [ expected an additional last argument ].
The proof:
Code:
if [ "$e" -eq '0' "]" ; then ...
if [ "$e" -eq '0' \] ; then ...
if test "$e" "-eq" 0 ; then ...
Are all correct. The ending ] is quoted; the shell removes quotes when passing the arguments to the command. You could also quote "-eq" if you want. This is for demonstration - it looks bad of course.
In comparison the later added
Code:
if [[ "$e" -eq '0' ]] ; then ...
is a "compound", an addition to the general shell syntax. Does not allow quoted [[ or -eq or ]].

A special thing is the ! that is an addition to the if syntax (the developers could have (better) chosen "unless" instead).

Last edited by MadeInGermany; 09-17-2018 at 03:51 AM. Reason: added code tags
 
Old 09-17-2018, 06:22 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,668

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
just for programming logic and practices.

this "if NOT TRUE evaluates to TRUE else it fails the test an will not execute the command assigned to it.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Want to grep data between square brackets(Including brackets) harshagr Programming 20 02-15-2018 07:23 AM
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 02:43 PM
Sed and brackets unihiekka Linux - Newbie 9 10-09-2010 11:08 PM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 01:37 PM

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

All times are GMT -5. The time now is 08:09 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration