Letcl is a Linux shell script that checks Let's Encrypt certificates issued for a domain. I've managed to modify it to run on FreeBSD and FreeBSD embedded systems like FreeNAS. The commands the script uses that are syntactically different between Linux and FreeBSD platforms are
The FreeBSD equivalent is:
Or, to change a date format from
...while the input format has to be specified for the FreeBSD
date -j -f '%b %d %H:%M:%S %Y %Z' "indate" +'%Y-%b-%d %H:%M %Z'[/CMD]
So, sprinkled throughout the code (the pull request is here if you'd like to take a look at it), I've got case statements that chose the appropriate command format based on $OSTYPE e.g.
The modified script works well (albeit slow). I then thought the command format is very similar for both platforms except for some switches. Could I pass those switches through variables? If I could, all the case constructs would disappear except for a single case construct early on in the code for setting up the switch variables, so, something like:
Within the body of the code, the
I've been researching and struggling for a couple of days to try to get this working and have not made any real headway. The problem arises because of the special characters in the flags being interpreted rather than being treated literally. I'm hoping the brains trust here can help me move forward as I'm stuck.
date
and sed
. For instance, consider the following Linux GNU sed command: sed ':a;N;$!ba;s/\n/ /g' file
The FreeBSD equivalent is:
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' file
Or, to change a date format from
%b %d %H:%M:%S %Y %Z
to %Y-%b-%d %H:%M %Z
, the Linux date
command will auto-detect the input format. date -d "indate" +'%Y-%b-%d %H:%M %Z'
...while the input format has to be specified for the FreeBSD
date
command:date -j -f '%b %d %H:%M:%S %Y %Z' "indate" +'%Y-%b-%d %H:%M %Z'[/CMD]
So, sprinkled throughout the code (the pull request is here if you'd like to take a look at it), I've got case statements that chose the appropriate command format based on $OSTYPE e.g.
Code:
case ${OSTYPE,,} in
freebsd*)
command using FreeBSD syntax
;;
*)
command using Linux syntax
;;
esac
The modified script works well (albeit slow). I then thought the command format is very similar for both platforms except for some switches. Could I pass those switches through variables? If I could, all the case constructs would disappear except for a single case construct early on in the code for setting up the switch variables, so, something like:
Code:
case ${OSTYPE,,} in
freebsd*)
sflag = "-e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'"
dflag = "-j -f '%b %d %H:%M:%S %Y %Z'"
;;
*)
sflag = "':a;N;$!ba;s/\n/ /g'"
dflag = "-d"
;;
esac
Within the body of the code, the
sed
and date
commands normalised for both Linux and FreeBSD platforms would look something like:
Code:
sed ${sflag} file
date ${dflag} "indate" +'%Y-%b-%d %H:%M %Z'
I've been researching and struggling for a couple of days to try to get this working and have not made any real headway. The problem arises because of the special characters in the flags being interpreted rather than being treated literally. I'm hoping the brains trust here can help me move forward as I'm stuck.