好多脚本中,都会有这一段,获取绝对路径,代码如下:
SOURCE="$0"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
grep 中一般会包含grep,需要排除的话,代码如下:
ps aux|grep nginx|grep -v grep||awk ‘{print $2}‘|xargs -i kill -9 {}
命令详解:grep
判断是否查找到,如果有匹配的字符串,返回值是0, 还会打印出匹配字符串的行。如果没有匹配, 会返回1。代码如下:
ls |grep nginx
if [ $? -eq 0 ];then
rm -rf nginx
fi
如果关心统计个数,可以用如下命令(和grep $?有点相似):
PACKAGE_CNT=`ls ${UPath} |grep -i "xxxx"|grep ".tar.gz"|wc -l`
[ $PACKAGE_CNT -gt 1 ] && echo "[ERROR] too many xxx.tar.gz packages in ${UPath}"&& return 1
[ $PACKAGE_CNT -eq 1 ] && return 0
原文:https://www.cnblogs.com/meiguhuaxian/p/12960867.html