变量与常用符号

1、shell变量分本地变量与环境变量

  局部(本地)变量:本地变量在用户现在的shell生命周期的脚本中使用

    定义:变量名=值

    • 等号两边不可以有空格
    • 取值包括空格,必须用双引号括起来
    • shell变量区分大小写
    • 声明变量不用声明类型
    • 可以存储不同类型的内容
    • 使用灵活
    • 使用时要明确变量的类型   
    • 必须以字母或下划线开头,不能以数字开头定义变量. 

清除变量:

    • name=arcerzhang  --定义变量
    • echo ${name}     --输出变量  
    • unset name      --清除变量 

变量扩展修饰符: 

${name:-arcerzhang}  假如变量name存在,且非空则直接输出name的值,否则将arcerzhang的值赋给name,然后输出.

${variable:=word}

${variable:+word}

${variable:?word}

${variable:offset}

  环境变量:

2、设置只读变量

View Code
[arcerzhang@DG2 shell]$ readonly freeviedo=boobooke
[arcerzhang@DG2 shell]$ freeviedo=arcerzhang
-bash: freeviedo: readonly variable
[arcerzhang@DG2 shell]$

3、环境变量

环境变量用于所有用户进程(经常称为子进程)。登陆进程为父进程;

  • 环境变量可用于所有子进程,这包括编辑器、脚本和应用
  • 环境变量可用于在命令行中设置,但用户注销时这些值将丢失
  • 环境变量均为大写
  • 必须用export命令导出

设置环境变量

  •   VARIABLE-NAME=value; export VARIABLE-NAME
  •   VARIABLE-NAME=value
  •   export VARIABLE-NAME

 4、定义数组

View Code
[arcerzhang@arcerzhang shell]$ clear
[arcerzhang@arcerzhang shell]$ myarr=(arcerzhang maryhu lyidazhang)
[arcerzhang@arcerzhang shell]$ echo ${#myarr}
10
[arcerzhang@arcerzhang shell]$

查看数组元素的个数

View Code
[arcerzhang@arcerzhang ~]$ myarr=(arcerzhang maryhu lydiazhang)
[arcerzhang@arcerzhang ~]$ echo ${#myarr[*]}
3
[arcerzhang@arcerzhang ~]$

输出数组中的元素;默认从0开始;统计数组第一个元素的长度${#myarr}

View Code
[arcerzhang@arcerzhang ~]$ myarr=(arcerzhang maryhu lydiazhang)
[arcerzhang@arcerzhang ~]$ echo ${#myarr[*]}
3
[arcerzhang@arcerzhang ~]$ echo ${myarr[1]}
maryhu
[arcerzhang@arcerzhang ~]$ echo ${myarr[0]}
arcerzhang
[arcerzhang@arcerzhang ~]$ echo ${myarr[0]} ${myarr[1]}
arcerzhang maryhu
[arcerzhang@arcerzhang ~]$ echo ${myarr[0]} ${myarr[1]} ${myarr[2]}
arcerzhang maryhu lydiazhang
[arcerzhang@arcerzhang ~]$

输出所有元素:echo ${myarr[*]}

5、特定变量参数

  • $#  传递 到脚本的参数个数
  • $*  以一个单字符串显示所有向脚本传递的参数
  • $$      脚本运行的当前进程ID号
  • $!      后台运行的最后一个进程的进程ID号
  • $@     与$#相同,但是使用时加引号,并在引号中返回每个参数
  • $-      显示sehll使用的当前选项,与set命令功能相同
  • $?      显示最后命令的退出状态。0表示没有错误,其他值表示有错误.

6、变量扩展

  • ${var:pos} 变量var从位置pos开始扩展
  • ${var:pos:len}从位置pos开始,并扩展len长度个字符
  • ${var/Pattern/Replacement}使用Replacement来替换var中的第一个Pattern的匹配
  • ${var//Pattern/Replacement}全局替换在var中所有的匹配,都会用Replacement来替换

7、指定类型的变量

declare或者typeset

  • -r 只读
    • declare -r var1
    • declare -r var2与readonly var1是完全一样的
  • -i 整形
    • declare -i number
  • -a 数组
  • -f 函数
  • -x export

 8、变量的间接引用

View Code
[arcerzhang@DG3 shell]$ admin=wilson
[arcerzhang@DG3 shell]$ wilson=boobooke
[arcerzhang@DG3 shell]$ echo $admin
wilson
[arcerzhang@DG3 shell]$ eval admin=\$$admin
[arcerzhang@DG3 shell]$ echo ${admin}
boobooke
[arcerzhang@DG3 shell]$

 9、特殊字符

#

注释,行首以#开头为注释(#!是个例外)

注释也可以存在与本行命令的后边

命令是不能跟在同一行上注释的后边的

在echo命令中被转义的#是不能作为注释的

#也可以出现在特定的参数替换结构中或者是数字常量表达式中

[arcerzhang@DG3 ~]$ echo "The #here does not begin a comment."
The #here does not begin a comment.
[arcerzhang@DG3 ~]$ echo 'The #here does not begin a comment.'
The #here does not begin a comment.
[arcerzhang@DG3 ~]$ echo The \#here does not begin a comment.
The #here does not begin a comment.
[arcerzhang@DG3 ~]$ echo The #这里开始一个注释
The
[arcerzhang@DG3 ~]$

 

echo ${PATH#*:} #参数替换,不是一个注释

echo $((2#101011)) #数制转换,不是一个注释

[arcerzhang@DG3 ~]$ echo $((2#101011))
43

 #Thanks,S.C.

 ;

 命令分隔符,可以用来在一行中来写多个命令

View Code
  filename=arcerzhang.log
  if [ -x "$filename" ] ;then
          echo "File $filename exists.";cp $filename $filename.bak
  else
          echo "File $filename not fount.";touch $filename
  fi;echo "File test complete."

;;

终止"case"选项.

#!/bin/bash
var=abc
case "$var" in
abc) echo "\$var=abc";;
xyz) echo "\$var=xyz";;
esac

.

.作为文件名的一部分。如果作为文件名的前缀的话,那么这个文件将成为隐藏文件

.命令如果作为目录名的一部分的话,那么.表达的是当前目录.".."表示上一级目录

.字符匹配,这是作为正则表达式的一部分,用来匹配任何的单个字符

demo:匹配第四位为x(可执行权限的文件)

[arcerzhang@DG3 shell]$ ls -l | grep ^...x
-rwxrw-r-- 1 arcerzhang arcerzhang  88 3月  31 23:29 case.sh
-rwxrw-r-- 1 arcerzhang arcerzhang 195 3月  31 23:22 cmd.sh
-rwxrw-r-- 1 arcerzhang arcerzhang  48 3月  31 22:46 hello.sh
-rwxrw-r-- 1 arcerzhang arcerzhang  62 3月  31 13:30 param.sh
-rwx--x--x 1 arcerzhang arcerzhang 218 3月  31 13:16 random.sh

"部分引用"STRING"阻止了一部分特殊字符

[arcerzhang@DG3 shell]$ welcom=How
[arcerzhang@DG3 shell]$ welcom=How are you
-bash: are: command not found
[arcerzhang@DG3 shell]$ welcom="How are you"
[arcerzhang@DG3 shell]$ echo ${welcom}
How are you
[arcerzhang@DG3 shell]$

'全引用.'STRING'阻止了全部特殊字符

[arcerzhang@DG3 shell]$ echo ${welcom}
How are you
[arcerzhang@DG3 shell]$ echo '${welcom}'
${welcom}
[arcerzhang@DG3 shell]$

,逗号链接了一系列的算术运算操作

[arcerzhang@DG3 shell]$ let "var=((a=9,15/3))"
[arcerzhang@DG3 shell]$ echo ${var}
5
[arcerzhang@DG3 shell]$

所有的内容都被运行了,但只有最后一项被返回.

\ 转义字符

[arcerzhang@DG3 shell]$ echo "This is a book"
This is a book
[arcerzhang@DG3 shell]$ echo "This is a \"book\""
This is a "book"
[arcerzhang@DG3 shell]$

/文件名路径分隔符.或用来做除法操作

[arcerzhang@DG3 shell]$ cd /etc/sysconfig/
[arcerzhang@DG3 sysconfig]$ let x=16/4
[arcerzhang@DG3 sysconfig]$ echo ${x}
4
[arcerzhang@DG3 sysconfig]$

`后置引用,命令替换 

[arcerzhang@DG3 ~]$ echo "您当前所在的目录是`pwd`"
您当前所在的目录是/home/arcerzhang
[arcerzhang@DG3 ~]$

 :空命令(true作用等同于=:)

#!/bin/bash
while :
do
        echo "welcom boobooke"
        sleep 5
done

#!/bin/bash
while true
do
        echo "welcom boobooke"
        sleep 5
done

 !取反操作符 

View Code
#!/bin/bash
x=1
y=2
if [ "$x" != "$y" ]
then
        echo "x不等于y"
else
        echo "x等于y"
fi

 *万能匹配字符(数学运算符) 

View Code
#操作符号* 表示乘
let b=5*5
echo $b
#**表示幂次方
let c=2**10
echo $c

 ?测试操作结果

View Code
#?测试操作结果,查看返回状态码
mkdir /root
echo $?

 #变量替换

View Code
#变量替换
a=5
b=7
echo $a $b

 $在正则表达式中作为行结束符

${}参数替换

$*,$@位置参数

$?退出状态百年来那个.$?保存一个命令/一个函数或者脚本本身的退出状态

$$进程ID变量.这个$$变量保存运行脚本进程ID

()命令组

View Code
arr=(arcerzhang maryhu lydiazhang)
echo $arr      #输出第一个元素
echo ${arr[*]} #输出全部元素
echo "查看下面实例,输出的结果将是welcom,而不会是boobooke;因为在()中的>命令列表,将作为一个子shell来运行"
a=welcom
(a=boobooke)
echo $a

{}代码块,又被称为内部组

View Code
cat {a.txt,b.txt} > c.txt

[] test ;[]还可以表示数组元素echo ${arr[*]}

View Code
##########################################
#[] test
#-lt 小于       --less than
#-gt 大于       --greater than
#-le 小于等于   --less than or equal
#-ge 大于等于   --greater than or equal
#-ne 不等于     --not equal
##########################################
echo "case one:"
x=3;y=2
if test $x -lt $y
then
        echo "x < y"
else
        echo "x > y"
fi
echo "case two:"
x=3;y=2
if [ $x -lt $y ]
then
        echo "x < y"
else
        echo "x > y"
fi

echo "case three:"
x=3;y=2
if [ $x -gt $y ]
then
        echo "x > y"
else
        echo "x < y"
fi

echo "case four:"
x=3;y=2
if test $x -gt $y

[]还可以表示数组元素 echo ${arr[*]}

[]还可以表示字符范围 grep ^[a-z] /root/anaconda-ks.cfg;过滤以a-z中开头的字符;

View Code
[arcerzhang@DG3 shell]$ grep ^[0] c.txt
00000000000000000

>&>>&>>< 重定向

\<,\>正则表达式中的单词边界

View Code
[arcerzhang@DG3 shell]$ vi c.txt
[arcerzhang@DG3 shell]$ grep '\<chongqing\>' c.txt
hello ,this is arcerzhnag!welcome to chongqing;
[arcerzhang@DG3 shell]$ vi c.txt
[arcerzhang@DG3 shell]$ grep '\<chongqing\>' c.txt
hello ,this is arcerzhnag!welcome to chongqing;
hello ,this is maryhu!welcome to chongqing too;
[arcerzhang@DG3 shell]$

管道.分析前边命令的输出,并将输出作为后边命令的输入

ls -l | grep abc

ps -ef | grep httpd

Ip=ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2}' | awk -F ':''{print:$2}'

&& 与逻辑操作(并且)

make && make install

|| 或逻辑操作

mkdir /root || ls -l  第一个命令如果执行成功,就不会执行第二命令;否则,才会执行第二命令.

&后台云新该命令.一个命令后跟一个&,表示将在后台运行.

sleep 5

sleep 5 &

posted @ 2013-03-30 20:06  ArcerZhang  阅读(1162)  评论(0编辑  收藏  举报