[Solved] How to get the path of a service shell script located in /etc/init.d/... dynamically?

Hi there

How to get the path of a service shell script located in /etc/init.d/... dynamically?

Header is (as usual for openwrt service scripts located in /etc/init.d):

#!/bin/sh /etc/rc.common
# chkconfig: 5 9999 9999

The code i've got (from a Debian 9 shell script with #!/bin/bash header) is:

FILE="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")";
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" && chmod +x "$DIR/$FILE";

Then, to use the full path including file name, e.g.:

echo $FILE + $DIR;

But that does not seem to work on OpenWRT 18.x. Error message:

root@OpenWrt:~# /etc/init.d/fwrules start
+ test -L /etc/rc.common
+ echo /etc/rc.common
+ basename /etc/rc.common
+ TEST_FILE=rc.common
/etc/rc.common: line 156: syntax error: bad substitution
+ cd
+ pwd
+ TEST_DIR=/root
+ chmod +x /
+ echo rc.common + /root

How to solve that? I just want to execute /etc/init.d/fwrules from EVERYWHERE while it's able to get its own path/location...

Thank you very very much for your feedbacks.

With kind regards from Switzerland,
Jan

You probably should use $initscript instead of $0 when you run /etc/rc.common in the shebang.

From the first lines of /etc/rc.common:

!/bin/sh
# Copyright (C) 2006-2012 OpenWrt.org

. $IPKG_INSTROOT/lib/functions.sh
. $IPKG_INSTROOT/lib/functions/service.sh

initscript=$1
action=${2:-help}
shift 2

Hi mikma

Doens't work:

Code:

start() {
  set -e > /dev/null 2>&1 && set -x > /dev/null 2>&1;
  SCRIPT_FILE="$(basename "$(test -L "$initscript" && readlink "$initscript" || echo "$initscript")>
  SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" > /dev/null 2>&1;
  SCRIPT_PATH="${SCRIPT_DIR}/${SCRIPT_FILE}";
  chmod 0775 $SCRIPT_PATH;

Output:

root@OpenWrt:~# /etc/init.d/fwrules start
+ test -L /etc/init.d/fwrules
+ echo /etc/init.d/fwrules
+ basename /etc/init.d/fwrules
+ SCRIPT_FILE=fwrules
+ SCRIPT_DIR=/root
+ SCRIPT_PATH=/root/fwrules
+ chmod 0775 /root/fwrules
chmod: /root/fwrules: No such file or directory

Thank you anyway...

When i remove redirection to null device on the end of line #2 in start() function:

root@OpenWrt:~# /etc/init.d/fwrules start
+ test -L /etc/init.d/fwrules
+ echo /etc/init.d/fwrules
+ basename /etc/init.d/fwrules
+ SCRIPT_FILE=fwrules
/etc/rc.common: line 156: syntax error: bad substitution
+ cd
+ pwd
+ SCRIPT_DIR=/root
+ SCRIPT_PATH=/root/fwrules
+ chmod 0775 /root/fwrules
chmod: /root/fwrules: No such file or directory
root@OpenWrt:~#

It works when i'm in /etc/init.d directory; but not when i'm somewhere.

That it works from everywhere, that's my idea... :wink:

OpenWrt does not use bash by default.
The default shell is "ash", and it does not have bash-specific extensions.

1 Like

What is the actual problem?
All init scripts are by default in /etc/init.d, so why can't you just assume that location to be a known static thing?

1 Like

Thank you. It's a general question.

With kind regards,
Jan

Init scripts are supposed to use a well known static path.
If you want to invoke some script from anywhere, put it to a suitable PATH location or adjust the PATH accordingly.
In addition, you can create symlinks as well as utilize script wrappers.

OK thx. Problem seems to be solved now.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.