momo zone

调核人的blog

bash shell 手记

bash shell 快捷键

  • Control-A——此键绑定将当前位置定位到行开头。
  • Control-E——此键绑定将当前位置定位到行尾。
  • Control-K——此键绑定删除截止行尾的所有内容。
  • Control-U——此键绑定删除截止行头的所有内容。
  • Meta-B——此键绑定后退一个单词。
  • Meta-F——此键绑定向前推进一个单词。
  • Meta-D——此键绑定删除当前单词。


设置变量 :

$ myvar=’This is my environment variable!’

取消变量

unset myvar

使用变量 :

$ echo foo${myvar}bar
fooThis is my environment variable!bar

强引用

$ my=’hello’

$ echo “I say: ${my}-my baby”

I say hello-my baby

弱引用

$ my=’hello’

$ echo ‘I say: ${my}-my baby’

I say ${my}-my baby

PS: “$var”和$var有什么区别?

看看这个例子: hello=a b c d    e

$ echo $hello


a b c d e

$ echo $”hello”


a b c d    e

加上了””引用, 变量中的空格将保留, 否则多个空格被bash读取为一个空格.

取得目录名:

$ dirname /usr/local/share/doc/foo/foo.txt
/usr/local/share/doc/foo

取得文件名

$ basename /usr/local/share/doc/foo/foo.txt
foo.txt

命令替换:

$ MYDIR=`dirname /usr/local/share/doc/foo/foo.txt` (注意是“ 而不是 ‘ ‘)
$ echo $MYDIR
/usr/local/share/doc/foo

$ MYDIR=$(dirname /usr/local/share/doc/foo/foo.txt)
$ echo $MYDIR
/usr/local/share/doc/foo

字符截断(首):

$ MYVAR=foodforthought.jpg
$ echo ${MYVAR##*fo}                       #${} 固定用法
rthought.jpg
$ echo ${MYVAR#*fo}
odforthought.jpg

字符截断(尾):

$ MYFOO=”chickensoup.tar.gz”
$ echo ${MYFOO%%.*}
chickensoup
$ echo ${MYFOO%.*}
chickensoup.tar

循环:

1.

myvar=0
while [ $myvar -ne 10 ]
do
echo $myvar
myvar=$(( $myvar + 1 ))

done

2.
myvar=0
until [ $myvar -eq 10 ]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done

选择:

case “${x##*.}” in
gz)
gzunpack ${SROOT}/${x}
;;
bz2)
bz2unpack ${SROOT}/${x}
;;
*)
echo “Archive format not recognized.”
exit
;;
esac

条件:

if [ ${1##*.} = tar ]
then
echo “(uncompressed tar)”
fi
tar tvf $1

子例程包含(相当于C中的include)

.       “/usr/lib/pm-utils/pm-function”

注意 中间有一个空格

另一个重要技巧  , 指定前缀补齐:

输入之前输入的部分内容后再按shift + up/down将显示之前所有该前缀的执行语句。比如tail -f 后shift+up/down将显示 之前输入过的tail -f /var/log/message ,tail -f /var/log/boot.msg 等

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: