I have a shell script that I currently use for some build related stuff for a mobile application.
Due to the the subtle differences between BSD & GNU one of build scripts originally written on a Mac (BSD)
environment=$1
if [[ -z $environment ]]; then
environment="beta"
fi
if ! [[ $environment =~ (live|beta) ]]; then
echo "Invalid environment: $environment"
exit 1
fi
mobile_app_api_url="https://api"$environment".mysite.com"
cp app/index.html.mob MobileApp/www/index.html
sed -i'' "s#MOBILE_APP_API_URL#\"$mobile_app_api_url\"#g" MobileApp/www/index.html
The sed command has been written on BSD (Mac) but as builds may take place on both Mac or Ubuntu (GNU) I need to modify this to work with on both flavours, what is the best approach for this?
ed
, it can edit the files in-place and it's fully portable. – don_crissti Mar 03 '17 at 14:12perl -pi -e '...'
. Both BSD and GNU sed derived their-i
fromperl
's one.perl
will be available on most GNU and BSD systems and most other systems. – Stéphane Chazelas Mar 03 '17 at 15:09-i''
is actually-i
(-i
concatenated with the empty string) so would only work with GNUsed
. You'd need-i ''
for BSDs. – Stéphane Chazelas Mar 03 '17 at 15:10perl
thanbash
though. For instance here, many of the BSDs would not havebash
or any shell that supports those[[...]]
or=~
(which can easily be replaced by a standard and more legiblesh
case
construct). – Stéphane Chazelas Mar 03 '17 at 15:43