4

I'm trying to find out which is the name of the service script that is running at startup. I need the name for calling the default script afterwards.

I can't assume the name in advance, that's why I have to get it during its execution.

NAME=${0##*/}
SCRIPTNAME=/etc/init.d/$NAME

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

I've tried differents approaches:

  • Use $BASH_SOURCE
  • Use basename $0
  • Use bash specific var ${0##*/}

All of them works if I run the script manually from a shell but not after a restart. I suppose that init.d launch process in a different way.

I'm using Debian Lenny (I know, quite old...) and bash v3.2.39. I'm opened to use other type of shell if necessary.

Thanks.

RicardoPB
  • 61
  • 3
  • How did they each fail? I'd expect `BASH_SOURCE` to have failed as the script is likely run by `/bin/sh` and so that variable likely wasn't set. But I'd have expected using `$0` to work (though have an `S##` or `K##` prefix you'll need to deal with). – Etan Reisner Apr 20 '15 at 11:35
  • I really don't understand what you want. @EtanReisner do you ? Could you please explain it to me ? –  Apr 20 '15 at 12:43
  • Thanks!! I reply in a new answer in order to have better formatting. – RicardoPB Apr 20 '15 at 12:58

1 Answers1

2

I wrote some extra code to know exactly the value of each variable and I found an exciting surprise.

BASH_SOURCE: /etc/rc2.d/S99iap_FA_iapfa

$0: /etc/rc2.d/S99iap_FA_iapfa

${0##*/}: S99iap_FA_iapfa

Vars are not empty, but the rc symlink name. I've written a simple workaround and know it seems to work independently it's executed from rc symlinks or the service itself.

RC_NAME=${0##*/}
NAME="${RC_NAME#[SK][0-9][0-9]}"

SCRIPTNAME=/etc/init.d/$NAME

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
RicardoPB
  • 61
  • 3