您可以简单地使用
sed -i.bkp 's/^extension=x.so/# extension=x.so/' /path/of/php.ini && /etc/init.d/httpd reload
-i.bkp将备份作为php.php.bkp并写入原始文件
&&如果第一个命令成功执行,则重新加载httpd服务。
但我只是注意到sed在搜索模式不匹配时给出退出状态0,因此您可以使用
php_ini=/path/of/php.ini
__module=x.so
grep -q "^extension=$__module" $php_ini && {
sed -i.bkp "s/^extension=$__module/# extension=$__module/" $php_ini &&
echo /etc/init.d/httpd reload; } || echo "cannot make requested change"
或者,您可以使用以下脚本启用和禁用:
#!/bin/bash
php_ini=/path/of/php.ini
__module="$2"
[[ ! -f $php_ini ]] && { echo "Error: Can not found $php_ini" >&2;
echo "Please define php.ini path in $php_ini";
exit 1; }
[[ -z $__module ]] && { echo "Error: Please Type Module Name:" >&2;
exit 1; }
show_help(){
cat <<_eof>
Usage: To enable :
$0 -ie
To disable :
$0 -id
example:
$0 -i xyz.so
_EOF
}
do_enable() {
grep -Eq "# extension=$__module$" $php_ini && {
sed -i.bkp "s/^# extension\=$__module$/extension=$__module/" $php_ini &&
echo /etc/init.d/httpd reload; echo "Changes Successfully Done"; } || echo "cannot make requested change"
}
do_disable() {
grep -q "^extension=$__module" $php_ini && {
sed -i.bkp "s/^extension=$__module/# extension=$__module/" $php_ini &&
echo /etc/init.d/httpd reload; echo "Changes Successfully Done"; } || echo "cannot make requested change"
}
Main() {
case $1 in
-ie) do_enable ;;
-id) do_disable ;;
*) show_help ;;
esac
}
Main $*