| Author |
Why the different output.
|
|
|
| I'm brand new to sed and awk so please bear with me.
What I am trying to do is make sure a script is executed from the directory
where it resides as opposed to executing it by using the full path name.
This has led me to questions in sed and awk and things I don't understand.
I want to have the script tell the user to run test.sh from the directory
where it resides as opposed to executing it by typing the full path name.
ie. Please execute this script by typing ./test.sh instead of
/home/rick/myscripts/test.sh
I need help understanding why the different output is produced by running
this script in the following two different ways. I understand the first
output but the second output has me scratching my head.
#!/bin/sh
echo $0
echo $0 | sed 's/\// /g' | awk '{ print $0 }'
echo $0 | sed 's/\// /g' | awk '{ print $1}'
Running /home/rick/myscripts/test.sh gives me the following output:
/home/rick/myscripts/test.sh
home rick myscripts test.sh
home
However running ./test.sh gives me the following output:
../test.sh
.. test.sh
..
Thanks
Rick
| |
| Chris F.A. Johnson 2005-02-19, 3:55 pm |
| On Sat, 19 Feb 2005 at 17:17 GMT, REM wrote:
> I'm brand new to sed and awk so please bear with me.
>
> What I am trying to do is make sure a script is executed from the directory
> where it resides as opposed to executing it by using the full path name.
> This has led me to questions in sed and awk and things I don't understand.
>
> I want to have the script tell the user to run test.sh from the directory
> where it resides as opposed to executing it by typing the full path name.
> ie. Please execute this script by typing ./test.sh instead of
> /home/rick/myscripts/test.sh
Why? It's not a good idea. Just have it check for the current
directory.
> I need help understanding why the different output is produced by running
> this script in the following two different ways. I understand the first
> output but the second output has me scratching my head.
>
> #!/bin/sh
> echo $0
> echo $0 | sed 's/\// /g' | awk '{ print $0 }'
> echo $0 | sed 's/\// /g' | awk '{ print $1}'
>
> Running /home/rick/myscripts/test.sh gives me the following output:
> /home/rick/myscripts/test.sh
> home rick myscripts test.sh
> home
>
> However running ./test.sh gives me the following output:
> ./test.sh
> . test.sh
> .
What don't you understand? This output looks perfectly normal.
If you run the script as ./test.sh, then that's what is in $0.
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
|
|
"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message
news:37pce9F5hkmj8U1@individual.net...
> On Sat, 19 Feb 2005 at 17:17 GMT, REM wrote:
>
> Why? It's not a good idea. Just have it check for the current
> directory.
>
The script is on a CD that the user may mount at /mnt/cdrom or /mnt or
/media/cdrecorder and there are tar files are on that CD that the script has
to extract. Without knowing where the user will be mounting this CD and
running the script from I am not sure how to tell the script where to find
the tar files. I am sure someone with more experience at scripting could
probably do this in a heartbeat.
>
> What don't you understand? This output looks perfectly normal.
>
> If you run the script as ./test.sh, then that's what is in $0.
OK I see what I've done.
>
>
> --
> Chris F.A. Johnson http://cfaj.freeshell.org/shell
> ========================================
===========================
> My code (if any) in this post is copyright 2005, Chris F.A. Johnson
> and may be copied under the terms of the GNU General Public License
| |
| Chris F.A. Johnson 2005-02-19, 3:55 pm |
| On Sat, 19 Feb 2005 at 18:15 GMT, REM wrote:
>
> "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message
> news:37pce9F5hkmj8U1@individual.net...
> The script is on a CD that the user may mount at /mnt/cdrom or /mnt or
> /media/cdrecorder and there are tar files are on that CD that the script has
> to extract. Without knowing where the user will be mounting this CD and
> running the script from I am not sure how to tell the script where to find
> the tar files. I am sure someone with more experience at scripting could
> probably do this in a heartbeat.
At the top of the script (POSIX shell):
cd "${0%/*}"
Or, for a Bourne shell:
cd "`dirname $0`"
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Ulrich M. Schwarz 2005-02-20, 8:55 am |
| "REM" <rem@rem.com> writes:
[...]
> The script is on a CD that the user may mount at /mnt/cdrom or /mnt or
> /media/cdrecorder and there are tar files are on that CD that the script has
> to extract. Without knowing where the user will be mounting this CD and
[...]
I suggest assuming some default and having a command-line parameter to
override it (best along with an error message to that effect if the
files aren't found).
Ulrich
--
Die Wachstuben in den Wachstuben werden durch Kunststoffflaschen
ersetzt, die durch Ziehen an den Kunststofflaschen geoeffnet werden.
(c) 1994, Werner Icking
| |
|
| >
> At the top of the script (POSIX shell):
>
> cd "${0%/*}"
>
> Or, for a Bourne shell:
>
> cd "`dirname $0`"
>
This is on a SCO v5.0.6 machine and claims to be POSIX compliant.
Here is what I came up with and it seems to work. Thanks for all your help.
Still wondering if there is a way to do this using command line sed and awk?
if [ $0 = test.sh -o $0 = ./test.sh ]
then
scriptdir=`pwd`
else
cd "`dirname $0`"
scriptdir=`pwd`
fi
Thanks
Rick
|
|
|
|