jmar83
September 7, 2020, 11:36am
1
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
mikma
September 7, 2020, 12:01pm
2
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
jmar83
September 7, 2020, 1:55pm
3
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...
jmar83
September 7, 2020, 1:57pm
4
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:~#
jmar83
September 7, 2020, 1:59pm
5
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...
hnyman
September 7, 2020, 6:24pm
6
OpenWrt does not use bash by default.
The default shell is "ash", and it does not have bash-specific extensions.
1 Like
hnyman
September 7, 2020, 6:37pm
7
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
jmar83
September 7, 2020, 6:49pm
8
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.
jmar83
September 8, 2020, 7:25am
10
OK thx. Problem seems to be solved now.
1 Like
tmomas
Closed
September 18, 2020, 7:25am
11
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.