bash工具-dir_util.sh

#!/usr/bin/env bash

#以pwd和当前脚本路径名$0 结合 给出 当前脚本所在目录名、当前脚本名
#调用者应该在切换目录之前调用本函数, 即 尽可能早的调用本脚本.  
#   若 调用者 切换到其他目录后,调用本脚本 则结果肯定不对.
# 使用例子: getCurScriptDirName $0
#返回: 当前脚本文件 绝对路径 CurScriptF, 当前脚本文件 名 CurScriptNm, 当前脚本文件 所在目录 绝对路径 CurScriptNm
function getCurScriptDirName(){

    #若函数参数少于1个,则退出(退出码为23)
    [ $# -lt 1 ] && return 23

    { { [[ $0 == /* ]] && CurScriptF=$0 ;} ||  CurScriptF=$(pwd)/$0 ;} && \

#当前脚本文件名, 此处 CurScriptF=build-linux-2.6.27.15-on-i386_ubuntu14.04.6LTS.sh
#CurScriptF为当前脚本的绝对路径
#若$0以/开头 (即 绝对路径) 返回$0, 否则 $0为 相对路径 返回  pwd/$0

    [ -f $CurScriptF ] && \
    CurScriptNm=$(basename $CurScriptF) && \
    CurScriptDir=$(dirname $CurScriptF)



}


#getCurScriptDirByConcat $(pwd) $0
function getCurScriptDirByConcat(){
    #若函数参数少于2个,则退出(退出码为34)
    [ $# -lt 2 ] && return 34
    
    #echo dir=$1, fn=$2

    _dir=$1
    _fn=$2
    { { [[ $_fn == /* ]]  && ScriptPathAbs="${_fn}"  ;} || ScriptPathAbs="${_dir}/${_fn}" ;} && \

    { {   [[ -f $ScriptPathAbs ]] ;} || return 35  ;} && \

    CurScriptNm=$(basename $ScriptPathAbs) && \
    CurScriptDir=$(dirname $ScriptPathAbs)
}

#创建目录、设置该目录主人为 当前主组/当前用户 、进入该目录
#使用举例: 
#createDir_CurUsrOwn_EnterIt /app 
function createDir_CurUsrOwn_EnterIt(){
    
    
    #若函数参数少于1个,则退出(退出码为21)
    [ $# -lt 1 ] && return 21

    dire=$1
    mainGroup=$(id -gn) && \
    username=$(whoami) && \
    sudo mkdir $dire && \
    sudo chown -R $mainGroup.$username $dire && \
    cd $dire
}

#全路径文件 重命名: 加 当前绝对时间后缀
#用法举例: mvFile_AppendCurAbsTime /bal/xxx.txt
#则 文件/bal/xxx.txt 被重命名为 比如 /bal/xxx.txt-20231210132251_1702185771_452355256
function mvFile_AppendCurAbsTime(){
        
    #若函数参数少于1个,则退出(退出码为21)
    [ $# -lt 1 ] && return 22

    fileName=$1 && \
    # 比如 fileName==/bal/xxx.txt && \
    UniqueId="$fileName-$(date +'%Y%m%d%H%M%S_%s_%N')" && \
    { { [ -f $fileName ] && mv -v $fileName "$UniqueId" ;} || : ;}
    #fix bug
}

function mvFile_AppendCurAbsTime_multi(){
        
    #若函数参数少于1个,则退出(退出码为21)
    [ $# -lt 1 ] && return 22


    UniqueDateTime="$(date +'%Y%m%d%H%M%S_%s_%N')" && \

    for fileNameK in "$@"; do
        UniqueId="${fileNameK}-${UniqueDateTime}" && \
        { { [ -f $fileNameK ] && mv -v $fileNameK "$UniqueId" ;} || : ;}
    done

}

#gitCko_tagBrc_asstCmtId: gitCheckout_tagOrBranch_assertCmtId
#调用举例 : gitCko_tagBrc_asstCmtId GitDir tagOrBranch CmtId
function gitCko_tagBrc_asstCmtId() {
#若函数参数少于3个,则退出(退出码为14)
[ $# -lt 3 ] && return 14
GitWorkTreeDir=$1  &&  tagOrBranch=$2  CmtId=$3 && \

GitWorkTreeDirBaseName=$(basename ${GitWorkTreeDir} ) && \

ErrMsg1="错误, $Ver 的commitId不是 $CmtId, 退出码13" && \
OkMsg="git仓库验证通过:git仓库目录${GitWorkTreeDirBaseName}的分支或标签${tagOrBranch}确实位于提交id${CmtId}" && \

#子模块 用 --git-dir=$GitDir  比较麻烦 , 因此 不用
_pwd=$(pwd) &&
cd $GitWorkTreeDir && \
git checkout $tagOrBranch && \
CurHeadCmtId=$(git  rev-parse HEAD) && \
{ { [ "X$CurHeadCmtId" == "X$CmtId" ] && echo $OkMsg ;} || { echo $ErrMsg1 && exit 13 ;} ;} && \

cd $_pwd
_=end

}

#获取调用者 是否开启了 bash -x  即 是否开启 bash 调试
#返回变量 _out_en_dbg, _out_dbg
function get_out_en_dbg(){
    { { [[ $- == *x* ]] && _out_en_dbg=true && _out_dbg="-x" ;} || { _out_en_dbg=false && _out_dbg="" ;}  ;}
    # echo $_out_en_dbg
}

function miniconda3install(){
(echo "安装miniconda3..." && \
cd /tmp/ && \
cat << 'EOF' > Miniconda3-py310_23.10.0-1-Linux-x86_64.sh.md5sum.txt
cefadd1cacd8e5b9a74b404df1f11016  Miniconda3-py310_23.10.0-1-Linux-x86_64.sh
EOF

{ md5sum --check Miniconda3-py310_23.10.0-1-Linux-x86_64.sh.md5sum.txt ||
 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py310_23.10.0-1-Linux-x86_64.sh ;} && \

hm=/app/miniconda3 && \
sudo mkdir -p $hm && \
sudo chown -R $(id -gn).$(whoami) $hm && \
bash Miniconda3-py310_23.10.0-1-Linux-x86_64.sh -b -u -p $hm
)
}


function tsinghua_pypi_src(){
hm=/app/miniconda3 && \
source $hm/bin/activate && \
#pip源设为清华源: https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
# python -m pip install --upgrade pip && \
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
conda deactivate
}

function miniconda3Activate(){
get_out_en_dbg && \
# echo "$_out_en_dbg,【$_out_dbg】" && \

#miniconda activate 不要开调试
CondaActvF=/app/miniconda3/bin/activate && \
{ [ -f  $CondaActvF ]  || miniconda3install   ;} && \
set +x && source $CondaActvF

#恢复可能的调试
{ { $_out_en_dbg && set -x && : ;} || : ;}

}

function cmakeInstall(){
    
{ cmake --version 1>/dev/null 2>/dev/null || sudo apt install cmake    -y ;} && \
{ g++ --version   1>/dev/null 2>/dev/null || sudo apt install build-essential -y ;}

}

function makLnk(){
# makLnk "/bal/py_util" "/bal/clang-add-funcIdAsm/SrcFileFuncIdGenService/py_util"

#若函数参数少于2个,则退出(退出码为14)
[ $# -lt 2 ] && return 14
lnkSrc=$1  &&  lnkDest=$2 && \
#lnkDest="/bal/clang-add-funcIdAsm/SrcFileFuncIdGenService/py_util" && \
#lnkSrc="/bal/py_util" && \
{ [ -e $lnkDest ] || \
ln -s $lnkSrc $lnkDest ;}

}


#给定文件的最后修改时刻是否在当前时刻的N秒内
function fileModifiedInNSeconds(){
# fileModifiedInNSeconds "/bal/clang-add-funcIdAsm/build/lib/libCTk.so" "5*60"

#若函数参数少于2个,则退出(退出码为14)
[ $# -lt 2 ] && return 14
filePath=$1  &&  limitSecondsExpr=$2 && \
{ [ -e $filePath ] || return 31 ;} && \
limitSeconds=$(($limitSecondsExpr)) && \
nowSeconds=$(date +%s) && \
fileEndModifySeconds=$(stat -c %Y  $filePath ) && \
deltaSeconds=$(( $nowSeconds -  $fileEndModifySeconds  )) && \
{ [  $deltaSeconds  -le $limitSeconds ] || return 1 ;} && \
return 0

}

-

相关推荐

  1. bash工具-dir_util.sh

    2024-04-01 17:42:02       20 阅读
  2. Bash快捷键

    2024-04-01 17:42:02       34 阅读
  3. bash入门

    2024-04-01 17:42:02       30 阅读
  4. Bash相关

    2024-04-01 17:42:02       12 阅读
  5. Chrome base 库详解:工具类和常用类库

    2024-04-01 17:42:02       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 17:42:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 17:42:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 17:42:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 17:42:02       18 阅读

热门阅读

  1. python 三层架构思想写代码。

    2024-04-01 17:42:02       17 阅读
  2. python 移位运算符

    2024-04-01 17:42:02       19 阅读
  3. TTL值(Time-To-Live)简介

    2024-04-01 17:42:02       17 阅读
  4. NoSQL(非关系型数据库)之Redis

    2024-04-01 17:42:02       27 阅读
  5. 编程练习(python)

    2024-04-01 17:42:02       15 阅读
  6. 大模型之路1:趟一条小路

    2024-04-01 17:42:02       18 阅读
  7. 关于python中常用命令(持续更新中)

    2024-04-01 17:42:02       21 阅读
  8. 2024.2.9力扣每日一题——二叉树的最近公共祖先

    2024-04-01 17:42:02       16 阅读
  9. SpringAOP和AspectJ有什么关系 ?

    2024-04-01 17:42:02       18 阅读
  10. ActiViz中的数据存储vtkDataArray

    2024-04-01 17:42:02       20 阅读