shell编程(二)

来源:互联网 发布:西数黑盘和蓝盘 知乎 编辑:程序博客网 时间:2024/05/18 03:31

 2.1 变量

什么是Shell变量
 本地变量
环境变量
变量替换(显示变量)
位置变量
标准变脸g特殊变量
影响变量的命令。

本地变量在用户现在的shell生命期的脚本中使用。
variable-name=value
set显示本地所有的变量
readonly variable-name


#LOCALTEST="TEST"
#echo ${LOCALTEST}
TEST

#readonly LOCALTEST
#LOCALTEST="CHINATLAB"
bash:LOCALTEST:readonly variable

#readonly 可以看系统所有的只读的变量

环境变量:
用于所有用户进程(经常称为子进程)
$HOME/.bash_profile(/etc/profile)
环境变量通过export命令来实现。
#export CHINA_TEST="shenzhen"
#env命令查看环境变量


变量替换:
就是显示变量的值比如:
#echo $testvar或者echo ${testvar}

还有其他的方式:
${Variable name} :显示实际值到Variable name
${Variable name:+value} :如果设置了Variable name,则显示其值value,否则,为空
${Variable name:?value}如果未设置variable name,显示用户定义错误信息value。
${variable name:-value}:如果未设置variable name,则显示其值value。
${Variable name:=value}:如果未设置variable name,设置其值,并显示。

变量的清除:
unset命令
#unset testvar
变成readonly的不能把变量取消。


位置变量:

允许有10个,从 $0--$9
 
$0:代表脚本的名字。


举例:建立parm.sh 文件,内容如下:

#!/bin/bash
#parm
echo "This is Scripts's name:$0"
echo "This is Scripts's first position perameter:$1"
echo "This is Scripts's second position perameter:$2"
echo "This is Scripts's third position perameter:$3"
echo "This is Scripts's foureth position perameter:$4"
echo "This is Scripts's fifth position perameter:$5"
echo "This is Scripts's sixth position perameter:$6"
echo "This is Scripts's severnth position perameter:$7"
echo "This is Scripts's eighth position perameter:$8"
echo "This is Scripts's nineth position perameter:$9"
~
~
~
~

chmod 764 parm.sh
 
运行:
./parm.sh test  A B C D E F G H I
结果:
This is Scripts's name:./parm.sh
This is Scripts's first position perameter:test
This is Scripts's second position perameter:A
This is Scripts's third position perameter:B
This is Scripts's foureth position perameter:C
This is Scripts's fifth position perameter:D
This is Scripts's sixth position perameter:E
This is Scripts's severnth position perameter:F
This is Scripts's eighth position perameter:G
This is Scripts's nineth position perameter:H

标准变量:
bash默认建立了一些标准环境变量,可在/etc/profile中定义。
EXINIT
HOME
IFS 含义是字符与字符的间隔用什么表示。
LOGNAME
MAIL
MAILCHECK:每个多少秒接受一次邮件。
MAILPATH:多个邮箱的情况下,设置路径
TERM:进入后终端的类型  TERM=vt100
PATH:
TZ:时区
PS:
PS1注意'[/u@/h /W]/$'的解释。
PS2

pwd:当前目录
TERMINFO:


2.1.7 特殊变量:
$#:传递到脚本的参数列表
$*:以一个单字符串显示所有向脚本传递的参数,与位置变量不同,此选项参数可超过9个
$$:脚本运行的当前进程ID号
$!:后台运行的最后一个进程的进程ID号
$@与$#相同,但是使用时加引号,并在引号中返回每个参数。
$-:显示shell使用的当前选项,与set命令功能相同
$?:像是最后命令的退出状态,0代表没有错误,其他任意值表明有错误。

放入刚才的例子中运行一下看下结果。

2.1.8 影响变量的命令:

declare
设置或者显示变量
-f 只显示函数名
-r创建只读变量(declare和typeset)
-x 创建转出变量
-i创建整数变量
使用+代替-可以点到选项的含义

export
用于创建传给于shell的变量
--表明选项结束,所有后续参数都是实参
-f 表明在“名-值”对中的名字是函数名
-n 把全局变量转换成局部变量
换句话说:明亮的变量不再传递给shell
-p 显示全局变量列表。

readonly:
和set就不多说了

unset
-f删除只读变量。

shift【n】
用于移动位置变量,调整位置变量,使$3的值赋予$2,$2的值赋予$1
typeset
用户显示或设置变量
是declare的同义词

 

练习:用echo命令打出来看一下结果。


2.2 引号
引号的必要性

echo ert * 默认文件名打印出来
echo "ert *"


双引号可以引用除 $,`./外的任意字符或字符串。
单引号和双引号类似
反引号 用于设置系统命令的输出到变量,shell将反引号中的内容作为一个系统命令并执行其内容。


2.3.1 运算符类型
为运算符
~op1 :反运算符,把op1中所有的二进为1的都变为0,0变为1
op1<<op2:左移运算符:
op1>>op2
op1&op2
op1^op2
op1|op2

逻辑运算符
&& ||
> ,==,<,!=.
赋值运算符:
=,+=,-=,*=,/=,%=,&=,^=.|=,<<=,>>=

let count=$count+$change
let count +=$change

表达式替换:
$[]和(())
--习惯使用$[],,所有shell的求值都是用整数完成。
$[]可以接受不同基数的数字
-【base#n】n表示基数从2到36的任意基数

echo $[10#8+1]
9

运算符的优先级:


 

原创粉丝点击