一、變量命名 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
????1、只能包含字母、數(shù)字和下劃線,并且不能以數(shù)字開頭,
????2、不應(yīng)該跟系統(tǒng)中已有的環(huán)境變量重名
????3、命名要有含義
二、變量賦值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
變量名稱=值var_name=value?等號(hào)兩邊不能有空格
[root@hao ~]#
NAME=
jerry
[root@hao
~]#
echo
$NAME
jerry
三、bash:變量類型 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????????????????????????????
1、環(huán)境變量
腳本在執(zhí)行時(shí)會(huì)啟動(dòng)一個(gè)子shell進(jìn)程。命令行中啟動(dòng)的腳本會(huì)繼承當(dāng)前shell環(huán)境變量,也就是說環(huán)境變量的作用域?yàn)楫?dāng)前shell進(jìn)程及其子shell進(jìn)程。系統(tǒng)自動(dòng)執(zhí)行的腳本(非命令行啟動(dòng))則需要自我定義各環(huán)境變量。可以使用export設(shè)置環(huán)境變量。
export VARNAME=VALUE 或者 VARNAME=VALUE export VARNAME
?
[root@hao ~]#
NAME=
jerry
[root@hao
~
]#
export NAME
[root@hao
~]#
echo
$NAME
jerry
[root@hao
~
]#
bash
[root@hao
~]#
echo
$NAME
jerry
[root@hao
~
]#
bash
[root@hao
~]#
echo
$NAME
jerry
[root@hao ~]#
pstree
init-+-NetworkManager-+-dhclient
??? ...
???? |-sshd---sshd---sshd---bash---
su---bash---bash---bash---pstree
???? |-udevd---2*[udevd]
???? `-wpa_supplican
2、本地變量(局部變量)
變量一定是進(jìn)程的變量,因此在一個(gè)shell中聲明的變量在其他的shell中不可用,并且在其子shell中也不可以得到。引用變量要加${varname},也可以不加{}。
[root@hao ~]#
NAME=
jerry
[root@hao
~]#
echo
$NAME
jerry
[root@hao
~
]#
bash
[root@hao
~]#
echo
$NAME
[root@hao
~
]#
exit
exit
[root@hao
~]#
echo
$NAME
jerry
本地變量:VARNAME=VALUE,作用域:整個(gè)bash進(jìn)程。
局部變量 :local VARNAME=VALUE,作用域:當(dāng)前代碼段。
3、位置變量
$n
例:寫一個(gè)腳本,能接受一個(gè)參數(shù)(文件路徑),判定此參數(shù)是否是一個(gè)存在的文件,如果是就顯示“ok”
[root@hao ~]#
nano filetest.
sh
[root@hao ~]#
cat
filetest.
sh
#
! /bin/
bash
#
if
[ -e $
1
];
then
echo
"
ok
"
else
echo
"
no such file
"
fi
[root@hao ~]#
chmod
+x filetest.
sh
[root@hao ~]#
./filetest.
sh
/etc/
fstab
ok
[root@hao
~]#
./filetest.
sh
/etc/
fstabs
no such
file
shift, 每次shift都會(huì)剔除一個(gè)參數(shù) shift n表示剔除n 個(gè)
[root@hao ~]#
nano filetest1.
sh
[root@hao ~]#
cat
filetest1.
sh
#
!/bin/
bash
echo
$
1
shift
echo
$
1
shift
echo
$
1
[root@hao
~]#
chmod
+x filetest1.
sh
[root@hao ~]#
./filetest1.
sh
1
2
3
1
2
3
4、特殊變量(系統(tǒng)變量)
用來保存某些特殊數(shù)據(jù):
$?: 保存上一個(gè)命令的執(zhí)行狀態(tài)返回值(0-255)。0為正確,其他為錯(cuò)誤。其中1,2,127系統(tǒng)預(yù)留,其他的可以自定義。
[root@hao ~]#
ls
/
var
account crash db games lib lock mail opt run tmp yp
cache cvs empty gdm local log nis preserve spool www
[root@hao
~]#
echo
$?
0
[root@hao ~]#
ls
/
varr
ls
: cannot access /varr: No such
file
or directory
[root@hao
~]#
echo
$?
2
如果只想看返回的狀態(tài)值而不想看輸出結(jié)果,則只需將輸出重定向至數(shù)據(jù)黑洞/dev/null
[root@hao ~]#
ls
/varr &> /dev/
null
[root@hao ~]#
echo
$?
2
$#:參數(shù)的個(gè)數(shù)
$*:參數(shù)列表
$@:參數(shù)列表(與上面的有不同)
例:寫一個(gè)腳本,能接受一個(gè)參數(shù)(文件路徑),判定此參數(shù)是否是一個(gè)存在的文件,如果是就顯示“ok”
[root@hao ~]#
nano filetest.
sh
[root@hao
~]#
cat
filetest.
sh
#
! /bin/
bash
#
echo
$#
echo
$*
echo
$@
if
[ $# -lt
1
];
then
echo
"
Usage:./filetest.sh ARG1 ARG2 ...
"
exit
7
fi
if
[ -e $
1
];
then
echo
"
ok
"
else
echo
"
no such file
"
fi
[root@hao ~]#
chmod
+x filetest.
sh
[root@hao ~]#
./filetest.
sh
0
Usage:.
/filetest.
sh
ARG1 ARG2 ...
[root@hao ~]#
./filetest.sh? /etc/fstab /etc/fstabs
2
/etc/fstab /etc/fstabs
/etc/fstab /etc/fstabs
ok
四、撤銷變量 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
[root@hao ~
]#
unset NAME
[root@hao
~]#
echo
$NAME
五、查看當(dāng)前shell變量 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
查看當(dāng)前shell所有變量,包含環(huán)境變量和本地變量
[root@hao ~
]#
set
BASH
=/bin/
bash
BASHOPTS
=
checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES
=
()
...
查看當(dāng)前shell環(huán)境變量 printenv env 或者export
[root@hao ~
]#
printenv
HOSTNAME
=
hao
SHELL
=/bin/
bash
TERM
=
xterm
...
六、追加shell變量 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
[root@hao ~]#
ANIMAL=
pig
[root@hao
~]#
ANIMAL=
$ANIMAL:goat
[root@hao
~]#
echo
$ANIMAL
pig:goat
[root@hao
~]#
echo
$PATH
/usr/lib64/qt-
3.3
/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/
bin
[root@hao
~]#
export PATH=$PATH:/usr/local/apache/
bin
[root@hao
~]#
echo
$PATH
/usr/lib64/qt-
3.3
/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/
bin
[root@hao
~]#
export PATH=/usr/local/mysql/
bin:$PATH
[root@hao
~]#
echo
$PATH
/usr/local/mysql/bin:/usr/lib64/qt-
3.3
/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin
七、第一個(gè)腳本程序 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
首先創(chuàng)建腳本程序first.sh,腳本程序的第一行要指定程序的魔數(shù),#!加上解釋器的路徑。然后賦予其執(zhí)行權(quán)限,之后執(zhí)行,執(zhí)行時(shí)候要加上路徑,因?yàn)樵跊]有給路徑時(shí),默認(rèn)去path中去找該命令是否存在。
[root@hao ~]#
cat
first.
sh
#
!/bin/
bash
ls
/
var
cat
/etc/
inittab
[root@hao
~]#
chmod
+x first.
sh
[root@hao ~]#
./first.
sh
account crash db games lib lock mail opt run tmp yp
cache cvs empty gdm local log nis preserve spool www
# inittab is only used by upstart
for
the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by
/etc/init/
rcS.conf
#
# Individual runlevels are started by
/etc/init/
rc.conf
#
# Ctrl
-Alt-Delete is handled by /etc/init/control-alt-
delete.conf
#
# Terminal gettys are handled by
/etc/init/tty.conf and /etc/init/
serial.conf,
# with configuration
in
/etc/sysconfig/
init.
#
# For information on how to
write
upstart event handlers, or how
# upstart works, see init(
5
), init(
8
), and initctl(
8
).
#
# Default runlevel. The runlevels used are:
#
0
-
halt (Do NOT set initdefault to this)
#
1
-
Single user mode
#
2
- Multiuser, without NFS (The same as
3
,
if
you
do
not have networking)
#
3
-
Full multiuser mode
#
4
-
unused
#
5
-
X11
#
6
-
reboot (Do NOT set initdefault to this)
#
id
:
5
:initdefault:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

