日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux——shell 中的变量

發布時間:2025/3/19 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux——shell 中的变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

變量的定義

變量,即在程序運行過程中它的值是允許改變的量
變量是用一串固定的字符來標示不固定的值的一種方法
變量是一種使用方便的占位符,用于引用計算機內存地址,該地址可以存儲 Script 運行時可更改的程序信息 在 shell
中變量是不能永久保存在系統中的,必須在文件中聲明

shell 腳本中變量的定義方法

  • 環境級變量
    export A=1

作用域為當前shell進程及其子進程,不能影響到其父進程; export varname=value
“導出”,如果變量已經定義可以只是用變量名 export varname,即
1. export varname=value
2. varname=value export varname 腳本在執行命令時會啟動一個子shell環境變量: 系統自動執行的腳本(非命令行啟動)就需要自我定義需要的個環境變量;

  • 用戶級變量
    vim ~/.bash_profile
    export A=1
    source ~/.bash_profile 重新加載

  • 系統級變量
    vim /etc/profile
    export A=1
    source /etc/profile 重新加載

變量命名規范

  • 只能含字母、數字和下劃線,并且以字母和下劃線開頭

  • 最好不要跟系統已有的環境變量重名

  • 見名知意

字符的轉譯及變量的聲明

\ 轉譯單個字符 "" 弱引用,批量轉譯""中出現的字符 '' 強引用,批量轉譯''中出現的字符 ''"" 兩者的區別在于,""不能轉譯"\","`","!","$" ${} 變量聲明 例如:A=1echo $Abecho ${A}b

變量值傳遞

  • $1 腳本后的第一個字符

  • $2 腳本后的第二個字符

  • $3 腳本后的第三個字符

  • $# 腳本后所跟字符串的個數

  • $* 腳本后跟的所有字符串,模式為 “ 1 2 3 ”(一串字符)

  • $@ 腳本后跟的所有字符串,模式為 “ 1 ” “ 2 ” “ 3 ”(三串字符)

[root@desktop27 mnt]# vim test.sh [root@desktop27 mnt]# cat test.sh #!/bin/bash echo \$0 is $0 echo \$1 is $1 echo \$2 is $2 echo \$3 is $3 echo \$# is $# echo \$* is $* echo \$@ is $@ [root@desktop27 mnt]# sh test.sh $0 is test.sh $1 is $2 is $3 is $# is 0 $* is $@ is [root@desktop27 mnt]# sh test.sh westos $0 is test.sh $1 is westos $2 is $3 is $# is 1 $* is westos $@ is westos [root@desktop27 mnt]# sh test.sh westos linux $0 is test.sh $1 is westos $2 is linux $3 is $# is 2 $* is westos linux $@ is westos linux [root@desktop27 mnt]# sh test.sh westos linux redhat $0 is test.sh $1 is westos $2 is linux $3 is redhat $# is 3 $* is westos linux redhat $@ is westos linux redhat

$* 腳本后跟的所有字符串,模式為 “ 1 2 3 ”(一串字符)
$@ 腳本后跟的所有字符串,模式為 “ 1 ” “ 2 ” “ 3 ”(三串字符)
$*、$@之間的比較

[root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for name in "$*" doecho $name done [root@desktop27 mnt]# sh -x for.sh westos linux redhat + for name in '"$*"' + echo westos linux redhat westos linux redhat [root@desktop27 mnt]# vim for.sh [root@desktop27 mnt]# cat for.sh #!/bin/bash for name in "$@" doecho $name done [root@desktop27 mnt]# sh -x for.sh westos linux redhat + for name in '"$@"' + echo westos westos + for name in '"$@"' + echo linux linux + for name in '"$@"' + echo redhat redhat [root@desktop27 mnt]#

用 read 實現變量傳遞(實現交互)

read tutu
read -s tutu加密交互
read -p "input: " tutu顯示提示語言

[root@desktop27 mnt]# vim test.sh [root@desktop27 mnt]# cat test.sh #!/bin/bash read -p "Please inout a number: " IP ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down [root@desktop27 mnt]# sh test.sh Please inout a number: 172.25.254.50 172.25.254.50 is up [root@desktop27 mnt]# sh test.sh Please inout a number: 172.25.254.227 172.25.254.227 is down [root@desktop27 mnt]#

linux系統中命令別名的設定——alias

  • 臨時設定

[root@desktop27 ~]# alias xie='vim' ##臨時設定,重新開啟shell后,就會失效 [root@desktop27 ~]# xie ##可以進入vim編輯模式 [root@desktop27 ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' alias xie='vim' [root@desktop27 ~]# logout ##退出當前shell Connection to 172.25.254.127 closed. [kiosk@desktop50 Desktop]$ ssh root@172.25.254.127 ##重新開啟新shell root@172.25.254.127's password: Last login: Sun Jun 17 21:25:00 2018 from foundation35.ilt.example.com [root@desktop27 ~]# alias ##vim 的別名 xie 已經不存在 alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@desktop27 ~]# xie bash: xie: command not found... [root@desktop27 ~]#
  • 永久設定

[root@desktop27 ~]# vim .bashrc ##超級用戶家目錄下的文件,只對超級用戶生效 [root@desktop27 ~]# cat .bashrc | head -n 8 | tail -n 1 alias xie='vim' [root@desktop27 ~]# source .bashrc [root@desktop27 ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' alias xie='vim' [root@desktop27 ~]# su - student ##切換用戶student Last login: Thu May 11 20:23:54 EDT 2017 on pts/0 [student@desktop27 ~]$ xie ##超級用戶下的文件無法對其他用戶生效 bash: xie: command not found... [student@desktop27 ~]$ logout [root@desktop27 ~]# vim /etc/bashrc ##系統永久設定,對所有用戶生效 [root@desktop27 ~]# cat /etc/bashrc | tail -n 1 alias xie='vim' [root@desktop27 ~]# source /etc/bashrc [root@desktop27 ~]# su - student Last login: Sun Jun 17 22:42:08 EDT 2018 on pts/1 [student@desktop27 ~]$ xie [student@desktop27 ~]$ [student@desktop27 ~]$ logout
  • 命令別名的刪除

[root@localhost ~]# unalias xie [root@localhost ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@localhost ~]# vim /etc/bashrc ##刪除所添加的內容 [root@localhost ~]# vim .bashrc ##刪除所添加的內容 [root@localhost ~]# source /etc/bashrc [root@localhost ~]# source .bashrc [root@localhost ~]# xie bash: xie: command not found... [root@localhost ~]#

exit退出值

用途說明
exit命令用于退出當前shell,在shell腳本中可以終止當前腳本執行。
常用參數
格式:exit n
退出。設置退出碼為n。(Cause the shell to exit with a status of n.)
格式:exit
退出。退出碼不變,即為最后一個命令的退出碼。(If n is omitted, the exit status is that > of the last command executed. )
格式:$?
上一個命令的退出碼。
格式:trap “commands” EXIT
退出時執行commands指定的命令。( A trap on EXIT is executed before the shell terminates.)
退出碼(exit status,或exit code)的約定:
0表示成功(Zero - Success)
非0表示失敗(Non-Zero - Failure)
2表示用法不當(Incorrect Usage)
127表示命令沒有找到(Command Not Found)
126表示不是可執行的(Not an executable)
128<= 信號產生

[root@localhost mnt]# vim ip_check.sh [root@localhost mnt]# cat ip_check.sh #!/bin/bash read -p "Please input IP:" IP ping -c1 -w1 $IP &> /dev/null [ "$?" -eq "0" ]&&{ echo -e "\033[34m$IP is up\033[0m" }||{ echo -e "\033[31m$IP is down\033[0m" } [root@localhost mnt]# sh ip_check.sh Please input IP:172.25.254.127 172.25.254.127 is up [root@localhost mnt]# sh ip_check.sh Please input IP:172.25.20.229 172.25.20.229 is down [root@localhost mnt]#

exit 退出腳本
break 結束并退出循環
continue 在循環中不執行continue下面的代碼,轉而進入下一輪循環

總結

以上是生活随笔為你收集整理的linux——shell 中的变量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。