-1

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?

  • 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. – Julie Pelletier Feb 7 '17 at 5:21
  • Your only other option AFAIK is to pass $0 to show_help. – PM 2Ring Feb 7 '17 at 5:30
  • @PM2Ring: That's an interesting alternative indeed. If you post that as an answer, I'll upvote it. – Julie Pelletier Feb 7 '17 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? – Michael Homer Feb 7 '17 at 9:11
2

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.

  • That's it. I found that it's Korn Shell's special handling that caused this. – iBug Feb 7 '17 at 8:35
-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.

  • It's not working. That link is actually poiting to /bin/dash which is also the target of /bin/sh. – iBug Feb 7 '17 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. – dave_thompson_085 Feb 7 '17 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.