LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-11-2013, 02:43 AM   #16
voleg
Member
 
Registered: Oct 2013
Distribution: RedHat CentOS Fedora SuSE
Posts: 354

Rep: Reputation: 51

man test:
Quote:
! EXPRESSION
EXPRESSION is false
You use
Quote:
if [ ! "$NAME" ]
what can be translated as "NOT variable".

Unbreakable test should be:
Code:
if [ 'x'"$NAME" = 'x' ] ; then
..
fi

Last edited by voleg; 11-11-2013 at 02:45 AM.
 
Old 11-11-2013, 03:03 AM   #17
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by voleg View Post
Unbreakable test should be:
Code:
if [ 'x'"$NAME" = 'x' ] ; then
..
fi
This line is not easy to understand ...
Code:
if [ ! "$NAME" ]
... but it is perfectly valid bash so is not the cause of the problem which is a syntax problem.

Another version of the same test, robust and transparent is
Code:
if [[ $NAME != '' ]]; then
or (less transparent IMHO)
Code:
if [[ -z $NAME ]]; then
 
Old 11-11-2013, 03:03 AM   #18
tuananhk53
LQ Newbie
 
Registered: Oct 2013
Location: Hanoi Vietnam
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by voleg View Post
man test:

You use

what can be translated as "NOT variable".

Unbreakable test should be:
Code:
if [ 'x'"$NAME" = 'x' ] ; then
..
fi
hi voleg, i'm an amater system anh linux
can you fix this script helps me?
thanhks voleg
 
Old 11-11-2013, 03:58 AM   #19
voleg
Member
 
Registered: Oct 2013
Distribution: RedHat CentOS Fedora SuSE
Posts: 354

Rep: Reputation: 51
No, you have to do this yourself. This is a "dao" way to learn.
You will like scripting, I promisse.
 
Old 11-11-2013, 04:03 AM   #20
voleg
Member
 
Registered: Oct 2013
Distribution: RedHat CentOS Fedora SuSE
Posts: 354

Rep: Reputation: 51
Quote:
Quote:
Unbreakable test should be:
Code:
if [ 'x'"$NAME" = 'x' ] ; then
..
fi
This line is not easy to understand ...
This construction works for me for /bin/sh and /bin/ksh before bash was invented.
Old dog will not teach new trick.

Last edited by voleg; 11-11-2013 at 04:05 AM.
 
Old 11-11-2013, 04:36 AM   #21
tuananhk53
LQ Newbie
 
Registered: Oct 2013
Location: Hanoi Vietnam
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by voleg View Post
This construction works for me for /bin/sh and /bin/ksh before bash was invented.
Old dog will not teach new trick.
hi voleg,
i understand what you say.
Thanks for your help!
 
Old 11-12-2013, 08:45 PM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Sometimes new tricks make life easier (as long as portability is not required): http://mywiki.wooledge.org/BashFAQ/031
 
Old 11-13-2013, 03:02 AM   #23
tuananhk53
LQ Newbie
 
Registered: Oct 2013
Location: Hanoi Vietnam
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
Sometimes new tricks make life easier (as long as portability is not required): http://mywiki.wooledge.org/BashFAQ/031
hi catkin
i don't understand what you say
hmm, maybe that is a censure for me......
 
Old 11-13-2013, 03:38 AM   #24
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
@tuananhk53

don't worry about it, it is not worth it.

if in any doubt use voleg's 'form'

Code:
if [ 'isitnull'"$VAR" = 'isitnull' ]
then
   echo "yes , VAR is \'null\' "
else
   echo "no , VAR = $VAR"
fi
the simple fact is , it works when [ "$VAR" ] doesn't AND will still work when it does

you should use it in place of [ ! "$NAME" ] in your script, it might 'fix it'

example

Code:
   UID1=`echo $line | cut -d: -f1`
   NAME=`echo $line | cut -d: -f5 | cut -d: -f1`
   if [ ! "$NAME" ]
   then
      NAME=$UID1
Code:
   UID1=`echo $line | cut -d: -f1`
   NAME=`echo $line | cut -d: -f5 | cut -d: -f1`
   if [ 'x'"$NAME" = 'x' ]
   then
      NAME=$UID1
 
Old 11-23-2013, 03:14 AM   #25
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by tuananhk53 View Post
hi catkin
i don't understand what you say
hmm, maybe that is a censure for me......
Was it "as long as portability is not required" or the linked BASH FAQ page that you did not understand?
 
  


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
[SOLVED] why always “syntax error near unexpected token 'fi'” dy20082250 Linux - Newbie 1 10-08-2013 09:44 AM
Syntax error near unexpected token `}' chiendarret Linux - Software 3 09-03-2013 12:11 PM
syntax error near unexpected token `}' Chris1969 Linux - Newbie 17 06-18-2013 01:54 PM
syntax error near unexpected token `else' nav007_2000 Programming 7 02-11-2011 09:14 AM
syntax error near unexpected token `fi' sureshkumar.repaka Linux - General 7 08-19-2007 10:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

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