Re: rcctl: find(1) service files in /etc/rc.d

2014-10-15 Thread Vadim Zhukov
2014-10-15 13:11 GMT+04:00 Craig R. Skinner :
> On 2014-10-14 Tue 00:24 AM |, Antoine Jacoutot wrote:
>>
>> Makes sense yes. Not sure I'd want a function just for that one liner though.
>> I'll commit something tomorrow.
>>
>
> Nice one, using shell internals.
>
> This restricts the listing to files which are also executable:
>
>
> Index: rcctl.sh
> ===
> RCS file: /cvs/src/usr.sbin/rcctl/rcctl.sh,v
> retrieving revision 1.45
> diff -u -p -r1.45 rcctl.sh
> --- rcctl.sh15 Oct 2014 07:38:24 -  1.45
> +++ rcctl.sh15 Oct 2014 09:01:35 -
> @@ -39,10 +39,12 @@ needs_root()
>  ls_rcscripts() {
> local _s
>
> -   cd /etc/rc.d && set -- *
> +   cd /etc/rc.d || exit
> +   set -- *
> for _s; do
> [ "${_s}" = "rc.subr" ] && continue
> -   [ ! -d "${_s}" ] && echo "${_s}"
> +   [[ -d "${_s}" ]] && continue
> +   [[ -f "${_s}" && -x "${_s}" ]] && echo "${_s}"
> done
>  }

It's mostly nitpicking in that particular case (I don't think
/etc/rc.d ought to be large enough), but, anyway, I usually prefer to
use "ls -f | while read" idiom instead of "set -- *; for". The first
way is more Unix-style as it's stream-oriented, i.e., doesn't keep the
whole list in memory.

For the "|| exit" part - maybe rcctl.sh wants to have "set -e" instead?

--
  WBR,
  Vadim Zhukov



Re: rcctl: find(1) service files in /etc/rc.d

2014-10-15 Thread Antoine Jacoutot
On Wed, Oct 15, 2014 at 10:11:18AM +0100, Craig R. Skinner wrote:
> On 2014-10-14 Tue 00:24 AM |, Antoine Jacoutot wrote:
> > 
> > Makes sense yes. Not sure I'd want a function just for that one liner 
> > though.
> > I'll commit something tomorrow.
> > 
> 
> Nice one, using shell internals.
> 
> This restricts the listing to files which are also executable:

I don't think that's necessary.

> Index: rcctl.sh
> ===
> RCS file: /cvs/src/usr.sbin/rcctl/rcctl.sh,v
> retrieving revision 1.45
> diff -u -p -r1.45 rcctl.sh
> --- rcctl.sh  15 Oct 2014 07:38:24 -  1.45
> +++ rcctl.sh  15 Oct 2014 09:01:35 -
> @@ -39,10 +39,12 @@ needs_root()
>  ls_rcscripts() {
>   local _s
>  
> - cd /etc/rc.d && set -- *
> + cd /etc/rc.d || exit
> + set -- *
>   for _s; do
>   [ "${_s}" = "rc.subr" ] && continue
> - [ ! -d "${_s}" ] && echo "${_s}"
> + [[ -d "${_s}" ]] && continue
> + [[ -f "${_s}" && -x "${_s}" ]] && echo "${_s}"
>   done
>  }
>  
> 

-- 
Antoine



Re: rcctl: find(1) service files in /etc/rc.d

2014-10-15 Thread Craig R. Skinner
On 2014-10-14 Tue 00:24 AM |, Antoine Jacoutot wrote:
> 
> Makes sense yes. Not sure I'd want a function just for that one liner though.
> I'll commit something tomorrow.
> 

Nice one, using shell internals.

This restricts the listing to files which are also executable:


Index: rcctl.sh
===
RCS file: /cvs/src/usr.sbin/rcctl/rcctl.sh,v
retrieving revision 1.45
diff -u -p -r1.45 rcctl.sh
--- rcctl.sh15 Oct 2014 07:38:24 -  1.45
+++ rcctl.sh15 Oct 2014 09:01:35 -
@@ -39,10 +39,12 @@ needs_root()
 ls_rcscripts() {
local _s
 
-   cd /etc/rc.d && set -- *
+   cd /etc/rc.d || exit
+   set -- *
for _s; do
[ "${_s}" = "rc.subr" ] && continue
-   [ ! -d "${_s}" ] && echo "${_s}"
+   [[ -d "${_s}" ]] && continue
+   [[ -f "${_s}" && -x "${_s}" ]] && echo "${_s}"
done
 }
 



Re: rcctl: find(1) service files in /etc/rc.d

2014-10-13 Thread Antoine Jacoutot
On Mon, Oct 13, 2014 at 09:31:05PM +0100, Craig R. Skinner wrote:
> Move 2 duplicate searches into a function.
> 
> The diff also ignores (RCS) subdirectories.
> 
> $ find /etc/rc.d ! -type f
> /etc/rc.d
> /etc/rc.d/RCS

Makes sense yes. Not sure I'd want a function just for that one liner though.
I'll commit something tomorrow.

> Index: rcctl.sh
> ===
> RCS file: /cvs/src/usr.sbin/rcctl/rcctl.sh,v
> retrieving revision 1.43
> diff -u -p -r1.43 rcctl.sh
> --- rcctl.sh  11 Oct 2014 19:12:19 -  1.43
> +++ rcctl.sh  13 Oct 2014 20:10:34 -
> @@ -93,7 +93,7 @@ svc_get_defaults()
>   print -r -- "$(svc_default_enabled_flags ${_svc})"
>   svc_default_enabled ${_svc}
>   else
> - for _i in $(ls -A /etc/rc.d | grep -v rc.subr); do
> + get_svc_list | while read _i; do
>   echo "${_i}_flags=$(svc_default_enabled_flags ${_i})"
>   done
>   for _i in ${_special_services}; do
> @@ -134,7 +134,7 @@ svc_get_status()
>   svc_get_flags ${_svc}
>   svc_is_enabled ${_svc}
>   else
> - for _i in $(ls -A /etc/rc.d | grep -v rc.subr); do
> + get_svc_list | while read _i; do
>   echo "${_i}_flags=$(svc_get_flags ${_i})"
>   done
>   for _i in ${_special_services}; do
> @@ -175,6 +175,12 @@ svc_is_special()
>   [ -n "${_svc}" ] || return
>  
>   echo ${_special_services} | grep -qw ${_svc}
> +}
> +
> +get_svc_list()
> +{
> + # Ignore rc.subr & (RCS) subdirectories:
> + find /etc/rc.d -type f -maxdepth 1 ! -name rc.subr
>  }
>  
>  append_to_pkg_scripts()
> 

-- 
Antoine



rcctl: find(1) service files in /etc/rc.d

2014-10-13 Thread Craig R. Skinner
Move 2 duplicate searches into a function.

The diff also ignores (RCS) subdirectories.

$ find /etc/rc.d ! -type f
/etc/rc.d
/etc/rc.d/RCS


Index: rcctl.sh
===
RCS file: /cvs/src/usr.sbin/rcctl/rcctl.sh,v
retrieving revision 1.43
diff -u -p -r1.43 rcctl.sh
--- rcctl.sh11 Oct 2014 19:12:19 -  1.43
+++ rcctl.sh13 Oct 2014 20:10:34 -
@@ -93,7 +93,7 @@ svc_get_defaults()
print -r -- "$(svc_default_enabled_flags ${_svc})"
svc_default_enabled ${_svc}
else
-   for _i in $(ls -A /etc/rc.d | grep -v rc.subr); do
+   get_svc_list | while read _i; do
echo "${_i}_flags=$(svc_default_enabled_flags ${_i})"
done
for _i in ${_special_services}; do
@@ -134,7 +134,7 @@ svc_get_status()
svc_get_flags ${_svc}
svc_is_enabled ${_svc}
else
-   for _i in $(ls -A /etc/rc.d | grep -v rc.subr); do
+   get_svc_list | while read _i; do
echo "${_i}_flags=$(svc_get_flags ${_i})"
done
for _i in ${_special_services}; do
@@ -175,6 +175,12 @@ svc_is_special()
[ -n "${_svc}" ] || return
 
echo ${_special_services} | grep -qw ${_svc}
+}
+
+get_svc_list()
+{
+   # Ignore rc.subr & (RCS) subdirectories:
+   find /etc/rc.d -type f -maxdepth 1 ! -name rc.subr
 }
 
 append_to_pkg_scripts()