bash shell tips

来源:互联网 发布:python中return怎么用 编辑:程序博客网 时间:2024/04/29 06:18

http://www.linuxsir.org/main/doc/abs/abs3.7cnhtm/options.html
http://tldp.org/LDP/abs/html/options.html
选项用来更改shell或/和脚本行为的机制
用set 或者#!/bin/bash -e 来更改脚本的执行选项。如同运行/bin/bash时的命令行配置选项
比如#!/bin/bash -e 可以使脚本执行到返回错误(返回值非0)的指令就退出。注:用diff的时候,有不同时候返回非0
不过一个脚本要是用于source <脚本> 就只能用 return来退出,用-e不起作用,如:[ -z "${TOPDIR}"] && return

http://www.gnu.org/software/bash/manual/bashref.html
http://tldp.org/LDP/abs/html/index.html

################################## Array#################################list=$(ls ${HOME}/)array=( ${list} )length=${#array[@]}length=${#array[*]}i=3element_i=${array[$i]}################################### Dynamic variable name################################## env variables to be checkedcheck_env_variables_list=("OPTSTM""STLINUX_TOOLCHAIN""SVN_EDITOR")i=0while [ $i -lt ${#check_env_variables_list[@]} ]do var_name=${check_env_variables_list[$i]} echo -n "Checking ${var_name} ... " var=$(eval echo \$${var_name}) if [ "x${var}" == "x" ] then   echo "${var_name} is not found or it is empty" else   echo "Found ${var_name}=${var}" fidone################################### Parse fields from lines################################## Example 1# $ svn info# Path: .# URL: http://forge-urd44.osn.sagem/svn/kdg-cdi/BO/branches/BO_kdg-cdi_KDGPVRv2KDGZv1# Repository Root: http://forge-urd44.osn.sagem/svn/kdg-cdi# Repository UUID: 285cafe6-30df-41da-affa-20e2169ee358# Revision: 4519# Node Kind: directory# Schedule: normal# Last Changed Author: g360386# Last Changed Rev: 4516# Last Changed Date: 2011-11-11 23:41:07 +0800 (Fri, 11 Nov 2011)URL=$(svn info ${svn_root_dir} | grep 'URL:[[:space:]]*.*' \ | sed -e 's/URL:[[:space:]]*\(.*\)$/\1/')ROOT_URL=$(svn info ${svn_root_dir} | grep 'Repository Root:[[:space:]]*.*' \ | sed -e 's/Repository Root:[[:space:]]*\(.*\)$/\1/')echo URL=${BO_URL}echo ROOT_URL=${BO_ROOT_URL}# Example 2# $ cat /proc/5261/stat# 5261 (bash) S 1912 5261 5261 34819 5379 4202496 5656 28429 0 3 21 8 6 3 20 0 1 0# 2769099 8339456 1214 4294967295 134512640 135312896 3213966592 3213965320# 3077989412 0 65536 36u86404 1266761467 3238380531 0 0 17 0 0 0 45 0 0# $ man proc# /proc/[pid]/stat# Status information about the process. This is used by ps(1). # It is defined in /usr/src/linux/fs/proc/array.c.## pid %d The process ID.## comm %s The filename of the executable, in parentheses. # This is visible whether or not the executable is swapped out.## state %c One character from the string "RSDZTW" where R is running, S# is sleeping in an interruptible wait, D is waiting in uninterruptible disk# sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.stat_str=$(cat /proc/5261/stat)pid=$(echo ${stat_str} | cut -d ' ' -f 1)cmd=$(echo ${stat_str} | cut -d ' ' -f 2)state=$(echo ${stat_str} | cut -d ' ' -f 3)# Example 3# $ cat /proc/5261/stat | awk -F ' ' '{print "pid="$1",cmd="$2}'# pid=5261,cmd=(bash)############################################################# sort by number in file name############################################################# $ find ./ -type f | sort -V# ./Patch1_2# ./Patch2_3# ./Patch3_4# ./Patch4_5# ./Patch5_6# ./Patch6_7# ./Patch7_8# ./Patch8_9# ./Patch9_10# ./Patch10_11# ./Patch11_12# $ ls# Patch10_11# Patch11_12# Patch1_2# Patch2_3# Patch3_4# Patch4_5# Patch5_6# Patch6_7# Patch7_8# Patch8_9# Patch9_10############################################################# getopts############################################################while getopts P:D:I:s:n:m:N:r:lh optiondo case "${option}" in  P) PROJECT=${OPTARG}   echo "Project ${PROJECT}"   ;;  D) DELIV=${OPTARG}   echo "Deliv ${DELIV}"   ;;  I) PATCH_NUM=${OPTARG}   echo "Patch Number ${PATCH_NUM}"   ;;  s) SAGEM=${OPTARG}   echo "Sagem bugzilla id ${SAGEM}"   ;;  n) NDS=${OPTARG}   echo "NDS bugzilla id ${NDS}"   ;;  m) ST=${OPTARG}   echo "ST bugzilla id ${ST}"   ;;  N) NAME=${OPTARG}   echo "Patch Name ${NAME}"   ;;  r) REV_MN=${OPTARG}   echo "SVN Patch Revision ${REV_MN}"   ;;  l) echo "Use SVN log to see files updated"   SVN=1   ;;  h) echo "Display Help"   Display_Help   exit 0   ;;  *) echo "Hmm, an invalid option was received."   echo "option is ${option}; arg is ${OPTARG}"   exit 1   ;; esacdone##################################  # Substring#################################  sub1="add"sub2="addxx"str="aafaddcddfadd"sub="${sub1}"case ${str} in     *"${sub}"*)        echo "${sub} is in ${str}"        ;;    *)        echo "${sub} is not in ${str}"esacif [[ "${str}" =~ "${sub}" ]]then    echo "${sub} is in ${str}"else    echo "${sub} is not in ${str}"fisub="${sub2}"case ${str} in     *"${sub}"*)        echo "${sub} is in ${str}"        ;;    *)        echo "${sub} is not in ${str}"esacif [[ "${str}" =~ "${sub}" ]]then    echo "${sub} is in ${str}"else    echo "${sub} is not in ${str}"fi##################################  # Script called by source(.) or not################################# case $0 in    *bash)        # you're sourcing it. you're ok            ;;    *)        # we can exit because we're in a shell, not sourcing        echo -e "\nPLEASE NOTE : You can not run this script, you must source it in 'bash' shell."        echo "So do either 'source $0' OR '. $0'"        echo "OR for regular development environment, have alias defined in user login shell environment."        exitesac
原创粉丝点击