shell指令碼中的變數介紹

2021-10-05 20:32:17 字數 4444 閱讀 4146

命令無法操作一直變換的目標

用一串固定的字元來表示不固定的目標可以解決此問題

[root@loo ~]# a=$(date)

[root@loo ~]# echo $a

sun may 3 00:30:47 edt 2020

1.環境級別

export a=1,環境關閉後變數失效

[root@loo ~]# vim file.sh

[root@loo ~]# sh file.sh

[root@loo ~]# . file.sh

sun may 3 00:30:47 edt 2020

[root@loo ~]# chmod +x file.sh

[root@loo ~]# export a=1

[root@loo ~]# sh file.sh

1

2.使用者級別

[root@loo ~]# su - westos 

[westos@loo ~]$ ls -a

. .bash_profile desktop .iceauthority music templates

.. .bashrc documents .lesshst pictures videos

.bash_history .cache downloads .local .pki .viminfo

.bash_logout .config .esd_auth .mozilla public

[westos@loo ~]$ vim .bash_profile

export a=1

[westos@loo ~]$source /etc/profile

3.系統級別

[root@loo ~]# vim /etc/profile

export a=2

[root@loo profile.d]# vim westos.sh 

[root@loo profile.d]# source westos.sh

[root@loo profile.d]# echo $a

3[root@loo profile.d]# cat westos.sh

##############################

#author: lee

#mail: [email protected]

#create_time: 2020/05/03

#version:

#info;

#############################

#!/bin/bash

##############################

export a=3

4.變數的名稱

」字元「 」—「 」數字「

不能用數字開頭

建議:變數名稱短時全用大寫,變數名稱長用_區分子類

westos

westos_linux

1.轉譯

\ #轉義單個字元

「」 #弱引用, 批量轉移個數字元,不能轉譯 \ ` $ !

『』 #強引用

[root@loo profile.d]# echo $5

[root@loo profile.d]# echo \$5

$5[root@loo profile.d]# echo '$5'

$5[root@loo profile.d]# echo "$5"

2.宣告

[root@loo profile.d]# echo $ab

[root@loo profile.d]# echo $a b

3 b[root@loo profile.d]# echo $b

3b

3.變數的陣列

[root@loo profile.d]# a='1 2 3 4 5'

[root@loo profile.d]# echo $a

1 2 3 4 5

[root@loo profile.d]# a=(1 2 3 4 5)

[root@loo profile.d]# echo $a

1[root@loo profile.d]# echo $

2[root@loo profile.d]# echo $

4[root@loo profile.d]# echo $

1 2 3 4 5

[root@loo profile.d]# echo $

1 2 3 4 5

[root@loo profile.d]# *=」1 2 3「 @=」1「 」2「 」3「

練習:

[root@loo ~]# ls -l loo

total 0

-rw-r--r-- 1 root root 0 may 3 01:12 file

[root@loo ~]# c=(`ls -l loo`)

[root@loo ~]# echo $

-rw-r--r--

[root@loo ~]# echo $

1

alias xie='vim'		##臨時設定

vim ~/.bashrc ##只針對使用者生效

alias xie='vim'

vim /etc/bashrc ##針對系統所有的使用者生效

alias xie='vim'

unalias xie ##刪除當前環境中的alias

練習指令碼:

create_user.sh userfile passfile

userfile用的使用者會被全部建立 passfile中的密碼會被全部設定

環境變數: 使用者在作業系統時使用到的命令搜尋路徑

[westos@loo ~]$ echo $path

/home/westos/.local/bin:/home/westos/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

設定方式:

使用者級

vim ~/.bash_profile 

export path=$path:/***/***

所有使用者

vim /etc/profile 

export path=$path:/***/***

直接利用命令執行結果

$ () 與`` ##優先執行

test=hostnametest=$(hostname)

指令碼中的傳參

2.1 非互動模式:

sh /mnt/test.sh westos linux redhat

$0 is /mnt/test.sh ##指令碼本身

$1 is westos ##指令碼後所輸入的第一串字元

$2 is linux

$3 is redhat

$* is westos linux redhat ##指令碼後所輸入的所有字元"westos linux redhat"

$@ is westos linux redhat ##指令碼後所輸入的所有字元'westos' 'linux' 'redhat'

$# is 3 ##指令碼後所輸入的字串個數

2.2 互動模式傳參:

read  westos ##對westos賦值

read -p "please input word:" ##輸出提示語

-s ##隱藏輸入內容

[root@loo ~]# read westos

hello

[root@loo ~]# echo $westos

hello

定義: 程式的別名

設定方式:

funcname() 

funcn

ame在指令碼中就代表action1 action2這兩個動

shell指令碼中的變數

1 在命令列中和指令碼中,變數定義得格式 name value 左右兩邊不能有空格,否則會當做命令來對待,輸出乙個command not found echo name echo 列印出變數,引用變數使用 name.2 單引號和雙引號 語法 和php中相同 雙引號仍然可以保有變數的內容,但單引號內僅...

shell指令碼中的變數

變數就是記憶體一片區域的位址 命令無法操作一直變化的目標 用一串固定的字元來表示不固定的目標可以解決此問題 export宣告 env顯示所有環境變數 echo path export a 1 在環境關閉後變數失效 字元 數字 轉譯單個字元 弱引用,批量轉譯個數字元 不能轉譯 強引用 表示當前程序的i...

shell指令碼中的變數

後記面試中很重要 在系統中,當我們執行 bin ls時與ls命令結果並不相同,因為ls是已經經過加工過的命令。alias xie vim 臨時設定,開啟了新的shell就會失效 vim bashrc 只對當前使用者生效 alias xie vim source bashrc vim etc bash...