1. 未写待更新

1. 未写待更新

1.1. 判断文件后缀函数( 比如 .sh , .png等等)

[[ =~ \.sh$ ]]

1.2. 判断文件类型和后缀,并进行下一步操作

1.判断文件类型的时候要先判断文件是否是一个软链接,然后再继续判断文件的具体类型。 2.可以先判断文件是否存在 3.判断文件类型和后缀之后对其进行操作,比如sh后缀的文件加上执行权限等等

1.3. 脚本环境定义函数

重新定义一下脚本的各种环境变量和别名等等,避免脚本出问题,尤其各种别名,比如rm的别名等等 不过一般不用定义也行,因为都会从shell中继承环境变量的(非登录非交互脚本用BASH_ENV变量空值其环境); 新开了一个子shell,除了父shell也就是执行这个脚本的shell中定义的环境变量(export)会继承外,其他的变量等等都是按照文件中保存的默认设置进行继承的。包括各种别名,等等。

1.4. 256色调色函数

前提: 1. 查看当前终端类型: echo $TERM xterm-color 2. 查看当前服务器终端色彩: tput colors 8 3.配置Linux终端如果支持就调整为256色终端,添加到.bashrc文件内。 if [ -e /usr/share/terminfo/x/xterm-256color ]; then #debian在/lib/terminfo/x/xterm-256color export TERM='xterm-256color' else export TERM='xterm-color' fi 4.如不支持xterm-256color,则需要安装: apt-get install ncurses-base yum install ncurses --------------------------------------------- 背景色测试 for i in {0..255}; do printf '\e[48;5;%dm%3d ' $i $i; (((i+3) % 18)) || printf '\e[0m\n'; done 字体色测试 for i in {0..255}; do printf '\e[38;5;%dm%3d ' $i $i; (((i+3) % 18)) || printf '\e[0m\n'; done 可以在前面添加其他控制符号,但是不能在后面和中间添加

2. 个人写好的常用function:

  1. 函数文件名和位置:/etc/profile.d/functions.sh
  2. 判断是否有参数输入
  3. 判断输入的参数是否是数字(除了0以外的正整数)
  4. 判断输入的参数是否是全部数字
  5. 判断输入的参数文字是什么来返回特定的值,比如start stop remove ,然后根据返回值进行脚本下一步的操作。
  6. 打印各种颜色(包括随机)和效果以及闪烁等等:
  7. 判断字符串长度
  8. 判断文件是否存在,存在告诉类型返回0,不存在返回1(这个不写了吧)
  9. 判断输入的文字是什么来返回特定的值,从而根据返回值来决定下一步操作(比如和select相结合)
  10. 判断输入的yes(Y e S; Y ;y) no (nO ;N)
  11. 返回一些列参数中的最大值,最小值,(冒泡排序脚本后面直接跟上一个数组进行排序后再输出)
  12. 判断系统版本
  13. 拷贝工具以及它依赖的库文件
  14. 判断函数后面的文件所在目录
  15. 将后面的函数文件中的函数定义位全局函数(主要用于awk)或取消定义。
# 1.判断是否有输入,判断输入是否只有1个参数,判断输入是否符合要求的参数个数 # 注意写一个负数的函数也 func_ifinput_exist(){ [ "$1" ] && return 0 || return 1 } func_ifinput_isone(){ [ "$#" -eq 1 ] && return 0 || return 1 } func_ifinput_isnumofarg(){ if func_is_onedigit $1;then (( $#-1 == $1 )) &> /dev/null && return 0 || return 1 else echo -e "Usage: func ARGNUM Arg1 Arg2 ... [if use in script,argument can be \$@]\nSpecial:\`func 0[00..]' return true" return 1 fi } # 判断是否一个还可写成简单点的 # func_ifinput_isone(){ # func_ifinput_exist "$@" && [ -z "$2" ] && return 0 || return 1 # # } # 更简单的: # func_ifinput_isone(){ # [ "$#" -eq 1 ] && return 0 || return 1 # # } # 2:判断数字:参数是否一个数字,参数是否一个正整数,是否所有的参数都是数字,是否所有的参数都是正整数 func_is_onedigit(){ [[ "$1" =~ ^[0-9]+$ && -z "$2" ]] && return 0 || return 1 } func_is_alldigit(){ if [ "$1" ]; then while [ "$1" ];do [[ $1 =~ ^[0-9]+$ ]] && shift || return 1 done return 0 else return 1 fi } func_is_numofdigit(){ func_is_onepositint $1 || { echo -e "Usage:func Positive_NUM digit1 digit2 ... [if use in script,argument(digit?) can be \$@]"; return 1; } func_is_alldigit $@ || return 1 [ "$1" -eq $[$#-1] ] && return 0 || return 1 } func_is_onepositint(){ [[ -z "$2" && "$1" =~ ^[0-9]+$ && ! "$1" =~ ^0+$ ]] && return 0 || return 1 } func_is_allpositint(){ if [ "$1" ]; then while [ "$1" ];do [[ $1 =~ ^[0-9]+$ && ! $1 =~ ^0+$ ]] && shift || return 1 done return 0 else return 1 fi } func_is_numofpositint(){ func_is_onepositint $1 || { echo -e "Usage:func Positive_NUM positint1 positint2 ... [if use in script,argument(positint?) can be \$@]"; return 1; } func_is_allpositint $@ || return 1 [ "$1" -eq $[$#-1] ] && return 0 || return 1 } # 3.递归函数简单版,需要注意的就是由于bash中双小括号只能支持最大63位的正整数(最高位64位是符号位),所以超过20的阶乘就无法直接计算了,所以必须用bc命令。 func_factorial(){ local input=$1 if func_is_onepositint $input ;then if (( $input <= 20 )); then [ $input -gt 1 ] && echo $[`func_factorial $[input-1]`*input] || echo 1 else echo `seq -s'*' $input` | \bc return 0; fi else echo -e "Please input a positive integer" return 1 fi } # 3.递归函数完整版,注意系统一定要有bc命令 func_factorial(){ local input=$1 if func_is_onepositint $input ;then [ $input -gt 1 ] && echo "$input * `func_factorial $((input-1))`" | \bc || echo 1 else echo -e "Please input a positive integer" return 1 fi } # 4. 打印各种颜色:(打印的时候用echo或许更方便,我这里用的是printf,让颜色控制的前面全部为0;它就相当于后面的数值把前面的0覆盖了,这样的话一个格式就能写3种不同情况了。用echo的话估计就是直接空变量s3等等,也可以,但是printf中的%s后面对应的变量不能为空的,因此我先判断了。) # func_color_begin(){ Nouse_fcst(){ if [ $* = "-l" -o $* = "--list" ];then local colorlist=(black red green yellow blue purple ching white ) for bg in `seq 8`;do local gnum=$[bg+39] printf "\nBackGround:bg%s(bg%d)\n\n" ${colorlist[$[bg-1]]} $bg for i in ${colorlist[*]};do printf " %-10s" $i done echo for i in 0 1 4 5 ; do for j in `seq 8`;do local cnum=$[j+29] local coname=${colorlist[$[j-1]]}$i local printf " \e[%d;%d;%dm%-10s\e[0m" $gnum $i $cnum $coname done echo done for i in 14 15 45 ; do for j in `seq 8`;do local cnum=$[j+29] local coname=${colorlist[$[j-1]]}$i printf " \e[%d;%d;%d;%dm%-10s\e[0m" $gnum ${i:0:1} ${i:1:1} $cnum $coname done echo done for i in 145 ; do for j in `seq 8`;do local cnum=$[j+29] local coname=${colorlist[$[j-1]]}$i printf " \e[%d;%d;%d;%d;%dm%-10s\e[0m" $gnum ${i:0:1} ${i:1:1} ${i:2:1} $cnum $coname done echo done done fi } fcst_isbg(){ local bg for bg in `seq 8`;do local bgname="bg${colorlist[$[bg-1]]}" [ "$*" = "bg${bg}" -o "$*" = "$bgname" ] && { local gnum=$[bg+39]; echo -n "\e[${gnum}m"; return 0 ; } done; return 1; } fcst_isco(){ local code=`echo $* | grep -Eo "[0-9]+"` local s1=0 ; local s2=0 ; local s3=0 ; [ `func_lenstr $code` -eq 1 ] &> /dev/null && s3=$code [ `func_lenstr $code` -eq 2 ] &> /dev/null && s2=${code:0:1} s3=${code:1:1} [ `func_lenstr $code` -eq 3 ] &> /dev/null && s1=${code:0:1} s2=${code:1:1} s3=${code:2:1} local i ; local j ; for i in `seq 8`;do local coname=${colorlist[$[i-1]]} for j in 0 1 4 5 14 15 45 145; do [ "$*" = "${coname}${j}" ] && { local cnum=$[i+29]; echo -n "\e[$s1;$s2;$s3;${cnum}m"; return 0 ; } done done return 1; } fcs(){ local colorlist=(black red green yellow blue purple ching white ) local bg ; local i ;local j; if [ "$*" = "-l" -o "$*" = "--list" &> /dev/null ];then for bg in {8..1};do local gnum=$[bg+39] printf "\nBackGround:%s(bg%d)\n\n" ${colorlist[$[bg-1]]} $bg printf " code" for i in ${colorlist[*]};do printf " %-10s" $i done echo for i in 0 4 5 45 1 14 15 145 ; do printf " %3s:" $i local s1=0 ; local s2=0 ; local s3=0 ; [ `func_lenstr $i` -eq 1 ] && s3=$i [ `func_lenstr $i` -eq 2 ] && s2=${i:0:1} s3=${i:1:1} [ `func_lenstr $i` -eq 3 ] && s1=${i:0:1} s2=${i:1:1} s3=${i:2:1} for j in `seq 8`;do local cnum=$[j+29] local coname=${colorlist[$[j-1]]}$i printf " \e[%s;%s;%s;%d;%dm%-10s\e[0m" $s1 $s2 $s3 $gnum $cnum $coname done echo done done return 0 elif [ $* = "-h" -o $* = "--help" &> /dev/null ]; then echo -e "Usage:echo -e \"\`fcs [ bgCODE | bgCOLOR ] | [ colorCODE ]\`STRING_TO_CHANGECOLOR\`fce\`\"\nfcs -l|--list : list all the bg and color." return 0 elif func_ifinput_isone $* ;then fcst_isco $* && return 0; fcst_isbg $* && return 0; # for bg in `seq 8`;do # [ "$*" = "bg${bg}" ] && { local gnum=$[bg+39]; echo -n "\e[${gnum}m"; return 0 ; } # done; # local code=`echo $* | grep -Eo "[0-9]+"` # local s1=0 ; local s2=0 ; local s3=0 ; # [ `func_lenstr $code` -eq 1 ] && s3=$code # [ `func_lenstr $code` -eq 2 ] && s2=${code:0:1} s3=${code:1:1} # [ `func_lenstr $code` -eq 3 ] && s1=${code:0:1} s2=${code:1:1} s3=${code:2:1} # for i in `seq 8`;do # local coname=${colorlist[$[i-1]]} # for j in 0 1 4 5 14 15 45 145 ; do # [ "$*" = "${coname}${j}" ] && { local cnum=$[i+29]; echo -n "\e[$s1;$s2;$s3;${cnum}m"; return 0 ; } # done # done elif func_ifinput_isnumofarg 2 $@;then # { fcst_isco $1 && fcst_isbg $2 && return 0; } || { fcst_isco $2 && fcst_isbg $1 && return 0; } || return 1 if `fcst_isco $1 > /dev/null && fcst_isbg $2 > /dev/null` ; then fcst_isco $1 fcst_isbg $2 return 0 elif `fcst_isco $2 > /dev/null && fcst_isbg $1 > /dev/null` ;then fcst_isco $2 fcst_isbg $1 return 0 else return 1 fi else return 1 fi } fce(){ echo -n "\e[0m" } # 5. 判断字符串长度: func_lenstr(){ if func_ifinput_isone "$@";then local str=$1 ; echo ${#str} ; return 0 else echo "Your input is not correct,it must be one string!" return 1 fi } # 9.判断yes or no,此函数在脚本中可用于配合read命令: func_is_yesorno(){ if func_ifinput_isone $@;then local input=`echo $1 | tr "A-Z" "a-z"` [ "$input" = "y" -o "$input" = "yes" ] && return 0 [ "$input" = "n" -o "$input" = "no" ] && return 1 echo -e "Your input is wrong, consider is as \`no'" return 1 elif ! func_ifinput_exist $@;then echo -e "Your input is not detected, consider it as \`no'" return 1 else echo -e "Your input arguments are more than one, consider it as \`no'" return 1 fi } # 11.判断系统版本 func_osversion(){ if [ -e /etc/centos-release ];then local version=`grep -Eo " [0-9.]+ " /etc/centos-release` elif [ -e /etc/os-release ];then local version=`sed -nr '/^VERSION=/s@^[^0-9]*([0-9.]+).*$@\1@p' /etc/os-release` else echo -e "No os-release files!" return 1 fi if [ "$1" = "-a" ];then echo $version else echo $version | sed -nr 's@^([0-9]+)\.?.*$@\1@p' fi return 0 } # 13.判断脚本所在目录 # 注意!此函数只能在脚本中执行,用于判断被执行的脚本所在的目录 # 其定义了一个环境变量DIR,表示脚本所在目录,可以把它当做后续的变量来进行使用。 func_pwd(){ # if ! func_ifinput_isnumofarg 1 $* >/dev/null; then # echo -e "`fcs red1`Input missing or more than one arguement!!!`fce`" # return 1 # fi # resolve $SOURCE until the file is no longer a symlink local SOURCE="$0" export DIR while [ -h "$SOURCE" ]; do 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 )" && return 0 } #14.把函数定义为环境函数和删除函数定义,其后面直接跟上函数文件。 func_exp(){ local funcName for funcfiles in $@; do funcName=`/usr/bin/grep -Eo '^f.*\(\){' ${funcfiles} | /usr/bin/grep -Eo 'f[^(){]+'` done #echo $funcName export -f $funcName } func_unset(){ local funcName for funcfiles in $@;do funcName=`/usr/bin/grep -Eo '^f.*\(\){' ${funcfiles} | /usr/bin/grep -Eo 'f[^(){]+'` done unset $funcName }
最后修改日期: 2021年7月7日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。