以传参的方式执行shell(模板)
作者:互联网
以传参的方式执行shell(模板)
#!bin/bash # USE: Template # author : xiaowei # date: 2019-11-01 # state : -name 选项必选,,, -v -m 选项可选; -v 设置有默认参数 # Version : beta 1.0.1 2019-11-01 function f_syntax { [ ! -z "$1" ] && echo ">>[$(date +"%Y%m%d.%H%M%S")] $1" echo " " echo "syntax:" echo "sh ${ShellName} <-name 'Parameter 1 |Parameter 1'> [-m test] [-v v1.0.1]" echo " " } unset MALLCHECK;export LC_ALL=C;export LANG=en_US;export TZ="BEIST-8"; ShellName="$(echo $0 | awk -F / '{print $NF}')";ShllOption='$@'; ShellPID=$$;PShellName=$(echo "${ShellName}" | sed 's/^[0-9]*_//'); WorkDir="$(echo $0 | sed s/${ShellName}//g)";[ -z "${WorkDir}" ] && WorkDir=${PWD};cd ${WorkDir};WorkDir=${PWD}; LogDir="${WorkDir/logs}";mkdir -p ${LogDir};chmod ugo+rwx ${LogDir} 2>/dev/null; LogFile="${LogDir}/${ShellName}.$(date +"%Y%m%d").log"; touch "${LogFile}" 2>/dev/null;chmod ugo+rw "${LogFile}" 2>/dev/null; typeset Shellcksum=$(cksum ${WorkDir%/}/${ShellName} 2>/dev/null | awk '{printf "%d",$1}'); uname_n=$(uname -n);typeset uname_a=$(uname -a);typeset Platform$(echo ${uname_a%% *} | tr a-z A-Z); typeset -i UserID=${UserID:-$(id -u)};typeset UserName=${UserName:-$(whoami)}; typeset UserHome=${UserHome:-$(awk -F: -v nu="${UserName}" '{if($1==nu){print $6;exit}}' /etc/passwd 2 >/dev/null)}; [[ $# -eq 0 ]] && f_syntax; for inopt in ${ShllOption} do case $(echo $inopt | tr a-z A-Z) in -NAME) CurOpt="-NAME";typeset In_Version="v1.0.1";continue;; -VERSION|-V) CurOpt="-VERSION";continue;; -M) CurOpt="-DESCRIBE";continue;; -*) f_syntax;; esac case #-NAME) typeset In_Name="${inopt}";continue;; -NAME) typeset In_Names="${inopt}";In_Name_s=$(echo ${In_Name} ${In_Names});In_Name=${In_Name_s};continue;; -VERSION) typeset In_Version="${inopt}";continue;; -DESCRIBE) typeset In_M="${inopt}";continue;; esac done [[ -z ${inopt} ]] && echo "-name Parameter does not exist" && f_syntax;
标签:shell,以传,WorkDir,typeset,inopt,continue,ShellName,echo,模板 来源: https://www.cnblogs.com/small-wei/p/11778437.html