環境和shell變數

2021-05-23 20:39:39 字數 4411 閱讀 4646

1.變數結合

[root@localhost ~]# error_msg="sorry this file doet not exist user $logname"

[root@localhost ~]# echo $

sorry this file doet not exist user root

[root@localhost ~]# echo $$

bruce willis

2.測試變數是否設定

1.[root@localhost ~]# echo "the sky is $ today"

the sky is blue today

[root@localhost ~]# unset colour

[root@localhost ~]# echo "the sky is $ today"

the sky is grey today

[root@localhost ~]#

2.root@localhost yjg]# ./vartest.txt

what time do you wish to start the payroll [03:00]:

4:21

process to start at 4:21 ok

it is a monthly or weekly run [weekly]:

sunrun type is sun

cannot open input file sun: no such file or directory

[root@localhost yjg]# ./vartest.txt

what time do you wish to start the payroll [03:00]:

process to start at 03:00 ok

it is a monthly or weekly run [weekly]:

run type is weekly

cannot open input file weekly: no such file or directory

3.[root@localhost yjg]# echo "the file is $"

-bash: file:  sorry cannot locate the variable files

[root@localhost yjg]# file="test"

[root@localhost yjg]# echo "the file is $"

the file is test

3.使用變數儲存系統引數

[root@localhost yjg]# source="/etc/passwd"

[root@localhost yjg]# dest="/usr/local/yjg/passwd.bak"

[root@localhost yjg]# cp $source $dest

4.設定唯讀變數

[root@localhost yjg]# tape_dev="/dev/rmt/0n"

[root@localhost yjg]# echo $

/dev/rmt/0n

[root@localhost yjg]# readonly tape_dev

[root@localhost yjg]# tape_dev="/dev/rmt/1n"

-bash: tape_dev: readonly variable

[root@localhost yjg]#

二、環境變數

1.[root@localhost yjg]# console=tty1

[root@localhost yjg]# export console

[root@localhost yjg]# echo $

tty1

2.env ------------顯示所有環境變數

3.unset console

4.嵌入shell變數

[root@localhost yjg]# exinit='set nu tab=10';export exinit

[root@localhost yjg]# vi kkk.txt

[root@localhost yjg]# home=/usr/local;export home

[root@localhost yjg]# pwd

/home/yjg

[root@localhost yjg]# cd

[root@localhost ~]# pwd

/usr/local

[root@localhost ~]#

[root@localhost ~]# export ifs=:

[root@localhost ~]# echo $path

/usr/lib/qt-3.3/bin /usr/kerberos/sbin /usr/kerberos/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /usr/x11r6/bin /root/bi

[root@localhost ~]# echo $

whoami

[root@localhost ~]#

[root@localhost ~]# editor=vi;export editor

[root@localhost ~]# echo $

vi三、set 命令

1、[yjg@localhost ~]$ vi .profile

set -a

mail=/usr/mail/$

path=$path:$home:bin

#editor=vi

term vt220

admin=/usr/adm

ps1="'hostname'>"

2、#!/bin/sh

#father script

echo "this is the father"

film="a few good men"

echo "i like the film:$film"##

# call the child script

child.txt

echo "back to father"

echo "and the film is :$film"

[yjg@localhost ~]$ vi child.txt

#!/bin/sh

#child.txt

echo "called from father .. i am the child"

echo "film name is :$film"

film="die hard"

echo "changing film to :$film"

3、#!/bin/sh

#father script

echo "this is the father"

film="a few good men"

echo "i like the film:$film"##

# call the child script

export film

child.txt

echo "back to father"

echo "and the film is :$film"

[yjg@localhost ~]$ vi child.txt

#!/bin/sh

#child.txt

echo "called from father .. i am the child"

echo "film name is :$film"

film="die hard"

echo "changing film to :$film"

四、位置變數引數和特定變數引數

$#----傳遞到指令碼的引數個數

$*-----以乙個單字串顯示所有向指令碼傳遞的個數,與位置變數不同,此選項引數可超過9個

$$-----指令碼執行的當前程序id號

$!------後台執行的最後乙個程序的程序id號

$@---- 與$#相同,但是使用時加引號,並在引號中返回每個引數

$-       顯示shell使用的當前選項,與set命令相同

$?-----顯示最後命令的退出狀態,0表示沒有錯誤,其它值表明有錯誤。

五、反引號

[root@localhost ~]# mydate=`date +%a" the "%e" of "%b" "%y`

[root@localhost ~]# echo $

[root@localhost ~]# expr 12 /* 12

144

linux 環境變數和shell變數

變數是任何一種程式語言都必不可少的組成部分,用於存放各種型別的變數。指令碼語言大多是弱型別語言 動態語言 也就是說在使用變數時,不需要事先宣告變數的型別,只需要直接賦值就可以。在bash中,每乙個變數的值都是字串。無論你給變數賦值時有沒有使用引號,值都會以字串的形式儲存。有一些特殊的變數會被shel...

shell 中內部變數和環境變數

內部變數主要是為shell程式設計提供支援 命令列引數或位置引數的數量 最近執行的一次命令或指令碼的出口狀態 shell指令碼的程序id 最近執行後台程序的pid 與 的區別 與 的區別 oldpwd 用cd到所建目錄之前的路徑,路徑必須是發生過改變才有值 optarg getopts 命令已經處理...

shell環境變數

環境變數 和 自定義變數 子程序僅會繼承父程序的環境變數,而不會繼承父程序的自定義變數,所以,你原本bash中的自定義變數在進入了子程序後就會消失不見,一直到你離開子程序並回到原本的父程序之後,這個變數才會出現。1.用env檢視預設的環境變數及其說明 export也可檢視,但是輸出的內容比env多 ...