Last active
January 11, 2018 08:26
-
-
Save sursir/9e44b8badbbc50d63790551969cf6046 to your computer and use it in GitHub Desktop.
System Shell bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### | |
### 判断参数内容 重置参数 (docker-php-entrypoint) | |
###################### | |
#!/bin/sh | |
set -e # 下面有命令失败(exit 0)直接退出 | |
# first arg is `-f` or `--some-option` | |
if [ "${1#-}" != "$1" ]; then # 判断会否为 -f or --some-option 选项 为选项则代表 重置参数为php调用 | |
set -- php "$@" # 重置参数(set --) | |
fi | |
exec "$@" # 最后执行最终命令 | |
# . 点号的用法 点号就是 source | |
# . == source | |
. ~/.bashrc | |
source ~/.bashrc | |
# 比较两个目录下的所有文件是否完全相同 | |
cd dir1 | |
find ./ -type f -exec md5sum {} \; | sort -k 2 > logdir/dir1.txt | |
cd dir2 | |
find ./ -type f -exec md5sum {} \; | sort -k 2 > logdir/dir2.txt | |
cd logdir | |
diff dir1.txt dir2.txt | |
# 上条命令返回值 0或者非负 | |
echo $? | |
# 逻辑表达式的用法: | |
# [[]] 比 [] 更好,更现代化 | |
[[ 3 -gt 4 && 4 -gt 5 ]] ✅ (正确) | |
[ 3 -gt 4 && 4 -gt 5 ] ❌ (错误) | |
[ 3 -gt 4 ] && [ 4 -lt 5 ] ✅ (正确) | |
# 逻辑成功返回的时0,失败返回的时非0,与shell命令执行成功与否返回值相同 | |
# 假如返回非0(即逻辑失败)可以在后面用 || 来捕获 并且在执行完之后可以对此失败进行处理仍然还可以返回非0 如:` || (echo 'a error' && return 1)` | |
# 以下为例子: | |
``` | |
[[ ! -f composer.lock.prev || $(md5sum composer.lock.prev | awk '{print $1}') != $(md5sum composer.lock | awk '{print $1}') ]] && | |
composer install --no-dev && cp -f composer.lock composer.lock.prev && echo 'installed depend' || | |
echo 'no install depend, use cached' | |
``` | |
# 对以上代码的解释: | |
# 假如没有上一版本lock,或者与上一版本lock与当前lock md5不同,重新安装依赖 并 更新composer.lock.prev为当前的composer.lock文件 | |
# 即使没有安装依赖也不会报错,因为用了 `|| echo`, 这样的话最终都不会非零退出 | |
# (重要):假如没有 composer.lock 会重新安装依赖,报错但不会退出。 | |
# (重要):但是!** 因为版本控制中没有加入lock文件,所以每次都将重新安装依赖 而且每次很慢(因为需要重新确定依赖版本并生成lock文件) ** | |
# 1. 假如介意每次都安装依赖 请添加 composer.lock 到版本库 或者 修改看情况将 composer.json 作为依据(不推荐) | |
# 2. 或者没有 comoposer.lock 时异常情况,那么添加以下代码 没有 composer.lock 将报错非0退出 | |
``` | |
[[ -f composer.lock ]] || (echo 'Error! composer.lock dont exists ! ' && exit 1) | |
``` | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ | |
### 系统 | |
################ | |
# debian 设置时区 | |
sudo dpkg-reconfigure tzdata | |
# ubuntu 配置默认编辑器 | |
sudo update-alternatives --config editor | |
# 修改 sudo 行为 (可保持原环境变量) | |
sudo visudo | |
################# | |
### 代理 | |
################# | |
# 设置命令行代理(http) | |
###快捷方式: | |
命令: | |
``` | |
alias pr='env http_proxy=http://192.168.1.100:1087 https_proxy=http://192.168.1.100:1087' | |
``` | |
### 同时要保证 sudo 下可用 | |
方法:在 sudo 环境下 保持 某些环境变量不改变 即:http_proxy | |
命令: | |
``` | |
> sudo visudo | |
``` | |
添加: | |
``` | |
Defaults env_keep="http_proxy https_proxy ftp_proxy no_proxy DISPLAY XAUTHORITY" | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ls -1 | sort -rV | awk 'FNR > 20 {printf("rm -rf %s\n", $0);}' | bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
six six six