当前位置: 移动技术网 > 网络运营>服务器>Linux > Shell脚本中实现更新PHP5

Shell脚本中实现更新PHP5

2017年12月12日  | 移动技术网网络运营  | 我要评论

我很迷恋 shell,很喜欢看着字符在黑色的 console 界面中跳跃着,奇怪的是,我居然没有因此成为一个 linux/unix 程序员,而只是个写 cgi 程序的倒霉蛋。大家都是怎么称呼 “php 程序员”的?对了——草根~ 嗯,在土里埋的太久,说不定哪天就烂掉了咯! 

可能是被 windows 惯坏了,实在不想换个 os,还好有 cygwin,mingw … 之类的东西, 适当的时候,可以拿出来装下 b,自我安慰一下~

我总喜欢从 windows.php.net 下载最新的 snapshot,不是我想体验最新的功能,只是强迫症的关系-,-。我机器上的所有软件,程序都是最新的,绝大部分都还挂着 beta 标签,甚至有一些是直接从 svn,git 上面拖下来的 trunk 版本,想想真是变态。如果你每天都爬上这些网站,人肉检查一下是不是有新的版本发布,以此满足一下自己变态的心理,那真是要疯掉了。

能让机器干的事情,就不要手工去做,是吧!下面这段代码会自动去 check 最新的 snapshot,解压到你想要的目录。 然后呢?建个 cron job 挂上去,就可以去找新的乐子了~

代码中下载的是,none thead safe vc9 版本,注意替换成自己能用的版本。需要强制更新的话,加上 “–force” 参数。

最后一行使用 icacls 重置了 php5-nts 目录下文件的权限(注意路径的写法,icacls 是 windows 自己的程序),因为 cygwin 会把 ntfs 的权限搞的巨恶心。
ps: 非 cgi/fcgi 安装模式,记得关掉 web server。

#!/bin/bash
 
install_path="/cygdrive/d/php5-nts"
build_time_file="/cygdrive/d/php5-nts/build-time"
package_url="http://windows.php.net/downloads/snaps/php-5.3-nts-win32-vc9-x86-latest.zip"
 
function uprint {
  if [ "${1:0:1}" = "-" ]; then
    echo $1 "# $2"
  else
    echo "# $1"
  fi
}
 
## if unzip available?
unzip=`which unzip 2> /dev/null`
if [ -z $unzip ]; then
  uprint "could not find unzip, please install."
  exit 1
fi
 
## test if build-time file exists, if not, create it
if [ ! -f $build_time_file ]; then
  uprint -n "build time file does not exists, created ... "
  touch $build_time_file
  echo -e "\e[32m[ok]\e[0m"
fi
 
## get current build time
current_build_time=`cat $build_time_file`
 
## get latest build time
latest_build_time=`curl --silent http://windows.php.net/snapshots/ | \
    grep "php-5.3-nts-vc9-x86" | \
    grep "vc9 x86 non thread safe (" | \
    grep -o "(.*)" | \
    sed 's/[()]//g'`
 
## any update?
package=`basename $package_url`
if [ "$current_build_time" != "$latest_build_time" ]; then
  uprint -e "new version available, build time: \e[36m$latest_build_time\e[0m"
else 
  if [ "$1" != "--force" ]; then
    uprint "you are using the latest snapshot version."
    exit 0
  else 
    uprint -e "\e[31mforce to update local php version.\e[0m"
  fi
fi
 
## delete if file already exists
ls $package > /dev/null 2>&1
if test $? -eq 0; then
  uprint -n "performing: rm -f \`ls $package\` ... "
  rm -f `ls $package`
  echo -e "\e[32m[ok]\e[0m"
fi
 
## get latest php5 binary package
uprint -n "downloading latest php binary package ... "
wget -q $package_url
echo -e "\e[32m[ok]\e[0m"
 
## extracting
if [ -f $package ]; then
  # kill php processes
  for php_pid in `ps -as | grep php | awk '{print $1}'`
  do 
    kill -9 $php_pid
  done
 
  uprint -n "extracting ... "
  unzip -o $package -x -d $install_path > /dev/null 2>&1
  echo -e "\e[32m[ok]\e[0m"
  echo $latest_build_time > $build_time_file
 
  uprint -n "cleaning up ... "
  rm -f $package
  echo -e "\e[32m[ok]\e[0m"
fi
 
## fixed cygwin permissions
icacls d:/php5-nts /reset /t > /dev/null
 
# vim: set expandtab tabstop=4 shiftwidth=4:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网