快速操作项目
打开
shopt -s dotglob #Use shopt -u dotglob to exclude hidden directorieswhile IFS= read -r d; docur_dir="${d##*/}"#echo "${cur_dir}"="code $d"alias "${cur_dir}"="code $d"done <<<$(find ~/development/* -prune -type d)# 或shopt -s dotglob # 文件开头是"."也能匹配for d in ~/development/*/; do # trailing slash - only directories matchcur_dir="${d%/}" # strip trailing slashcur_dir="${cur_dir##*/}"alias "${cur_dir}"="code $d"done
移动到可以将 code 改为 cd
服务器快速拉取项目
实现思路:
- 使用git alias git push &&
实现push 成功后执行特定的动作 - 动作是ssh 到服务器上
- 到指定的目录执行 git pull —rebase
实现:
###### 定义 git alias> git config alias.sp !git pull --rebase && echo =====push====== && git push && bash -ic "'sp "$0" lxgkw'"# 先 pull 代码,然后 push, 然后执行特定的别名 sp# 可以 git config -e 进行配置编辑###### 定义 bash alisas❯ alias spsp='bash /Users/lxg/www/scripts/server_pull.sh'❯ alias test7test7='ssh -p 2222 bae@192.168.88.52'❯ alias pub7pub7='ssh -p 2222 bae@192.168.88.71'###### 写本地脚本❯ cat /Users/lxg/www/scripts/server_pull.sh#!/bin/bash#启用别名调用shopt -s expand_aliasessource /Users/lxg/.bashrc#服务器列表#list={test7,pub7}list=(${1//,/ })for ser in ${list[@]}doif [ ${ser:0:1} == 'p' ];thenser='pub7'elseser='test7'fiecho ====server pull: ${ser}===========sshCmd='lxgp '$2if [ ${2:0:3} == 'lxg' ];thensshCmd=$2fi#链接远程服务器并执行命令sshCmd='shopt -s expand_aliases;source /home/bae/.bashrc;eval '$sshCmdsshCall=${ser}' "'$sshCmd'"'eval $sshCalldone# ./server.sh t,b lxgkw test7 pub7 都更新指定的项目# ./server.sh t,b kapi-web test7 更新指定目录下的项目##### 服务器上的别名[bae@syt-dev7-d:~]$ alias lxgpalias lxgp='/home/bae/scripts/lxg_git_pull.sh'[bae@syt-dev7-d:~]$ alias lxgkwalias lxgkw='/home/bae/scripts/lxg_git_pull.sh kapi-web'# 目录映射项目名称alias lxgpp='path=$(pwd $1);lxgp ${path##*/}'#项目目录映别名shopt -s dotglobfor d in `ls -d ~/wwwroot/*`; do # trailing slash - only directories matchcur_dir="${d%/}" # strip trailing slashcur_dir="${cur_dir##*/}"alias "${cur_dir}"="cd $d"done##### 服务器上的脚本[bae@syt-dev7-d:~]$ cat scripts/lxg_git_pull.sh#!/usr/bin/expectset dir [lindex $argv 0]set name lixiaoguangset pwd WKCYmB1PIVZaUisset workDir /home/bae/wwwroot/$dircd $workDirspawn git pull --rebaseexpect "*Username*"send "$name\n"expect "*Password*"send "$pwd\n"expect eof#interact# 到指定的目录下执行 git pull,自动填入账号密码
优化后的服务器拉取代码版本
#!/usr/bin/bashpull() {dir=$1workDir="/home/bae/wwwroot/$dir"expect <<-EOFset name lixiaoguangset pwd WKCYmB1PIVZaUiscd $workDirspawn git pull --rebaseexpect "*Username*"send "$name\n"expect "*Password*"send "$pwd\n"expect eofEOF}##加入修改判断hasChange=`git status --ignore-submodules=dirty | grep modified | wc -l`if [ $hasChange -gt 0 ]; thenecho '========== stash'git stashecho '========== pull'pull $1echo '========== stash pop'git stash popelsepull $1fiexit 0
检查php语法
- 只检查 已修复的PHP文件的语法是否正确
— 整个语句
#!/bin/bash
# check php syntax
for i in `git status -s -uall | grep "^[^D].*.php$" | awk '{print $NF}'`;
do
if [ ! -f $i ];then
continue
fi
t=$(php -l "$i" 2>/dev/null)
code=$?
if [ $code != 0 ]; then
echo "$t" 1>&2
exit 255
fi
done
exit 0
语句解析
已短格式输出git中修改的文件列表
git status -s
❯ git status
On branch rc-PR-606-lxg-visionOptimize
Your branch is up to date with 'origin/rc-PR-606-lxg-visionOptimize'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: public/index.php
no changes added to commit (use "git add" and/or "git commit -a")
❯ git status -s
M public/index.php
非 D 开头的php结尾的行(非删除文件的php文件列表)
grep “^[^D].*.php$”
只要最后一行数据 (文件列表)
awk ‘{print $NF}’
如果文件不存在就跳过
if [ ! -f $i ];then
continue
fi
使用 php -l 检查指定文件的语法,输出值赋值到 t
t=$(php -l “$i” 2>/dev/null)
如果上个语句为非 0 退出,代表语法错误,输出具体的错误,整体脚本使用 exit 255 退出,表示异常。
如果没有错误 exit 0 退出,表示正常。
