LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-21-2010, 05:47 AM   #1
cooky_jun
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
help for explanation for two lines code in bash


if [ -d $d ]; then
if [ x$dm = x ]; then dm=$d; else dm=$dm:$d; fi

first line is search directory $d ?
what does [x$dm = x] mean?? $dm is empty ?
 
Old 05-21-2010, 06:25 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Hi and welcome to LinuxQuestions!

The first line, checks if $d is a directory. The second line checks if $dm is an empty string: in this case the concatenation x$dm brings to x. Exactly as you said.
 
Old 05-21-2010, 06:25 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

-d file - True if file exists and is a directory.

x$dm = x => This checks if $dm is empty (or not). You see this convention (prepending a character) sometimes.

$dm = "" => should also work.

Hope this helps.

Beaten by colucix (again...)
 
Old 05-21-2010, 06:40 AM   #4
cooky_jun
LQ Newbie
 
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys
 
Old 05-21-2010, 06:42 AM   #5
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Please mark this thread as solved !!

Thank You
 
0 members found this post helpful.
Old 05-21-2010, 08:27 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by druuna View Post
$dm = "" => should also work.
Actually this works only by means of double brackets or by embedding $dm in double quotes:
Code:
$ [[ $dm = "" ]] && echo ok
ok
$ [ "$dm" = "" ] && echo ok
ok
$ [ $dm = "" ] && echo ok
bash: [: =: unary operator expected
$
Another option is:
Code:
$ [ -z $dm ] && echo ok
ok
but I suspect that in this case (without quotes) the test reduces to testing a string [ -z ], that is always true.
Quote:
Beaten by colucix (again...)
Just for a bunch of seconds...
 
1 members found this post helpful.
  


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
Perl vs. bash code - number of lines noir911 Programming 11 10-07-2011 01:35 PM
Code fragment explanation please! Completely Clueless Programming 14 10-30-2009 11:15 AM
awk code explanation pete1234 Programming 2 10-14-2006 08:05 PM
Code explanation ... ? dbee Linux - Software 1 12-17-2005 02:06 AM
fat fs code explanation ramya272 Programming 0 03-06-2004 10:55 AM


All times are GMT -5. The time now is 03:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration