shell script
I'm trying to write a shell script (in a tcsh) which uses a filename argument without its extension: filename=$1:r echo filename doesn't work. What is the right way to do it? ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: shell script
On Dec 30, 2012, at 9:54 PM, William Ehrich wrote: > I'm trying to write a shell script (in a tcsh) which uses a filename argument > without its extension: > > filename=$1:r > echo filename > > doesn't work. What is the right way to do it? Not sure about tcsh, but this will do it in bash: $ filename='foo.txt' $ echo $filename | sed s/\\..*$// foo If you need foo.bar.txt to to return foo.bar you'll need something a bit stronger... ./s ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: shell script
Like so: sed s/\\.[^.]*$// On Monday, December 31, 2012, Scot Hacker wrote: > > On Dec 30, 2012, at 9:54 PM, William Ehrich > > wrote: > > > I'm trying to write a shell script (in a tcsh) which uses a filename > argument without its extension: > > > > filename=$1:r > > echo filename > > > > doesn't work. What is the right way to do it? > > Not sure about tcsh, but this will do it in bash: > > $ filename='foo.txt' > $ echo $filename | sed s/\\..*$// > foo > > If you need foo.bar.txt to to return foo.bar you'll need something a bit > stronger... > > ./s > > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk > -- Jared Earle :: There is no SPORK jea...@gmail.com :: http://jearle.eu Hosting :: http://cat5.org Blog :: http://blog.23x.net ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: shell script
Do you know the extension ahead of time? If so, you can use 'basename'. FILE=path/foo.txt BASE="$(basename "$FILE" .txt)" It also strips off the path so BASE is now "foo". If you want the directory you can use 'dirname'. On Mon, Dec 31, 2012 at 12:54 AM, William Ehrich wrote: > I'm trying to write a shell script (in a tcsh) which uses a filename > argument without its extension: > > filename=$1:r > echo filename > > doesn't work. What is the right way to do it? > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk -- arno s hautala/-| a...@alum.wpi.edu pgp b2c9d448 ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: shell script
I wrote: I'm trying to write a shell script (in a tcsh) which uses a filename argument without its extension: filename=$1:r echo filename doesn't work. What is the right way to do it? This works (it just needed the #!): #! echo $1:r ~/pp > shellscript boo.com boo ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Can a shell script easily find itself?
I feel silly for asking this. But I just realized I don't try this very often. Is there a way for a shell script to find itself? Or more precisely, the directory it is in? I am trying to run a program that wants an ini file specified on the command line; but it defaults to the assumption of having its config file in /etc unless you tell it where it is. And rather than a one-line script that hard codes a directory, I'd rather that it (the script) can tell where it is located, to use an ini file there. (Yea, a one-line script to just pass a config file argument to a program.) --- This message was composed with the aid of a laptop cat, and no mouse ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
Use the command "pwd". On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: > I feel silly for asking this. But I just realized I don't try this very > often. > > Is there a way for a shell script to find itself? Or more precisely, the > directory it is in? > > I am trying to run a program that wants an ini file specified on the > command line; but it defaults to the assumption of having its config file > in /etc unless you tell it where it is. And rather than a one-line script > that hard codes a directory, I'd rather that it (the script) can tell where > it is located, to use an ini file there. > > (Yea, a one-line script to just pass a config file argument to a program.) > > --- > This message was composed with the aid of a laptop cat, and no mouse > > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk > -- Best Regards, John Musbach ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
> Use the command "pwd". Nope. That tells me where the user is, not where the shell script is. > On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: > I feel silly for asking this. But I just realized I don't try this very often. > > Is there a way for a shell script to find itself? Or more precisely, the > directory it is in? > > I am trying to run a program that wants an ini file specified on the command > line; but it defaults to the assumption of having its config file in /etc > unless you tell it where it is. And rather than a one-line script that hard > codes a directory, I'd rather that it (the script) can tell where it is > located, to use an ini file there. > > (Yea, a one-line script to just pass a config file argument to a program.) > > --- > This message was composed with the aid of a laptop cat, and no mouse > > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk > > > > -- > Best Regards, > > John Musbach --- This message was composed with the aid of a laptop cat, and no mouse ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
to expand on this answer you can use ` to save the result of the command. Try this HERE=`pwd` echo $HERE so now the current path is in a variable for your script to use. On Jul 21, 2013, at 3:46 PM, John Musbach wrote: > Use the command "pwd". > > > On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: > I feel silly for asking this. But I just realized I don't try this very often. > > Is there a way for a shell script to find itself? Or more precisely, the > directory it is in? > > I am trying to run a program that wants an ini file specified on the command > line; but it defaults to the assumption of having its config file in /etc > unless you tell it where it is. And rather than a one-line script that hard > codes a directory, I'd rather that it (the script) can tell where it is > located, to use an ini file there. > > (Yea, a one-line script to just pass a config file argument to a program.) > > --- > This message was composed with the aid of a laptop cat, and no mouse > > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk > > > > -- > Best Regards, > > John Musbach > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk -- Bob Love "Atheism is a non prophet organization." ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
On Sun, Jul 21, 2013 at 4:54 PM, Michael wrote: > > Use the command "pwd". > > > Nope. That tells me where the user is, not where the shell script is. > Are you sure? If you do say, SCRIPT_DIR=`pwd` echo "$SCRIPT_DIR" the echo should return the directory the script ran in. > On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: > >> I feel silly for asking this. But I just realized I don't try this very >> often. >> >> Is there a way for a shell script to find itself? Or more precisely, the >> directory it is in? >> >> I am trying to run a program that wants an ini file specified on the >> command line; but it defaults to the assumption of having its config file >> in /etc unless you tell it where it is. And rather than a one-line script >> that hard codes a directory, I'd rather that it (the script) can tell where >> it is located, to use an ini file there. >> >> (Yea, a one-line script to just pass a config file argument to a program.) >> >> --- >> This message was composed with the aid of a laptop cat, and no mouse >> >> ___ >> MacOSX-talk mailing list >> MacOSX-talk@omnigroup.com >> http://www.omnigroup.com/mailman/listinfo/macosx-talk >> > > > > -- > Best Regards, > > John Musbach > > > --- > This message was composed with the aid of a laptop cat, and no mouse > > -- Best Regards, John Musbach ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
>> Use the command "pwd". > > Nope. That tells me where the user is, not where the shell script is. > > Are you sure? If you do say, > > SCRIPT_DIR=`pwd` > echo "$SCRIPT_DIR" > > the echo should return the directory the script ran in. 100% sure. keybounceMBP:Applications michael$ pwd /Users/michael/Applications keybounceMBP:Applications michael$ cat ~/bin/testdir #!/bin/sh pwd SCRIPT_DIR=`pwd` echo "$SCRIPT_DIR" keybounceMBP:Applications michael$ testdir /Users/michael/Applications /Users/michael/Applications keybounceMBP:Applications michael$ I want it to return my ~/bin directory in this case. >> On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: >> I feel silly for asking this. But I just realized I don't try this very >> often. >> >> Is there a way for a shell script to find itself? Or more precisely, the >> directory it is in? >> >> I am trying to run a program that wants an ini file specified on the command >> line; but it defaults to the assumption of having its config file in /etc >> unless you tell it where it is. And rather than a one-line script that hard >> codes a directory, I'd rather that it (the script) can tell where it is >> located, to use an ini file there. >> >> (Yea, a one-line script to just pass a config file argument to a program.) >> >> --- >> This message was composed with the aid of a laptop cat, and no mouse >> >> ___ >> MacOSX-talk mailing list >> MacOSX-talk@omnigroup.com >> http://www.omnigroup.com/mailman/listinfo/macosx-talk >> >> >> >> -- >> Best Regards, >> >> John Musbach > > --- > This message was composed with the aid of a laptop cat, and no mouse > > > > > -- > Best Regards, > > John Musbach --- This message was composed with the aid of a laptop cat, and no mouse ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
Hm, here is a discussion on Stack Overflow of your question. http://online.wsj.com/article/SB10001424127887324348504578606493979321554.html On Jul 21, 2013, at 4:23 PM, Michael wrote: >>> Use the command "pwd". >> >> Nope. That tells me where the user is, not where the shell script is. >> >> Are you sure? If you do say, >> >> SCRIPT_DIR=`pwd` >> echo "$SCRIPT_DIR" >> >> the echo should return the directory the script ran in. > > 100% sure. > > keybounceMBP:Applications michael$ pwd > /Users/michael/Applications > keybounceMBP:Applications michael$ cat ~/bin/testdir > #!/bin/sh > > pwd > > SCRIPT_DIR=`pwd` > echo "$SCRIPT_DIR" > > keybounceMBP:Applications michael$ testdir > /Users/michael/Applications > /Users/michael/Applications > keybounceMBP:Applications michael$ > > I want it to return my ~/bin directory in this case. > > >>> On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: >>> I feel silly for asking this. But I just realized I don't try this very >>> often. >>> >>> Is there a way for a shell script to find itself? Or more precisely, the >>> directory it is in? >>> >>> I am trying to run a program that wants an ini file specified on the >>> command line; but it defaults to the assumption of having its config file >>> in /etc unless you tell it where it is. And rather than a one-line script >>> that hard codes a directory, I'd rather that it (the script) can tell where >>> it is located, to use an ini file there. >>> >>> (Yea, a one-line script to just pass a config file argument to a program.) >>> >>> --- >>> This message was composed with the aid of a laptop cat, and no mouse >>> >>> ___ >>> MacOSX-talk mailing list >>> MacOSX-talk@omnigroup.com >>> http://www.omnigroup.com/mailman/listinfo/macosx-talk >>> >>> >>> >>> -- >>> Best Regards, >>> >>> John Musbach >> >> --- >> This message was composed with the aid of a laptop cat, and no mouse >> >> >> >> >> -- >> Best Regards, >> >> John Musbach > > --- > This message was composed with the aid of a laptop cat, and no mouse > > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk -- Bob Love "Women can keep a secret just as well as men, but it takes more of them to do it." ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
On Jul 21, 2013, at 1:57 PM, John Musbach wrote: > > > On Sun, Jul 21, 2013 at 4:54 PM, Michael wrote: > >> Use the command "pwd". > > Nope. That tells me where the user is, not where the shell script is. > > Are you sure? If you do say, > > SCRIPT_DIR=`pwd` > echo "$SCRIPT_DIR" > > the echo should return the directory the script ran in. Note that my original response distinguished between the directory the script is run from and the directory containing the script: #!/bin/bash current_dir=$(pwd) script_dir=$(dirname $0) echo $current_dir echo $script_dir ./s -- >>> Birdhouse Hosting <<< Custom web and mail hosting services http://hosting.birdhouse.org d(-_-)b ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
> Hm, here is a discussion on Stack Overflow of your question. > > http://online.wsj.com/article/SB10001424127887324348504578606493979321554.html I think that's the wrong paste :-). But in response to that article: Our constitution puts the U.S. Supreme court at the top; international courts would only have a valid say if they were inferior to the Supreme Court. And congress cannot modify the constitution by treaty; a treaty that violates the constitution is not valid. http://StrictConstitution.BlogSpot.com -- my own blog on that and similar issues. > On Jul 21, 2013, at 4:23 PM, Michael wrote: > >>>> Use the command "pwd". >>> >>> Nope. That tells me where the user is, not where the shell script is. >>> >>> Are you sure? If you do say, >>> >>> SCRIPT_DIR=`pwd` >>> echo "$SCRIPT_DIR" >>> >>> the echo should return the directory the script ran in. >> >> 100% sure. >> >> keybounceMBP:Applications michael$ pwd >> /Users/michael/Applications >> keybounceMBP:Applications michael$ cat ~/bin/testdir >> #!/bin/sh >> >> pwd >> >> SCRIPT_DIR=`pwd` >> echo "$SCRIPT_DIR" >> >> keybounceMBP:Applications michael$ testdir >> /Users/michael/Applications >> /Users/michael/Applications >> keybounceMBP:Applications michael$ >> >> I want it to return my ~/bin directory in this case. >> >> >>>> On Sun, Jul 21, 2013 at 4:16 PM, Michael wrote: >>>> I feel silly for asking this. But I just realized I don't try this very >>>> often. >>>> >>>> Is there a way for a shell script to find itself? Or more precisely, the >>>> directory it is in? >>>> >>>> I am trying to run a program that wants an ini file specified on the >>>> command line; but it defaults to the assumption of having its config file >>>> in /etc unless you tell it where it is. And rather than a one-line script >>>> that hard codes a directory, I'd rather that it (the script) can tell >>>> where it is located, to use an ini file there. >>>> >>>> (Yea, a one-line script to just pass a config file argument to a program.) >>>> >>>> --- >>>> This message was composed with the aid of a laptop cat, and no mouse >>>> >>>> ___ >>>> MacOSX-talk mailing list >>>> MacOSX-talk@omnigroup.com >>>> http://www.omnigroup.com/mailman/listinfo/macosx-talk >>>> >>>> >>>> >>>> -- >>>> Best Regards, >>>> >>>> John Musbach >>> >>> --- >>> This message was composed with the aid of a laptop cat, and no mouse >>> >>> >>> >>> >>> -- >>> Best Regards, >>> >>> John Musbach >> >> --- >> This message was composed with the aid of a laptop cat, and no mouse >> >> ___ >> MacOSX-talk mailing list >> MacOSX-talk@omnigroup.com >> http://www.omnigroup.com/mailman/listinfo/macosx-talk > > > -- > Bob Love > "Women can keep a secret just as well as men, but it takes more of them to do > it." > > --- This message was composed with the aid of a laptop cat, and no mouse ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
>> Hm, here is a discussion on Stack Overflow of your question. >> >> http://online.wsj.com/article/SB10001424127887324348504578606493979321554.html > > I think that's the wrong paste :-). Correct one: http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
On 21 Jul 2013, at 14:16 , Michael wrote: > I feel silly for asking this. But I just realized I don't try this very often. > > Is there a way for a shell script to find itself? Or more precisely, the > directory it is in? Yes, but it depends on the shell. DIR="$( cd "$( dirname "$0" )" && pwd )" will always<1> set the DIR to the path the script is in in bash, for example. That command is space safe so you will end up with "/Users/myname/My Shell Script/script One.sh” in DIR if need be. <1> FSVO of always -- There is no Humpty Dumpty, and there is no God. None, not one, no God, never was. ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk
Re: Can a shell script easily find itself?
There wasn't last time I tried this about 20 years ago. :-( The reason is the architecture of the UNIX file system, where file contents actually reside in inodes, and directory entries are really only links to inodes. For example, you create a script. Now you put a hard link to that script from two other directories. Now you ask the script "where it is." Which of the three locations do you want it to return? As far as the file system is concerned, none of them is any "more authoritative" than another. On Jul 21, 2013, at 1:16 PM, Michael wrote: > I feel silly for asking this. But I just realized I don't try this very often. > > Is there a way for a shell script to find itself? Or more precisely, the > directory it is in? > > I am trying to run a program that wants an ini file specified on the command > line; but it defaults to the assumption of having its config file in /etc > unless you tell it where it is. And rather than a one-line script that hard > codes a directory, I'd rather that it (the script) can tell where it is > located, to use an ini file there. > > (Yea, a one-line script to just pass a config file argument to a program.) > > --- > This message was composed with the aid of a laptop cat, and no mouse > > ___ > MacOSX-talk mailing list > MacOSX-talk@omnigroup.com > http://www.omnigroup.com/mailman/listinfo/macosx-talk ___ MacOSX-talk mailing list MacOSX-talk@omnigroup.com http://www.omnigroup.com/mailman/listinfo/macosx-talk