LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2017, 08:35 PM   #16
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,893

Rep: Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613

... and I highly recommend [[ ]] over [ ] as explained here http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS. Double brackets are basically more 'robust'/sane.
I was on a ksh (applies equally to bash) course @ HP many years ago and they went into this; tl;dr always use [[ ]] .
 
Old 05-13-2017, 02:09 AM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,804

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Still the [ ] (test command) is a challenge in case the tested variable contains a test operator, for exanple
Code:
file1='!'
or
Code:
file1='-z'
and
Code:
if [ "$file1" = "foo" ]
Workarounds
1. Prepend an extra string, I use it outside the quotes to emphasize it's extra
Code:
if [ x"$file1" = x"foo" ]
2. Or switch the order, put the constant first
Code:
if [ "foo" = "$file1" ]
3. Or use the [[ ]] compound that is not a command and is easier to parse, and has less pitfalls for beginners
Code:
if [[ "$file1" = "foo" ]]
 
1 members found this post helpful.
Old 05-13-2017, 05:03 AM   #18
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,764
Blog Entries: 11

Rep: Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380Reputation: 5380
Quote:
Originally Posted by r3sistance View Post
This seems like a horrible way of checking a variable is not empty/null, the '! -z' method given by suicidalggroll is definitely a better test.
but is it really so horrible?
an almost identical construct is still being used in grub configuration (which uses some sort of ashell?) and i think i remember there was an explanation to the effect that it's more robust that way, more compatible and less likely to break with edge cases (variable not defined instead of empty)...
that said, it does look like bad coding (and so does /boot/grub/grub.cfg...).

Quote:
Originally Posted by chrism01 View Post
... and I highly recommend [[ ]] over [ ] as explained here http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS. Double brackets are basically more 'robust'/sane.
this fails on e.g. dash (and i guess other lightweight shells), it says: [[: not found
 
  


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
Remapping a Northgate Omnikey "Omni" button to serve as a "Windows" or "Super" key. CVAlkan Linux - Hardware 6 01-07-2019 12:21 PM
[SOLVED] X: "loading extension glx" "no screens found" "fatal server error" (w/ nvidia driver) Geremia Slackware 7 12-29-2014 11:00 AM
[SOLVED] "net rpc" "failed to connect to ipc$ share on" or "unable to find a suitable server" larieu Linux - General 0 11-09-2014 12:45 AM
[SOLVED] Scripting Help: Using "File1" as list of objects to search for in "File2" Asharru Programming 10 09-05-2010 06:53 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:14 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
Open Source Consulting | Domain Registration