快速操作项目

打开

  1. shopt -s dotglob #Use shopt -u dotglob to exclude hidden directories
  2. while IFS= read -r d; do
  3. cur_dir="${d##*/}"
  4. #echo "${cur_dir}"="code $d"
  5. alias "${cur_dir}"="code $d"
  6. done <<<$(find ~/development/* -prune -type d)
  7. # 或
  8. shopt -s dotglob # 文件开头是"."也能匹配
  9. for d in ~/development/*/; do # trailing slash - only directories match
  10. cur_dir="${d%/}" # strip trailing slash
  11. cur_dir="${cur_dir##*/}"
  12. alias "${cur_dir}"="code $d"
  13. done

移动到可以将 code 改为 cd

服务器快速拉取项目

实现思路:

  1. 使用git alias git push && 实现push 成功后执行特定的动作
  2. 动作是ssh 到服务器上
  3. 到指定的目录执行 git pull —rebase

实现:

  1. ###### 定义 git alias
  2. > git config alias.sp !git pull --rebase && echo =====push====== && git push && bash -ic "'sp "$0" lxgkw'"
  3. # 先 pull 代码,然后 push, 然后执行特定的别名 sp
  4. # 可以 git config -e 进行配置编辑
  5. ###### 定义 bash alisas
  6. alias sp
  7. sp='bash /Users/lxg/www/scripts/server_pull.sh'
  8. alias test7
  9. test7='ssh -p 2222 bae@192.168.88.52'
  10. alias pub7
  11. pub7='ssh -p 2222 bae@192.168.88.71'
  12. ###### 写本地脚本
  13. cat /Users/lxg/www/scripts/server_pull.sh
  14. #!/bin/bash
  15. #启用别名调用
  16. shopt -s expand_aliases
  17. source /Users/lxg/.bashrc
  18. #服务器列表
  19. #list={test7,pub7}
  20. list=(${1//,/ })
  21. for ser in ${list[@]}
  22. do
  23. if [ ${ser:0:1} == 'p' ];then
  24. ser='pub7'
  25. else
  26. ser='test7'
  27. fi
  28. echo ====server pull: ${ser}===========
  29. sshCmd='lxgp '$2
  30. if [ ${2:0:3} == 'lxg' ];then
  31. sshCmd=$2
  32. fi
  33. #链接远程服务器并执行命令
  34. sshCmd='shopt -s expand_aliases;source /home/bae/.bashrc;eval '$sshCmd
  35. sshCall=${ser}' "'$sshCmd'"'
  36. eval $sshCall
  37. done
  38. # ./server.sh t,b lxgkw test7 pub7 都更新指定的项目
  39. # ./server.sh t,b kapi-web test7 更新指定目录下的项目
  40. ##### 服务器上的别名
  41. [bae@syt-dev7-d:~]$ alias lxgp
  42. alias lxgp='/home/bae/scripts/lxg_git_pull.sh'
  43. [bae@syt-dev7-d:~]$ alias lxgkw
  44. alias lxgkw='/home/bae/scripts/lxg_git_pull.sh kapi-web'
  45. # 目录映射项目名称
  46. alias lxgpp='path=$(pwd $1);lxgp ${path##*/}'
  47. #项目目录映别名
  48. shopt -s dotglob
  49. for d in `ls -d ~/wwwroot/*`; do # trailing slash - only directories match
  50. cur_dir="${d%/}" # strip trailing slash
  51. cur_dir="${cur_dir##*/}"
  52. alias "${cur_dir}"="cd $d"
  53. done
  54. ##### 服务器上的脚本
  55. [bae@syt-dev7-d:~]$ cat scripts/lxg_git_pull.sh
  56. #!/usr/bin/expect
  57. set dir [lindex $argv 0]
  58. set name lixiaoguang
  59. set pwd WKCYmB1PIVZaUis
  60. set workDir /home/bae/wwwroot/$dir
  61. cd $workDir
  62. spawn git pull --rebase
  63. expect "*Username*"
  64. send "$name\n"
  65. expect "*Password*"
  66. send "$pwd\n"
  67. expect eof
  68. #interact
  69. # 到指定的目录下执行 git pull,自动填入账号密码

优化后的服务器拉取代码版本

  1. #!/usr/bin/bash
  2. pull() {
  3. dir=$1
  4. workDir="/home/bae/wwwroot/$dir"
  5. expect <<-EOF
  6. set name lixiaoguang
  7. set pwd WKCYmB1PIVZaUis
  8. cd $workDir
  9. spawn git pull --rebase
  10. expect "*Username*"
  11. send "$name\n"
  12. expect "*Password*"
  13. send "$pwd\n"
  14. expect eof
  15. EOF
  16. }
  17. ##加入修改判断
  18. hasChange=`git status --ignore-submodules=dirty | grep modified | wc -l`
  19. if [ $hasChange -gt 0 ]; then
  20. echo '========== stash'
  21. git stash
  22. echo '========== pull'
  23. pull $1
  24. echo '========== stash pop'
  25. git stash pop
  26. else
  27. pull $1
  28. fi
  29. exit 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 退出,表示正常。