LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-10-2010, 03:52 AM   #1
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Rep: Reputation: 39
need some clarity about syntax in following shell script


Hi
I need some help regarding the following shell script.
Code:
#!/bin/sh
dir=$(dirname "$0")
"$dir/network-bridge" "$@" vifnum=0 netdev=eth0 bridge=xenbr0
Want to know what dirname does I read the man page dirname stores name of parant directory is what I understood is it correct ?

given
Code:
dir=$(dirname "$0")
the above code what does it stores dir $() is executing what "$0"
is it referring to name of script that is passing it as command line parameter if yes then why is it put in double quotes
Code:
""
.
 
Old 04-10-2010, 04:33 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by tkmsr View Post
Want to know what dirname does I read the man page dirname stores name of parant directory is what I understood is it correct?
Not really: dirname strips the trailing "/something" from a string (path). It is not aware of the actual parent directory of a file. This means it works even on non-existent paths, e.g.
Code:
$ dirname /pippo/pluto/paperino
/pippo/pluto
Suppose you launch the script posted above as
Code:
./script.sh
in this case the dirname command results in
Code:
.
If you want to retrieve the full path of the script, better to use readlink (despite its name it can be forced to retrieve the full path of a file, not only symbolic links). Using a nested command substitution you can retrieve the full path of the parent directory of the script:
Code:
dir=$(dirname "$(readlink -f "$0")")
Regarding the double quotes, they can be useful in this case if the name of the script (hopefully not) or its path contains blank spaces.

Last edited by colucix; 04-10-2010 at 06:29 AM. Reason: explained below...
 
Old 04-10-2010, 04:48 AM   #3
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by colucix View Post

Suppose you launch the script posted above as
Code:
./script.sh
in this case the dirname command results in
Code:
.
Yes you pointed it rightly this is what I am not clear with.As why is it giving
Code:
.
Thanks I got it.
Quote:
Originally Posted by colucix View Post
Regarding the double quotes, they can be useful in this case if the name of the script (hopefully not) or its path contains blank spaces.
Can you give an example?

Last edited by tkmsr; 04-10-2010 at 04:51 AM.
 
Old 04-10-2010, 06:28 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, let's say that using double quotes around variables is a good habit in order to prevent unforeseen problems. Indeed, it is unlikely to have spaces in script names, but suppose you have spaces in some parent directory of the script and you don't put double quotes around the inner command substitution, e.g.
Code:
$ pwd
/home/alex/dir with spaces
$ cat test.sh
#!/bin/bash
dir=$(dirname $(readlink -f "$0"))
echo $dir
$ ./test.sh
dirname: extra operand `with'
Try `dirname --help' for more information.
This is because dirname accepts only one argument. If the argument contains spaces, it's split into two or more fields unless you put it inside quotes.

Actually in my example above I left the inner substitution unquoted (edited now)..

Last edited by colucix; 04-10-2010 at 06:30 AM.
 
Old 04-10-2010, 11:28 PM   #5
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by colucix View Post
Well, let's say that using double quotes around variables is a good habit in order to prevent unforeseen problems. Indeed, it is unlikely to have spaces in script names, but suppose you have spaces in some parent directory of the script and you don't put double quotes around the inner command substitution, e.g.
Code:
$ pwd
/home/alex/dir with spaces
$ cat test.sh
#!/bin/bash
dir=$(dirname $(readlink -f "$0"))
echo $dir
$ ./test.sh
dirname: extra operand `with'
Try `dirname --help' for more information.
This is because dirname accepts only one argument. If the argument contains spaces, it's split into two or more fields unless you put it inside quotes.

Actually in my example above I left the inner substitution unquoted (edited now)..
Thank you.
 
  


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
is there a '?:' syntax shell script? logicalfuzz Linux - General 3 01-29-2010 09:26 AM
Shell script (iptables) syntax error mrmnemo Programming 6 12-04-2009 08:57 AM
Shell script syntax question arash8m Programming 4 06-01-2007 08:39 AM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 08:38 AM
Shell Script Syntax dragon49 Programming 1 03-12-2003 09:25 AM

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

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