O'Reilly - Learning the bash Shell
Advanced Bash-Scripting Guide
http://www.tldp.org/LDP/abs/html/
Substitution
cf. 4.3.1
var="" var/=""
${var:-value} "" & value $var & $var
${var:=value} $var = value $var & $var
${var:+value} "" & "" $var & value
# Example
np=${1:-1} # np=$1 if defined ($1); else np=1
Note: ${1:=value} is invalid
Pattern matching
cf. 4.3.2
# & ## DELETE the shortest/longest matching from the head
% & %% DELETE the shortest/longest matching from the tail
${filename##*/} # basename [file] after the last /
${filename%/*} # dirname [file] before the last /
${filename%.*} # strip the extension before the last .
${filename##*.} # get the extension after the last .
# setup login shell
chsh
# or in .profile
[ -f /bin/bash ] && exec /bin/bash --login
# ls --color
# kernel
kernel=`uname -s`
case $kernel in
FreeBSD )
alias ls='ls -G'
alias vi=vim
;;
SunOS )
alias ls='ls --color'
;;
Linux )
#distr=`cat /proc/version`
alias ls='ls --color'
;;
* )
echo "unknown kernel-name"
exit
;;
esac
# export & set
export # 察看环境变量
export var # 设置环境变量
unset var # 清除环境变量
readonly var # 设置变量 只读
readonly # 察看只读变量
# Built-in
$EDITOR /usr/bin/vi # 影响处理mail时所用的编辑器
$HOME /home/john
$LOGNAME john
$MAIL /var/mail/john
$MANPATH /usr/share/man:/opt/gnome/share/man
$PAGER less
$PATH /usr/bin:/bin
$PS1 [\u@\h:\w]\$
$PWD /home/john/bin
$TERM linux /usr/share/terminfo
$RANDOM
$BASH /bin/bash # 当前shell
$SHELL /bin/bash # 登陆shell
# 整型变量
declare -i 声明类型
let int_var=exp 整数赋值
$((...)) 表达式求值
[ $(( 3>2 )) = 1 ] 关系
# 数组
declare -a # 声明类型
array[0]=0th
array=([0]=0th [2]=2nd [1]=1st)
array=(0th 1st 2nd)
array[month]=Feb # 下标不必为数字
unset array
unset array[i]
unset array[@]
for i in "${array[@]}"; do # 对数组元素循环, 不是对下标循环
...
done
# function
funct_name ()
{
local var_name
...
}
stty -a
C-M \r, RETURN
C-J \n, Newline
C-L \f, Formfeed
No comments:
Post a Comment