- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Basename $0 in function
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 09:34 AM
тАО12-06-2006 09:34 AM
F_Set_base ()
{
...
_ProgName=`${BASENAME} ${0} | ${AWK} -F\. '{print$1}'`
echo ${_ProgName}
...
}
Main ()
{
F_Set_base
...
}
Main $*
I get a value of _ProgName=F_Set_base instead of the script name I am running on the command line.
Thanks in advance,
Steve
Solved! Go to Solution.
- Tags:
- basename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 09:42 AM
тАО12-06-2006 09:42 AM
Re: Basename $0 in function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 09:54 AM
тАО12-06-2006 09:54 AM
Re: Basename $0 in function
change -> _ProgName=`${BASENAME} ${0} | ${AWK} -F\. '{print$1}'`
to -> _ProgName=`basename ${0} | awk -F\. '{print$1}'`
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 10:47 AM
тАО12-06-2006 10:47 AM
Re: Basename $0 in function
Copying your script as poated but adding the first three lines works as expected. That is, the basename of the file being executed is returned.
#!/usr/bin/sh
BASENAME=basename
AWK=awk
F_Set_base ()
{
_ProgName=`${BASENAME} ${0} | ${AWK} -F\. '{print$1}'`
echo ${_ProgName}
}
Main ()
{
F_Set_base
}
Main $*
...This applies equally to HP-UX and AIX.
You might try running your full script with 'set -u' to expose unset parameters; and with 'set -x' to print the command execution sequences. You could do this as:
# sh -ux ./myscript
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 03:24 PM - edited тАО10-22-2011 01:35 PM
тАО12-06-2006 03:24 PM - edited тАО10-22-2011 01:35 PM
Re: basename $0 in function
As Steve says, it returns the function name in ksh and and as James says, in the POSIX shell, it returns the name of the script.
Note: Instead of invoking basename and awk, you can replace both by using shell pattern matching:
_ProgName=${0##*/}
_ProgName=${_ProgName%.*}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2006 04:36 PM
тАО12-06-2006 04:36 PM
Re: Basename $0 in function
posix functions are defined with the syntax name(){body;}
and the function compound command syntax is function name {body;}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 12:00 AM
тАО12-08-2006 12:00 AM
Re: Basename $0 in function
The behavior you describe is from the Korn shell on HPUX 11.11.
The documentation states, within a function,
Posix shell:
... The positional parameter 0 is unchanged.
Korn shell:
... The positional parameter 0 is set to the function name.
NOTE that this appears to be a HPUX implementation issue.
The links to
.. KornShell 88
and
.. KornShell 93
on David Korn's home page, *neither* specify that $0 is set to the function name !!
The Korn shell is
... /usr/bin/ksh
.
Note that
... /usr/bin/sh
is the Posix shell, *not* the Bourne shell, which is
... /usr/old/bin/sh
.
If you force Korn shell with
#!/usr/bin/ksh
as the first line, you get
_ProgName= F_Set_base
.
If you force Posix shell with
#!/usr/bin/sh
as the first line, you get
_ProgName= script_name
.
If you do not force a specific shell at line 1 of the script, then the shell used will be the one that you are running at the time, usually the one named in /etc/passwd as your login shell. On HPUX, the system by default will use /usr/bin/sh (or, for single-user mode, /sbin/sh, which is also a Posix shell), e.g., cron or system startup scripts.
bv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 12:18 AM
тАО12-08-2006 12:18 AM
Re: Basename $0 in function
#!/usr/bin/ksh
...
export MYNAME=${0##*/}
...
function SomeTask
{
echo $MYNAME
}
...
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2006 05:13 AM
тАО12-11-2006 05:13 AM
Re: Basename $0 in function
I'm not sure why HP-UX 11.11 treats this differently than other O/S's (namely AIX and Solaris)? Although my testing on HP has so far shown no differences in my scripts between the Korn and Posix settings, I am leary of relying on the approach of just setting everything to Posix. I know this doesn't work on my tests on a Sun server (maybe /bin/sh is still Bourne shell)?
And if I understand Curt's comments, using a posix function should return the script name for $0. But isn't the code I posted at the beginning of this thread in Posix format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2006 05:36 AM
тАО12-11-2006 05:36 AM
SolutionI agree that:
#!/bin/sh
function WHOAMI
{
echo "I am $0"
}
WHOAMI
echo "I am $0"
...produces different results using HP-UX or a Linus 'bash' shell than does AIX. In HP-UX, '/usr/bin/sh' is the POSIX shell. For AIX '/usr/bin/sh' links to the Korn shell. Both, including 'bash' have Posix standards incorportated as you can see from purusing the respective manpages.
Given that their *are* subtle differences in various shells in edge cases exemplified by the script above, I prefer to use the technique Bill noted; viz. to capture the value of $0 at a shell's startup. Thus, to obtain the basename of the running code:
#!/usr/bin/sh
# typeset MYNAME=${0##*/}
...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2006 08:27 AM
тАО12-11-2006 08:27 AM
Re: Basename $0 in function
_ProgName=${0##*/}
_ProgName=${_ProgName%.*}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2006 08:53 AM
тАО12-11-2006 08:53 AM
Re: Basename $0 in function
> _ProgName=${0##*/}
> _ProgName=${_ProgName%.*}
will return $0 (the script or function name depending on the shell you are using), strip all leading directories (ie, /usr/local/bin/myscript.sh becomes myscript.sh) and the second line takes myscript.sh and strips away any trailing .stuff such as .sh or .exe or whatever. Note that the last construct strips just the last .stuff, so a script named:
myscript.ver3.4.2a
will be truncated to myscript.ver.3.4
Note that Unix has no inherent concept of a type-extension so some script writers will use .ksh or .sh but others skip the extra characters.
Bill Hassell, sysadmin