0

Say I've got this:

#!/bin/sh
function show_help {
  cat<<%
Usage: $0 [-h]
%
}
# the following lines is pseudo-code
if argument contains "-h"
  show_help
otherwise
  do_stuff

If I run ./test it does some stuff as intended, but if I run ./test -h, it produces

Usage: show_help [-h]

but I intended to let it produce

Usage: ./test [-h]

so how can I achieve this by modifying only the show_help function? I don't want to modify the script itself, so I won't just add SCRIPT_NAME=$0 under the shebang line.
I hope the solution is some kind of a builtin variable like $PWD or function like pwd, does there really exist one?

4
  • 3
    Since you prevent us from giving you a good solution, what is the point of asking the question? You already know the best way to do it. Any other solution will be ugly as hell. Feb 7, 2017 at 5:21
  • Your only other option AFAIK is to pass $0 to show_help.
    – PM 2Ring
    Feb 7, 2017 at 5:30
  • @PM2Ring: That's an interesting alternative indeed. If you post that as an answer, I'll upvote it. Feb 7, 2017 at 5:32
  • How on earth can using ksh cause the problem when /bin/sh is linked to dash and the question is tagged bash? Feb 7, 2017 at 9:11

3 Answers 3

3

I can't reproduce this. The question is tagged with '/bash'.

With bash, $0 is always the name of the script, so if this is in /tmp/test

#!/bin/bash
function show_help {
cat <<%
Usage: $0 [-h]
%
}
show_help

then bash /tmp/test gives me Usage: /tmp/test [-h].

If I use ksh93 /tmp/test I do get Usage: show_help [-h], due to the ksh setting $0 when you declare a function in a non POSIX manner.

Switching to a portable function declaration

#!/bin/bash
show_help() {
cat <<%
Usage: $0 [-h]
%
}
show_help

and you get Usage: /tmp/test [-h] from both ksh and bash.

So there are multiple errors in the original script. The #!/bin/sh should be #!/bin/bash if the question is about bash, the function declaration is incorrect, and the syntax of the if at the end is just wrong.

1
  • That's it. I found that it's Korn Shell's special handling that caused this.
    – iBug
    Feb 7, 2017 at 8:35
0

In BASH, use $FUNCNAME[1]. $FUNCNAME[0] is the show_help. You an also get the line number in the parent function that called show_help as well as the name of the source file ($BASH_LINENO[1], $BASH_SOURCE[1]).

I am looking for a similar answer for korn shell (ksh)

-1

Since this question seems to be homework question. So I am not answering in a exact way but putting some hints down.

  1. Get the parent PID of running process from grep PPid /proc/self/status | awk '{print $2}
  2. Find out it's executable name ls /proc/<PPid>/exe
  3. Use readlink to print out the name of above softlink readlink /proc/<PPid>/exe

Output of 3 is the name of running script.

2
  • It's not working. That link is actually poiting to /bin/dash which is also the target of /bin/sh.
    – iBug
    Feb 7, 2017 at 6:18
  • 1
    Not all systems supporting bash have /proc and all Bourne-style shells including bash (and at least tcsh also) have a special variable $$ giving the shell's pid. But as noted, the executable for the shell process /proc/$$/exe is the shell not the script; the script (if any) will be open on some fd but IME not always the same one. Feb 7, 2017 at 8:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .