Linux Shell学习笔记

一、变量

1、普通变量

variable1=hello
variable2="hello world"
echo $variable1   //output hello
echo $variable2   //output helloworld

 变量自增的实现:

方法一:

i=0

i=`expr $i + 1`  #中间有空格,加号两边也有空格

方法二:

let i=$i+1

方法三:

i=$(expr $i + 1)

 

2、数组变量:

详细用法:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html

//赋值方式1:
arr[0]=a;
arr[1]=b;
arr[2]=c;
//赋值方式2:
arr=(a b c);
//输出数组的某一项:
echo ${arr[2]} //输出:c
//输出数组的全部项:
echo ${arr[*]} //output: a b c
或者  echo ${arr[@]}

3、只读变量

variable=hello
readonly variable
echo variable  //ouput hello
variable=world  //产生错误,不能修改

4、如何删除变量

unset variable  //不能够使用unset删除只读变量

5、环境变量

  在shell中包含三种变量:局部变量,环境变量和shell变量。局部变量就是只在当前shell脚本中使用的变量,而其他shell脚本不能够使用这个变量;环境变量在任何shell脚本的子进程中都能使用的变量;shell变量是指shell设置的特殊变量,也是shell正确运行所必需的变量,这些变量有些是局部变量,有些是环境变量。

  我们可以通过导出(export)将变量加入到环境变量中,语法如下:

  PATH=/bin/sh;export PATH   或者  export PATH=/bin/sh

6、shell变量

例如:

PATH         命令搜索路径,以冒号为分隔符.注意与DOS下不同的是, 当前目录不在系统路径里 
HOME         用户home目录的路径名,是cd命令的默认参数 
7、变量替换的缺省值

${parameter:-word}  如果parameter为空或者未设置,则使用word来替代,同时parameter的值不变

${parameter:+word}  如果parameter设置了,则使用word来替代,同时parameter的值不变

${parameter:=word}  如果parameter为空或者未设置,则parameter值设置为word

${parameter:?message}  如果parameter为空或者未设置,则message作为标准错误打印出来,用来检查变量是否正确设置

8、命令替换赋值

var_Date=`date`  //这里将执行date命令,输出的数据赋给var_Date

var_user=`who|wc -l`  //这里将查询用户的数

grep `id -un` /etc/passwd  //  这里会先执行id -un命令,得出数据,在执行grep命令

二、流控制

1、if 语句

字符串比较:http://blog.163.com/xychenbaihu@yeah/blog/static/13222965520101029635555/

if list1
then list2
elif list3
then list4
else
list5
fi

在if语句中使用test语法,test用法可以分为三类:文件测试,字符串测试,数字比较,语法为:

test option file/string/int  或[ option file/string/int]

例子:

if[-d /home/bin/man] then echo "the path is exist"  //测试路径是否存在
argc=$#;

echo $argc

if [ $argc != 2 ]

then

    echo "hello world";

else

    echo "error";

fi

exit;

 

temp="e"

if [ -n $temp ]

then

    echo "ok"

else

    echo "error"

fi

 

 

2、case语句

case word in 
pattern1)
list1;;
pattern2)
list2;;
   pattern3)
list3;;
esac
temp="e"

case $temp in

    "a")

        echo "a char";;

    "e")

        echo "e char";;

    "d")

        echo "d char";;

    *)

        echo "all char";;

esac

 

三、循环

1、while循环

while command
do
list
done

例子

#!/bin/bash
i=0
while [ $i -lt 10 ]
do
echo $i
i=`expr $i + 1` //或者 i=$(($i+1))
done

例子二、

#!/bin/bash
i=0
while((i<10));
do
echo $i
i=$(expr $i + 1)
done



2、for循环     详细用法:http://hi.baidu.com/sunboy_2050/item/888e44e11b36b3bd2e140bcc

例子一、

i=0
for((i=1;i<10;i++));do
echo $i
done

例子二、

for i in stringchar{0..5};do

    echo $i

done

//输出:

stringchar0

stringchar1

stringchar2

stringchar3

stringchar4

stringchar5

 

四、符号

1、命令代换符 `或者$()

例子:

Date=`date`

Date=$(date)

echo $Date


2、算数代换符$(())

例子:

var=45

echo $((var+3)) //输出48


五、shell输出颜色控制:

txtblk='\e[0;30m' # Black - Regular

txtred='\e[0;31m' # Red

txtgrn='\e[0;32m' # Green

txtylw='\e[0;33m' # Yellow

txtblu='\e[0;34m' # Blue

txtpur='\e[0;35m' # Purple

txtcyn='\e[0;36m' # Cyan

txtwht='\e[0;37m' # White

bldblk='\e[1;30m' # Black - Bold

bldred='\e[1;31m' # Red

bldgrn='\e[1;32m' # Green

bldylw='\e[1;33m' # Yellow

bldblu='\e[1;34m' # Blue

bldpur='\e[1;35m' # Purple

bldcyn='\e[1;36m' # Cyan

bldwht='\e[1;37m' # White

unkblk='\e[4;30m' # Black - Underline

undred='\e[4;31m' # Red

undgrn='\e[4;32m' # Green

undylw='\e[4;33m' # Yellow

undblu='\e[4;34m' # Blue

undpur='\e[4;35m' # Purple

undcyn='\e[4;36m' # Cyan

undwht='\e[4;37m' # White

bakblk='\e[40m'   # Black - Background

bakred='\e[41m'   # Red

bakgrn='\e[42m'   # Green

bakylw='\e[43m'   # Yellow

bakblu='\e[44m'   # Blue

bakpur='\e[45m'   # Purple

bakcyn='\e[46m'   # Cyan

bakwht='\e[47m'   # White

txtrst='\e[0m'    # Text Reset

 









你可能感兴趣的:(Linux shell)