搜尋此網誌

2010年12月28日 星期二

bash学习笔记(三)—— 基本语法

变量,判断,重复动作

参数展开是shell提供变量值在程序中使用的过程。如
aa="hello word"
sleep 10
echo $aa 或 "$aa"
变量展开需用"",如果改为'',则不会展开。echo '$aa',输出为'$aa'

替换运算符


${varname:-word}
如果varname存在且非null,则返回其值;否则,返回word。例,如果count未定义,则${count:-0}返回0。

${varname:=word}
如果varname存在且非null,则返回其值;否则,varname=word。

${varname:?message}
如果varname存在且不是null,则返回它的值;否则,显示varname:message后退出脚本。

${varname:+word}
如果varname存在且非null,则返回word;否则,返回null。例,如果count已定义,则${count:+1}返回1。

ps:上述运算符内的冒号都是可选的。如果去掉则将定义中的存在且非null改为存在,即运算符仅用于测试变量是否存在。

模式匹配运算符

${var#pattern}
如果模式匹配于变量值的开头处,则删除匹配的最短部分,并返回剩下部分。

${var##pattern}
如果模式匹配于变量值的开头处,则删除匹配的最长部分,并返回剩下部分。

${var%pattern}
如果模式匹配于变量值的结尾处,则删除匹配的最短部分,并返回剩下部分。

${var%%pattern}
如果模式匹配于变量值的结尾处,则删除匹配的最长部分,并返回剩下部分。

例:
path=/home/tolstoy/mem/long.file.name
${path#/*/} 结果: tolstoy/mem/long.file.name
${path##/*/} 结果:long.file.name
${path%.*} 结果:/home/tolstoy/mem/long.file
${path%%.*} 结果:/home/tolstoy/mem/long

字符串操作:
${#path} : 返回变量的字符串长度, 为32
${string:position:length},例{path:6:11} : 返回6-11字符串:tolstoy/mem
${string/substring/replacement}
Replace first match of $substring with $replacement
${string//substring/replacement}
Replace all matches of $substring with $replacement

特殊变量: 例
$ set -- hello "hi there" greetings         设置新的位置参数
$ echo there are $# total arguments         显示计数值
there are 3 total arguments
$ for i in $* (or $@)                     $*,$@一次表示所有的命令行参数
  do echo i is $i
  done
i is hello
i is hi
i is there
i is greetings
$ for i in "$@"              "$@"将所有命令行参数视为单个字符串,即"$1 $2"
  do echo i is $i
  done
i is hello
i is hi there
i is greetings
$ shift                                    去除第一个参数
$ echo there are $# total arguments
there are 2 total arguments



if-elif-else-fi基本语法:例
#! /bin/bash

echo "Is it morning? Please answer yes or no."
read YES_OR_NO                   #输入value
if [ "$YES_OR_NO" = "yes" ]; then
  echo "Good morning!"
elif [ "$YES_OR_NO" = "no" ]; then
  echo "Good afternoon!"
else
  echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."
  exit 1
fi
exit 0

ps: "[]"为判断语句,返回1或0。if [pipeline] 等价于 if test pipeline。
因此可在[]中加入test的各种参数。
如:
if [ -f "$file" ] && [ ! -x "$file" ]; then
    echo $file is a file but cannot execute
elif [ -d "$file" ]; then
    echo $file is a directory
fi



case/esac基本语法:    case命令可类比C语言的switch/case语句,esac表示case语句块的结束。C语言的case只能匹配整型或字符型常量表达式,而Shell脚本的case可以匹配字符串和Wildcard,每个匹配分支可以有若干条命令,末尾必须以;;结束,执行时找到第一个匹配的分支并执行相应的命令,然后直接跳到esac之后,不需要像C语言一样用break跳出。
#! /bin/bash

echo "Is it morning? Please answer yes or no."
read YES_OR_NO
case "$YES_OR_NO" in
yes|y|Yes|YES)
  echo "Good Morning!";;
[nN][oO])
  echo "Good Afternoon!";;
*)
  echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."
  exit 1;;
esac



for/in/do/done基本语法:例
#! /bin/bash

for FRUIT (in apple banana pear); do
  echo "I like $FRUIT"
done
ps: 如果未写()中内容,则默认执行 for FRUIT in "$#"



while/do/done基本语法:例
#! /bin/sh

echo "Enter password:"
read TRY
while [ "$TRY" != "secret" ]; do
  echo "Sorry, try again"
  read TRY
done

沒有留言:

張貼留言