f



bash: Where Am I?

Hi everybody.

I have a bash script I've created that needs to know what directory it's
in. I can't use pwd, because that will give me the directory from where
the script was run, and not where it actually is on the disk. Can anyone
suggest a way of doing this?

Thanks in advance,
Steve.

0
Steve
5/20/2006 1:03:00 PM
comp.unix.shell 15443 articles. 2 followers. Post Follow

5 Replies
802 Views

Similar Articles

[PageSpeed] 37

Steve Jones wrote:

> I have a bash script I've created that needs to know what 
> directory it's in. I can't use pwd, because that will give 
> me the directory from where the script was run, and not where 
> it actually is on the disk. Can anyone suggest a way of 
> doing this?

How about "dirname $0"?

Cheers, 

-- 
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/41
0
Klaus
5/20/2006 1:32:01 PM
On Sat, 20 May 2006 13:32:01 +0000, Klaus Alexander Seistrup wrote:

> Steve Jones wrote:
> 
>> I have a bash script I've created that needs to know what 
>> directory it's in. I can't use pwd, because that will give 
>> me the directory from where the script was run, and not where 
>> it actually is on the disk. Can anyone suggest a way of 
>> doing this?
> 
> How about "dirname $0"?
> 
> Cheers,

Of course - it's obvious now you mention it. Silly me.

Thanks a lot,
Steve.

0
Steve
5/20/2006 3:25:05 PM
In article 
<pan.2006.05.20.15.25.05.195595@squaregoldfish.NOSPAM.co.uk>,
 Steve Jones <steve@squaregoldfish.NOSPAM.co.uk> wrote:

> On Sat, 20 May 2006 13:32:01 +0000, Klaus Alexander Seistrup wrote:
> 
> > Steve Jones wrote:
> > 
> >> I have a bash script I've created that needs to know what 
> >> directory it's in. I can't use pwd, because that will give 
> >> me the directory from where the script was run, and not where 
> >> it actually is on the disk. Can anyone suggest a way of 
> >> doing this?
> > 
> > How about "dirname $0"?
> > 
> > Cheers,
> 
> Of course - it's obvious now you mention it. Silly me.

Now go read the FAQ and learn the limitations of this.  It's not 
possible to do it fully portably and generally.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Barry
5/20/2006 10:49:14 PM
curdir=$(cd `dirname $0`; pwd)

0
kevjunk
5/21/2006 3:36:27 PM

Steve Jones wrote:
> Hi everybody.
> 
> I have a bash script I've created that needs to know what directory it's
> in. I can't use pwd, because that will give me the directory from where
> the script was run, and not where it actually is on the disk. Can anyone
> suggest a way of doing this?
> 
> Thanks in advance,
> Steve.
> 

Try the ksh type command.  If someone enters just the script name
and the shell uses PATH to find it, dirname $0 will return just a dot.
The type command returns the full path.

scriptpath=`ksh -c "type \"$0\""`

Bill Seivert

0
Bill
5/22/2006 12:52:07 AM
Reply:

Similar Artilces:

web hosting, shell provider, unix shells, bnc shell, ircd provider, shell hosting, bnc shells, irc shells, ircd shells, unix shell account, irc shell, irc, eggdrop shell, irc vps, irc shell, linux vps
http://www.hostrazor.net/ web hosting, shell provider, unix shells, bnc shell, ircd provider, shell hosting, bnc shells, irc shells, ircd shells, unix shell account, irc shell, irc, eggdrop shell, irc vps, irc shell, linux vps -- questo articolo e` stato inviato via web dal servizio gratuito http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it ...

Convert Bash shell script to Korn shell script
The following bash script works fine to delete all files that are older than $1 minutes. To execute this script, bash cleanup +10 find /mypath -type f -cmin $1 -exec rm -f {} \; Unfortunately, now I just realize I need to make it work in Korn Shell. Even I change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the script by ksh cleanup +10, it still have different errors: cleanup[9]: -cmin: not found. cleanup[10]: -type: not found. Looks like it doesn't work anymore. I think I need to re-write the Korn Shell script that do the task. I tried to search for equivalent Korn shell commands but unsuccessful. Please help. thanks!! Matt <jrefactors@hotmail.com> wrote: > The following bash script works fine to delete all files that are > older than $1 minutes. To execute this script, > bash cleanup +10 > > find /mypath -type f -cmin $1 -exec rm -f {} \; > > Unfortunately, now I just realize I need to make it work in Korn > Shell. Even I > change the header from #!/bin/bash to #!/usr/bin/ksh, and execute the > script by ksh cleanup +10, it still have > different errors: > > cleanup[9]: -cmin: not found. > cleanup[10]: -type: not found. > > > Looks like it doesn't work anymore. I think I need to re-write the > Korn Shell script that do the task. > I tried to search for equivalent Korn shell commands but unsuccessful. > > > Please help. thanks!! Try typing it out on your comma...

can I run unix shell command in the ModelSim shell?
Hi, Just wonder if I can run unix shell command in the Modelsim shell, expecially those time/date command? Thanks. clinton__bill@hotmail.com wrote: > Hi, > Just wonder if I can run unix shell command in the Modelsim shell, > expecially those time/date command? ModelSim uses Tcl as its scripting language so (almost) anything you can do from Tcl can be done in ModelSim. Tcl has a variety of shell and file interaction commands available. Of course, if you want to execute Unix commands, you need to be running on a Unix (or Cygwin) system. Note that Tcl commands are NOT direct...

Unix shell
Hi all, We offer free shells with web hosting and a spam-filtered email account. - 50MB storage space (more available upon request) - Domain hosting available upon request - Access via SSH, SFTP, and POP3-ssl - Instant messaging clients for IRC, AIM, MSN, ICQ, Yahoo!, Gadu-Gadu, Jabber, and Lily - FastTrack (KaZaA), SoulSeek, and BitTorrent clients - Optional trainable server-side spam detection is also included. We also have a few vhosts for fans of IRC. Instant accounts available at: http://silenceisdefeat.org/ Anyway, I hope this is an acceptable place to let you guys know. If it isn't, please tell us via the email addresses on our site. Thanks ...

for of the Bash shell
#!/bin/bash --norc a=5 for (( b=1 ; b <= a ; b++ )) ; do echo $b done for (( b=1 ; b <= $a ; b++ )) ; do echo $b done Note that both loops will produce the same output. So the question is whether there is any reason to refer to variables inside ((...)) with $ prefixed or not. Could there be any pitfalls with one way or the other ? By the way does anyone know which was the earliest version of Bash which had the for ((...)) construct ? Version 2.03.0 doesn't have it. On 2007-03-31, Spiros Bousbouras wrote: > #!/bin/bash --norc > > a=5 > > for (( b=1 ; b &l...

unix shells
I have compiled a program called mk, and it's internal routines work with unixlib in a taskwindow happily enough. However the wheels fall off when a recipe wants to 'fire up' a child process for calling the gcc commands. It's a little confusing, because it echos the command perfectly to it's running Taskwindow, but errors with mk fork Invalid argument Looking inside a bit further, I think the error comes from a program unix.c where there also is reference to /bin/sh Should I be trying to map this to a new Taskwindow with Set UnixFS$/bin/sh Taskwindow or some redirection? Ron M In message <534c5a1151.beeb@ron1954.woosh.co.nz> Ron <beeb@woosh.co.nz> wrote: > I have compiled a program called mk, and it's internal routines > work with unixlib in a taskwindow happily enough. > However the wheels fall off when a recipe wants to 'fire up' a > child process for calling the gcc commands. > It's a little confusing, because it echos the command perfectly > to it's running Taskwindow, but errors with > mk fork Invalid argument > > Looking inside a bit further, I think the error comes from a > program unix.c where there also is reference to /bin/sh > > Should I be trying to map this to a new Taskwindow with > Set UnixFS$/bin/sh Taskwindow or some redirection? > > Ron M I have tried changing the command gcc to the coreutils cp, in case it is a heap/wimpslot issue but look...

Unix Shell
I'm new to RUBY, but am familar with other scripting languages, PHP, Perl, Wscript, etc. I'd like to be able to run items from the shell such as ls -l > dirlisting.txt. Just an example. Can I do this? I would have just used the search on these forums, but unfortuantely when I click Search I get "The page cannot be found". Hope they can get this fixed so I won't have to ask every little question. Thanks in advance. -Greg -- Posted via http://www.ruby-forum.com/. Greg Johnson wrote: > I'd like to be able to run items from the shell > such as l...

how to do this in bash shell?
Hi , I have a question like this, say i have a string like this abc="100 150 169 230 233 443 643 322" I would like to loop through each of number, But i don't know how many numbers are written in this string. Is there any efficient way to loop through it? any help is highly appreciated. Thank you. Carson On 2004-08-24, Carson wrote: > Hi , > > I have a question like this, > > say i have a string like this > > abc="100 150 169 230 233 443 643 322" > > I would like to loop through each of number, > > ...

[BASH] shell functions don't work the same when opening sub-shells
Hi, I've got a rather odd occurrance when I open a sub-shell for changes to my environment. In my .bashrc file I have some shell functions that I've written to help make life easier for me. One of these functions takes an option, -p, and then a pattern to search for. The pattern if found, is used in creating the sub-shell. I have it setup that the function always defaults to a specific type of pattern if the -p option is not provided. When starting a new shell all works as it should, but after exiting a sub-shell and using this function again, the function completely ignore...

XEmacs shell-script-mode vs. Emacs shell-script-mode[bash]
I've noticed the shell-script-mode I get with Emacs behaves differently from that which I get with XEmacs. In particular the Emacs version provides a hard indentation feature (I don't know the formal term for hitting Tab to indent code to reflect the syntactic structure, but that's what I mean). Can I configure XEmacs (latest CVS) to support hard indentation when editing bash scripts? -- p->m == (*p).m == p[0].m >>>>> "Hattuari" == Hattuari <susudata@setidava.kushan.aa> writes: Hattuari> Can I configure XEmacs (latest CVS) to ...

Bash: Getting rubbish parameters when script first run in a new bash shell
When I run a script (see below) in a new Bash shell, which is called via a DOT alias, the first few times I run it I get "ghost" parameters from the main shell. I use the shifts below to kill them; but would really like to know how to elimiate these "rubbish" parameters altogether alias epsb='. epsb' #!/bin/bash # eps # description : goto /inetpub/wwwroot/barratthomes/*param* cd c:/inetpub/wwwroot/www.yyyyyy.co.uk/epsystem/ if [ $# != 0 ] then echo "num of params $# $@" eval dir=$1 cd *$dir* shift shift shift shift shift shift fi zzapper -- vim -c &quo...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335832409)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335832409) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335814411)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335814411) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335857612)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335857612) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of Banking, payment and messaging systems. Exp with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Scope: Knowledge of Banking, payment and messaging systems. Experience with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Please send your...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335553540)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335553540) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of Banking, payment and messaging systems. Exp with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Scope: Knowledge of Banking, payment and messaging systems. Experience with Unix, shell Scripting, MQ Series, Oracle, Crystal Reports and WebSphere. Strong analytical skills. Ability to analyze a problem. own the problem and follow thru to resolution. Production support experience with critical systems. Good written and oral communication skills. Please send your...

US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609)
US-NJ-Jersey City: UNIX Developer, UNIX, Shell scripting, SwiftAlliance Access; (45335557609) ============================================================================================= Position: UNIX Developer Reference: SMC01885 Location: Jersey City NJ Duration: 7M Skills: Knowledge of SwiftAlliance Access. Exp in upgrading/migrating Swift Alliance Access, Swift Alliance Gateway, SwiftNet Link. Knowledge of Compliance and Watchlist filtering applications like Mircosoft. Knowledge of B...

Why does this happen bash Unix
I have : submit_task () { input="$@"; printf "task to be submitted = '%s'\n" $input; } antchk () { if [ $antchk -eq 0 ]; then return 0; fi; outdir=${CDMG_OUTDIR}/antchk; submit_task "$CDMG_ANTCHK_SUB"; # CDMG_ANTCHK_SUB = "/apps/dad/bin/antchk <outdir> <laff> <ucell> <technology> -lis <outdir>/lis.antchk" } antchk; and I call it with : ../run_checkers -cell ic33t -task antchk And the output is: task to be submitted = '/apps/dad/bin/antchk' task to be submitted = '<outdir>' task to be submitted = '<laff>' task to be submitted = '<ucell>' task to be submitted = '<technology>' task to be submitted = '-lis' task to be submitted = '<outdir>/lis.antchk' It's calling the submit_task for every word in the CDMG_ANTCHK_SUB variable I thought putting it in "" would stop the split. Work is not completed. I have to sub the <*> for the proper data before submission. ___ _ ____ ___ __ __ / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___ / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \ /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/ /___/ Texas Instruments ASIC Circuit Design Methodology Group Dallas, Texas, 214-480-4455, b-patton@ti.com 2004-12-16, 11:04(-06), Billy Patton: > I have : > submit_task (...

databases from unix shell
My name is uday kiran and i am student who completed M.Tech in cs. Currently i am in bangalore i am having some doubts in unix and c++ and i request u to clarif my doubts . 1) How can we call database querys and connect to database from unix shell programming . Can this be possible. if so how . This question is asked in many interviews . I am not able to find out the answer any where . So please tell me some ways of accessing database from unix shell scripting . 2) Sir how can we access database from c and c++ .. This is also asked in interviews . can u suggest me some good books regardi...

bash as login shell
5.0.7 with all update and maintenance packs, security patches I would like to change the login shell to bash for both root and my regular account, but no explicit choice for bash occurs in the drop down list on Account Manager. A test user account was created, "/usr/bash" was entered manually for login shell, "add shell environment files to home directory" was checked, and the account login seems to proceed normally to a functioning bash shell. I had previously invoked bash from a command prompt. The dropdown list of shell choices includes /usr/bash for the new user, but not other users after selecting "change login shell" in Account manager. I assume that manual entry of "/usr/bash" and checking the shell environment option would work for previous users as well. Is this approach likely to cause any problems? Thanks, David P. Lurie David P. Lurie typed (on Mon, Feb 23, 2004 at 10:18:53PM -0500): | 5.0.7 with all update and maintenance packs, security patches | | I would like to change the login shell to bash for both root and my regular | account, but no explicit choice for bash occurs in the drop down list on | Account Manager. | | A test user account was created, "/usr/bash" was entered manually for login | shell, "add shell environment files to home directory" was checked, and the | account login seems to proceed normally to a functioning bash shell. I had | previously invoked bash from a command prom...

Linux/unix Shell
Hi, Is there any linux/unix shell that also works in windows..actually i used telnet or Putty to login to my unix development machine and get the console which is single is there a similiar utility like putty/telent but give me multiple shell/consoles. if anybody knows please let me know about that. rgrds, Munish emanshu wrote: > Hi, > > Is there any linux/unix shell that also works in windows..actually i > used telnet or Putty to login to my unix development machine and get > the console which is single is there a similiar utility like > putty/telent but give me multiple shell/consoles. > if anybody knows please let me know about that. > rgrds, > Munish > have you tried opening multiple instances of either of these or hyperterminal? -- JosephKK Gegen dummheit kampfen Die Gotter Selbst, vergebens. --Shiller On 19 Jan 2006 04:35:26 -0800, emanshu <munish.nr@gmail.com> wrote: > Hi, > > Is there any linux/unix shell that also works in windows..actually i > used telnet or Putty to login to my unix development machine and get > the console which is single is there a similiar utility like > putty/telent but give me multiple shell/consoles. > if anybody knows please let me know about that. > rgrds, > Munish > Try screen. -- <apt> it has been said that redhat is the thing Marc Ewing wears on his head. -----BEGIN PGP SIGNED MESSAGE----- Hash:...

free unix shells
Are there any free unix shell accounts out there that someone can use to practice shell scripting etc? On Thu, 23 Mar 2006, nick wrote: > Are there any free unix shell accounts out there that someone can use > to practice shell scripting etc? Yes: your own computer. Download Solaris (free) and install it. -- Rich Teer, SCNA, SCSA, OpenSolaris CAB member President, Rite Online Inc. Voice: +1 (250) 979-1638 URL: http://www.rite-group.com/rich nick wrote: > Are there any free unix shell accounts out there that someone can use > to practice shell scripting etc? > Why not just by a cheap Sun box on e-Bay? Some come with Solaris installed or you can buy a Solaris 10 media kit for $40 or download it for free. For that matter, I have an old Ultra 5, 360MHz Sparc, 256MB, ~9GB disk, CD-ROM, floppy I'd part with. You'd have to find a Sun keyboard and mouse for it and a monitor. I bought it to practice newly acquired Sysadmin skills and have since upgraded to a trio of Ultra 10s. nick wrote: > Are there any free unix shell accounts out there that someone can use > to practice shell scripting etc? In addition to what others have suggested (buying a cheap Sun or running Solaris x86 on your PC), take a look at the HP testdrive http://www.testdrive.hp.com/ There you will find free accounts on machines running Linux, HP-UX, tru64, FreeBSD etc. You will have to read the conditions of their use, but I can't see anyone complaining abo...

unix shell script
Hi all, I am in need of a shell script which will cat all the audit files into one log and then email. Thanks. Muddu "muddu" <mudassar.dba@gmail.com> wrote in message news:1156371761.252866.267670@b28g2000cwb.googlegroups.com... > Hi all, > > I am in need of a shell script which will cat all the audit files into > one log and then email. > "cat *.aud" will merge all files together. You can redirect it to a file and/or email. Thank you very much for your hint. Regards. Muddu Bob Jones wrote: > "muddu" <mudassar.dba@gmail.com> wrote in message > news:1156371761.252866.267670@b28g2000cwb.googlegroups.com... > > Hi all, > > > > I am in need of a shell script which will cat all the audit files into > > one log and then email. > > > > "cat *.aud" will merge all files together. You can redirect it to a file > and/or email. ...

Unix without shell
Another thread (on find ... -exec ...) got me thinking about "pure Unix", i.e. Unix without a shell. I realize that, with the exception of the occasional find ... -exec ..., and maybe at login time, I don't know of any other instance in which I interact with Unix without some shell's intervention. In fact I don't have a very clear idea of where the shell ends and Unix begins. I want to remedy this situation. I'm sure it's a royal pain to deal with Unix without the services of a shell, but I would like to experience it first hand. Is it possible? I suppose...

shell scripting with bash
Hello there. I'm quite new to unix/linux OSs. I've got a task which is to look through a file for a line containing a specific word and erase it if it exists. After that, I need to insert a new line. Can this be peformed by a *.sh file using bash? If it not to much trouble, please provide such a script and links to a good web site on such a subject. yonatan@il.nlayers.com (Jonathan Taub) writes: > Hello there. > > I'm quite new to unix/linux OSs. > I've got a task which is to look through a file for a line containing > a specific word and erase it if it exist...

Web resources about - bash: Where Am I? - comp.unix.shell

Resources last updated: 3/29/2016 10:54:40 PM