bash shell tips
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=3
element_i=${array[$i]}
##################################
# Dynamic variable name
#################################
# env variables to be checked
check_env_variables_list=("OPTSTM""STLINUX_TOOLCHAIN""SVN_EDITOR")
i=0
while [ $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}"
fi
done
##################################
# 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 option
do
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
;;
esac
done
##################################
# Substring
#################################
sub1="add"
sub2="addxx"
str="aafaddcddfadd"
sub="${sub1}"
case ${str} in
*"${sub}"*)
echo "${sub} is in ${str}"
;;
*)
echo "${sub} is not in ${str}"
esac
if [[ "${str}" =~ "${sub}" ]]
then
echo "${sub} is in ${str}"
else
echo "${sub} is not in ${str}"
fi
sub="${sub2}"
case ${str} in
*"${sub}"*)
echo "${sub} is in ${str}"
;;
*)
echo "${sub} is not in ${str}"
esac
if [[ "${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."
exit
esac
相關文章
- 【Shell】【Tips】Linux的.bash_profile和.bashrc與子Shell的關係Linux
- [Bash] Curly braces tips
- shell Bash變數變數
- Bash Shell 基礎特性
- Shell(Bash)學習· 總章
- xecuting shell commands with bash
- bash shell 程式與磁碟資料
- shell程式設計–bash變數程式設計變數
- bash shell計算時間差
- Linux的Bash Shell詳解Linux
- linux自定義shell(bash)命令Linux
- 9.bash shell程式設計程式設計
- BASH shell set命令詳解(轉)
- Shell Script(bash)--教學例(轉)
- Linux深入探索04-Bash shellLinux
- HP-UX 安裝使用bash shellUX
- BASH SHELL 程式設計簡介(轉)程式設計
- 20天學會bash shell script (二)
- 20天會學bash shell script(一)
- 20 天學會bash shell script (三)
- shell和bash指令碼命令學習指令碼
- Shell Script(bash)--用於自動備份的Shell Script(轉)
- 執行shell指令碼報錯:-bash: ./test1.sh: /bin/bash^M: ...指令碼
- shell程式設計–bash變數介紹程式設計變數
- Linux中bash shell環境變數Linux變數
- bash shell指令碼執行方法總結指令碼
- Aix6.1 安裝 Bash Shell環境AI
- bash shell 實現快速排序演算法排序演算法
- BASH Shell的指令碼程式設計(轉)指令碼程式設計
- bash shell指令碼接受多個引數指令碼
- bash shell實現2048小遊戲詳解遊戲
- 高階bash/shell指令碼程式設計指南指令碼程式設計
- 【Bash百寶箱】shell命令列快捷鍵大全命令列
- 12 Bash For Loop Examples for Your Linux Shell ScriptingOOPLinux
- Linux Bash Shell學習(十):流程控制——forLinux
- Shell 程式設計:Bash空格的那點事程式設計
- Linux Bash Shell學習(七):shell程式設計基礎——執行Shell指令碼、functionLinux程式設計指令碼Function
- Linux Bash Shell學習(八):shell程式設計基礎——string操作Linux程式設計