LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-05-2010, 08:49 AM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
Shell Scripts Variables Test


What will be the output of the following Shell Script?

Code:
a="1245"

[-n $a]
echo $?

[-z $a]
echo $?

That was asked in the interview I attended today.
 
Old 08-05-2010, 09:07 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Hi_This_is_Dev View Post
What will be the output of the following Shell Script?

Code:
a="1245"

[-n $a]
echo $?

[-z $a]
echo $?

That was asked in the interview I attended today.

and did you get it correct??
$a = 12345

so

test if variable is a not empty

-n $a
echo $? returns 0


test if variable is zero bytes
-z $a
echo $? returns 1

not too hard..

Last edited by centosboy; 08-05-2010 at 09:08 AM.
 
Old 08-05-2010, 09:18 AM   #3
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by centosboy View Post
and did you get it correct??
$a = 12345

so

test if variable is a not empty

-n $a
echo $? returns 0


test if variable is zero bytes
-z $a
echo $? returns 1

not too hard..



I also mentioned the same answer:

0
1

But the numbers were in quotation marks. Besides, the $ was missing. That also confused me.
 
Old 08-05-2010, 09:34 AM   #4
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Hi_This_is_Dev View Post
I also mentioned the same answer:

0
1

But the numbers were in quotation marks. Besides, the $ was missing. That also confused me.
what do you mean $ was missing???
i have actually written it incorrectly

$a=value - wrong
a=value - correct
 
Old 08-05-2010, 10:28 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Hi_This_is_Dev View Post
I also mentioned the same answer:

0
1

But the numbers were in quotation marks. Besides, the $ was missing. That also confused me.
wrong, both will give you error. must have spaces between square brackets.
 
Old 08-28-2010, 08:21 PM   #6
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by ghostdog74 View Post
wrong, both will give you error. must have spaces between square brackets.

Well, friend, PHP uses $ before variable name so I forgot that $ is not used in Shell Script when we assign a value to a variable.


About a single space after [ and before ], all I can say is that they often print incorrectly on papers and it is not sure whether to test us or it is their typo.

Code:
a="1245"

[-n $a]
echo $?

Above, even though we know that a single space after [ and before ] is required there but they would ask us to choose an option from:

[A] 0
[B] 1
[C] 5
[D] 7

and we know no output is going to be generated as the statement is incorrect.

Sometimes, in exams / test or interview it becomes a problem to figure out if they really meant it or forgot to review it.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
indexing variables in lists using shell scripts enigma69 Programming 14 06-01-2010 01:14 AM
[SOLVED] Variables between shell scripts Potatos Linux - Newbie 1 05-20-2010 09:32 AM
Korn Shell test if multiple variables are set jonlake Programming 4 04-09-2008 10:41 AM
about shell scripts global variables nesta Programming 2 01-05-2007 12:32 AM
Passing variables between shell scripts big_ginge21 Programming 3 09-26-2006 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:57 PM.

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