Shell脚本学习之二:变量与运算符

variable-name = value

[root@Gwan ~]# LOCALTEST="test"

[root@Gwan ~]# echo ${LOCALTEST}

test

[root@Gwan ~]# echo $LOCALTEST

test

[root@Gwan ~]# set   ##显示所有本地变量,此时能找到LOCALTEST

[root@Gwan ~]# exit

[root@Gwan ~]# set   ##看不到LOCALTEST


readonly variable-name:创建符号常量的好方法!

hours_per_day = 24   seconds_per_hour = 3600  days_per_week = 7    赋值

readyonly hours_per_day seconds_per_hour days_per_week            设置为只读模式

[root@Gwan ~]# LOCALTEST="test"

[root@Gwan ~]# echo $LOCALTEST

test

[root@Gwan ~]# readonly LOCALTEST

[root@Gwan ~]# LOCALTEST="wade tao"

-bash: LOCALTEST: readonly variable

[root@Gwan ~]# readonly  ##查看当前所有的只读变量

declare -ar BASH_VERSINFO='([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")'

declare -ir EUID="0"

declare -r LOCALTEST="test"

declare -ir PPID="6981"

declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"

declare -ir UID="0"


环境变量:用于所有用户进程(又称子进程),登录进程成为父进程。

Shell中执行的用户进程均成为子进程。即可运行在父进程和子进程

export设置或显示/打印(export -p)环境变量;readonly则使得变量不得修改。

[root@Gwan ~]# export CHINAWADE="wade"

[root@Gwan ~]# env | grep CHINA

CHINAWADE=wade   

[root@Gwan ~]# readonly CHINAWADE

显示用户定义错误信息value

[root@Gwan ~]# echo ${testvar1:?"ERROR, no value defined"}

-bash: testvar1: ERROR, no value defined

NOTE】从程序的环境中删除变量,要用env命令,env也可临时地改变环境变量的值。

  env –i  PATH=$PATH    HOME=$HOME  LC_ALL=C    awk  ‘…’ file1 file2

i = initialize  忽略继承的环境,仅使用命令行上所给定的变量与值。

NOTE】打印时,env不会为环境变量加上引号,以供从新输入到shell中,export –p可显示此功能。     


替换运算符

3.  ${变量:-word} 如果变量存在且非空,则返回其值;否则返回word

                用于:如果变量未定义,则返回默认值即word

   ${变量:+word} 如果变量存在且非空,则返回word;否则返回null

                同于:测试变量是否存在

   ${变量:=word} 如果变量存在且非空,则返回其值;否则,设置它为word,并返回其值

                用于:如果变量非订立,则设置变量为默认值,且还返回!

   ${变量:?message} 如果变量存在且非空,则返回其值;否则,显示变量:message,并退出当前的命令或脚本。省略message会出现默认信息:parameter null or not set

                用于:不足忧变量未定义所导致的错误。

[root@Gwan ~]# testvar = “this is a test”

[root@Gwan ~]# echo ${testvar1:-chinawade}

Chinawade                      #-相当于not

[root@Gwan ~]# echo ${testvar:+chinawade}

chinawade

[root@Gwan ~]# echo ${testvar:-chinawade}

this is a test

[root@Gwan ~]# echo ${testvar:=chinawade}

this is a test

[root@Gwan ~]# echo ${testvar2:="chinawade"}

chinawade

[root@Gwan ~]# echo $testvar2

chinawade


变量清除

Unset :从执行中的shell中删除变量(unset -v)与函数(unset -f)

[root@Gwan ~]# echo $testvar

this is a test

[root@Gwan ~]# unset testvar    

[root@Gwan ~]# echo $testvar

 

[root@Gwan ~]# echo $testvar2

chinawade

[root@Gwan ~]# readonly testvar2

[root@Gwan ~]# unset testvar2

-bash: unset: testvar2: cannot unset: readonly variable

##readonly要谨慎使用。


位置变量(位置参数positional parameter):$0 $1 ...... $9 ${10}

Step1 创建脚本parm,并给其执行权限(r4w2x1)

[root@Gwan ~]# ll | grep par

-rw-r--r--  1  774 root       591 Aug  3 23:47 parm

[root@Gwan ~]# chmod 777 parm

[root@Gwan ~]# ll | grep par

-rwxrwxrwx  1  774 root       591 Aug  3 23:47 parm

[root@Gwan ~]# cat parm

#!/bin/bash

#parm

echo "This is the name of the script. $0"

echo "This is the 1st location parameter of the shell: $1"

echo "This is the 2nd location parameter of the shell: $2"

echo "This is the 3rd location parameter of the shell: $3"

echo "This is the 4th location parameter of the shell: $4"

echo "This is the 5th location parameter of the shell: $5"

echo "This is the 6th location parameter of the shell: $6"

echo "This is the 7th location parameter of the shell: $7"

echo "This is the 8th location parameter of the shell: $8"

echo "This is the 9th location parameter of the shell: $9"

 

Step2 执行脚本,赋值。

[root@Gwan ~]# ./parm A B C D E F

This is the name of the script. ./parm

This is the 1st location parameter of the shell: A

This is the 2nd location parameter of the shell: B

This is the 3rd location parameter of the shell: C

This is the 4th location parameter of the shell: D

This is the 5th location parameter of the shell: E

This is the 6th location parameter of the shell: F

This is the 7th location parameter of the shell:

This is the 8th location parameter of the shell:

This is the 9th location parameter of the shell:

 

Step3: 向系统命令传递参数

[root@Gwan ~]# ll | grep parm.sh

-rw-r--r--  1 root root        48 Aug  4 00:14 parm.sh

[root@Gwan ~]# cat parm.sh

#!/bin/bash

#parm.sh

find /root -name $1 -print

[root@Gwan ~]# chmod 755 parm.sh

[root@Gwan ~]# ll | grep parm.sh

-rwxr-xr-x  1 root root        48 Aug  4 00:14 parm.sh

[root@Gwan ~]# ./parm.sh myfile    

/root/myfile

[root@Gwan ~]# ./parm.sh parm

/root/parm


标准变量

bash默认建立了一些标准环境变量,可在/etc/profile中定义

EXINIT  定义vi

HOME  根(登录)目录

[root@Gwan ~]# echo $HOME

/root

IFS    分隔符,缺省是空,显示是:

[root@Gwan ~]# echo $IFS

 

[root@Gwan ~]# IFS=':'

[root@Gwan ~]# echo $PATH

/usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /usr/X11R6/bin /root/bin

[root@Gwan ~]# IFS=''

[root@Gwan ~]# echo $PATH

/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

 

LOGNAME

[root@Gwan ~]# echo $LOGNAME

root

[root@Gwan ~]# set | grep "LOG"

LOGNAME=root

 

MAIL     ##邮箱放在哪里

MAILCHECK  ##每个多少S检查有邮件进来

[root@Gwan ~]# set | grep "MAIL"

MAIL=/var/spool/mail/root

MAILCHECK=60

[root@Gwan ~]# echo $MAIL

/var/spool/mail/root

MAILPATH ##当有多个邮箱的时候使用

TERM    ##总段字符类型

[root@Gwan ~]# set | grep "TERM"

TERM=vt100

PATH ##可执行文件去寻找的路径

[root@Gwan ~]# set | grep "PATH"

PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

TZ ## TIME ZONE

PS1 ## 提示符

[root@Gwan ~]# set | grep "PS1"

PS1='[\u@\h \W]\$ '     u-root h-host w-working directory

PS2 ##一行上面运行多个命令时

PWD ## present working directory

[root@Gwan ~]# set | grep "PWD"

OLDPWD=/home

PWD=/root


特殊变量

[root@Gwan ~]# cat parm

#!/bin/bash

#parm

echo "This is the name of the script. $0"

echo "This is the 1st location parameter of the shell: $1"

echo "This is the 2nd location parameter of the shell: $2"

echo "This is the 3rd location parameter of the shell: $3"

echo "This is the 4th location parameter of the shell: $4"

echo "This is the 5th location parameter of the shell: $5"

echo "This is the 6th location parameter of the shell: $6"

echo "This is the 7th location parameter of the shell: $7"

echo "This is the 8th location parameter of the shell: $8"

echo "This is the 9th location parameter of the shell: $9"

echo "Display the number of parmeters:$#"

echo "Display all parameters in the script.:$*"

echo "Display process ID:$$"

echo "Display the exit state of the former CMD:$?"

[root@Gwan ~]# ./parm  A CHINA CHINAWADE GWAN

This is the name of the script. ./parm

This is the 1st location parameter of the shell: A

This is the 2nd location parameter of the shell: CHINA

This is the 3rd location parameter of the shell: CHINAWADE

This is the 4th location parameter of the shell: GWAN

This is the 5th location parameter of the shell:

This is the 6th location parameter of the shell:

This is the 7th location parameter of the shell:

This is the 8th location parameter of the shell:

This is the 9th location parameter of the shell:

Display the number of parmeters:4

Display all parameters in the script.:A CHINA CHINAWADE GWAN

Display process ID:10117

Display the exit state of the former CMD:0


1.8影响变量的命令

declare -设置或显示变量

export --创建传给子shell的变量

-p显示所有

declare -x USER="root"

[root@Gwan ~]# export country="USA"

[root@Gwan ~]# export | grep cou

declare -x country="USA"

readonly

 

[root@Gwan ~]# ./parm A CHINA chinawade gwan

This is the name of the script. ./parm

This is the 1st location parameter of the shell: A

This is the 2nd location parameter of the shell: CHINA

This is the 3rd location parameter of the shell: chinawade

This is the 4th location parameter of the shell: gwan

This is the 5th location parameter of the shell:

This is the 6th location parameter of the shell:

This is the 7th location parameter of the shell:

This is the 8th location parameter of the shell:

This is the 9th location parameter of the shell:

Display the number of parmeters:4

Display all parameters in the script.:A CHINA chinawade gwan

Display process ID:10404

Display the exit state of the former CMD:0

This is the 1st location parameter of the shell: chinawade

This is the 2nd location parameter of the shell: gwan

[root@Gwan ~]# cat parm

#!/bin/bash

#parm

echo "This is the name of the script. $0"

echo "This is the 1st location parameter of the shell: $1"

echo "This is the 2nd location parameter of the shell: $2"

echo "This is the 3rd location parameter of the shell: $3"

echo "This is the 4th location parameter of the shell: $4"

echo "This is the 5th location parameter of the shell: $5"

echo "This is the 6th location parameter of the shell: $6"

echo "This is the 7th location parameter of the shell: $7"

echo "This is the 8th location parameter of the shell: $8"

echo "This is the 9th location parameter of the shell: $9"

echo "Display the number of parmeters:$#"

echo "Display all parameters in the script.:$*"

echo "Display process ID:$$"

echo "Display the exit state of the former CMD:$?"

shift 2

echo "This is the 1st location parameter of the shell: $1"

echo "This is the 2nd location parameter of the shell: $2"

 

2.2 引号

2.2.1 引用的必要性

变量和替换操作,在脚本中执行变量替换时最容易犯的一个错误就是引用错误。

[root@Gwan ~]# echo ert *

ert 10201_database_linux32.zip anaconda-ks.cfg Desktop firefox-3.6.3.tar.bz2 gnome-libs-devel-1.4.1.2.90-44.1.i386.rpm GwanRPMS helloworld.sh hifile install.log install.log.syslog myfile parm parm.sh wade

[root@Gwan ~]# echo "ert *"

ert *

2.2.2 双引号

使用双引号可引用除$', \外的任意字符或字符串。

[root@Gwan ~]# echo -e "ert, $SHELL '\n* china`echo wade` "

ert, /bin/bash '

* chinawade 

2.2.3 单引号

单引号与双引号类似,不同的是shell会忽略任何引用值,也就是说,如果屏蔽了其特殊含意,会将引号里德所有字符,包括引号都作为一个字符串。

[root@Gwan ~]# echo  'ert, $SHELL * china`echo wade`'

ert, $SHELL * china`echo wade`

2.2.4 反引号

用于设置系统命令的输出到变量。shell将反引号中的内容作为一个系统命令,并执行器内容。

[root@Gwan ~]# echo "*china`echo wade`" 

*chinawade

2.2.5 反斜杠

如果一个字符有特殊含意(& * + ^ $ ' " | ?),反斜杠防止shell误解其含义,即屏蔽其特殊含义。

[root@Gwan ~]# echo *

10201_database_linux32.zip anaconda-ks.cfg Desktop firefox-3.6.3.tar.bz2 gnome-libs-devel-1.4.1.2.90-44.1.i386.rpm GwanRPMS helloworld.sh hifile install.log install.log.syslog myfile parm parm.sh wade

You have new mail in /var/spool/mail/root

[root@Gwan ~]# echo \*

*


2.3

运算符

运算符是对计算机发的指令

运算对象

-数字、字符(字面值)

-变量

-表达式

表达式:运算符和运算对象的组合体

2.3.1 运算符类型

按位运算符

~,《,》,&, |, ^

$[]  表示形式告诉shell对方括号中的表达式求值。

[root@Gwan ~]# echo $[2+8]

10

[root@Gwan ~]# echo $[2>>8]

0

[root@Gwan ~]# echo $[2<<4]

32

[root@Gwan ~]# echo $[2^4]

6

[root@Gwan ~]# echo $[~3]

-4

 

2.3.1

逻辑运算符

[root@Gwan ~]# echo $[2&&2]

1

[root@Gwan ~]# echo $[2&&0]

0

[root@Gwan ~]# echo $[0&&0]

0

[root@Gwan ~]# echo $[1&&0]

0

[root@Gwan ~]# echo $[1||0]

1

[root@Gwan ~]# echo $[0||0]

0

[root@Gwan ~]#

赋值运算符

[root@Gwan ~]# var=65

[root@Gwan ~]# let var+=4

[root@Gwan ~]# echo $var

69

[root@Gwan ~]#

表达式替换

$[](())

习惯使用$[],所有shell的求值都是用整数完成,可以接受不同基数的数字

$[ base#n] n表示基数从236的任何基数。

[root@Gwan ~]# echo $[10#8 +1 ]

9

运算符的优先级

有张表,多查询

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24463783/viewspace-675577/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24463783/viewspace-675577/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
未来社区的建设背景和需求分析指出,随着智能经济、大数据、人工智能、物联网、区块链、云计算等技术的发展,社区服务正朝着数字化、智能化转型。社区服务渠道由分散向统一融合转变,服务内容由通用庞杂向个性化、服务导向转变。未来社区将构建数字化生态,实现数据在线、组织在线、服务在线、产品智能和决策智能,赋能企业创新,同时注重人才培养和科研平台建设。 规划设计方面,未来社区将基于居民需求,打造以服务为中心的社区管理模式。通过统一的服务平台和应用,实现服务内容的整合和优化,提供灵活多样的服务方式,如推送式、订阅式、热点式等。社区将构建数据与应用的良性循环,提高服务效率,同时注重生态优美、绿色低碳、社会和谐,以实现幸福民生和产业发展。 建设运营上,未来社区强调科学规划、以人为本,创新引领、重点突破,统筹推进、整体提升。通过实施院落+社团自治工程,转变政府职能,深化社区自治法制化、信息化,解决社区治理中的重点问题。目标是培养有活力的社会组织,提高社区居民参与度和满意度,实现社区治理服务的制度机制创新。 未来社区的数字化解决方案包括信息发布系统、服务系统和管理系统。信息发布系统涵盖公共服务类和社会化服务类信息,提供政策宣传、家政服务、健康医疗咨询等功能。服务系统功能需求包括办事指南、公共服务、社区工作参与互动等,旨在提高社区服务能力。管理系统功能需求则涉及院落管理、社团管理、社工队伍管理等,以实现社区治理的现代化。 最后,未来社区建设注重整合政府、社会组织、企业等多方资源,以提高社区服务的效率和质量。通过建立社区管理服务综合信息平台,提供社区公共服务、社区社会组织管理服务和社区便民服务,实现管理精简、高效、透明,服务快速、便捷。同时,通过培育和发展社区协会、社团等组织,激发社会化组织活力,为居民提供综合性的咨询和服务,促进社区的和谐发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值