建设三轮摩托车官方网站,网站开发设计语言,如何网页截图快捷键,滨州seo排名0 前言
在
Linux shell编程学习笔记16#xff1a;bash中的关联数组https://blog.csdn.net/Purpleendurer/article/details/134053506?spm1001.2014.3001.5501
中#xff0c;我们在定义关联数组时使用了declare命令。
其实#xff0c;declare命令的功能不只是定义定义关…0 前言
在
Linux shell编程学习笔记16bash中的关联数组https://blog.csdn.net/Purpleendurer/article/details/134053506?spm1001.2014.3001.5501
中我们在定义关联数组时使用了declare命令。
其实declare命令的功能不只是定义定义关联数组现在我们就来探讨它的功能和用法。
1 declare 命令的功能和格式
我们可以使用 命令 help declare 查看 的功能和格式 purpleEndurer bash $ help declare declare: declare [-aAfFgilrtux] [-p] [name[value] ...] Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are given, display the attributes and values of all variables. Options: -f restrict action or display to function names and definitions -F restrict display to function names only (plus line number and source file when debugging) -g create global variables when used in a shell function; otherwise ignored -p display the attributes and value of each NAME Options which set attributes: -a to make NAMEs indexed arrays (if supported) -A to make NAMEs associative arrays (if supported) -i to make NAMEs have the integer attribute -l to convert NAMEs to lower case on assignment -r to make NAMEs readonly -t to make NAMEs have the trace attribute -u to convert NAMEs to upper case on assignment -x to make NAMEs export Using instead of - turns off the given attribute. Variables with the integer attribute have arithmetic evaluation (see the let command) performed when the variable is assigned a value. When used in a function, declare makes NAMEs local, as with the local command. The -g option suppresses this behavior. Exit Status: Returns success unless an invalid option is supplied or an error occurs. 1.1 功能
declare命令的功能是 查看 或 设置变量值和属性查看自定义函数的名称和定义。
1.2 格式 declare [-aAfFgilrtux] [-p] [变量名[值] ...] 选项说明
选项功能描述备注-a使 指定变量名 成为索引数组如果支持属性选项indexed arrays-A使 指定变量名 成为关联数组如果支持属性选项ssociative arrays-f将操作或显示限制为函数名称和定义function-F将显示限制为仅函数名称加上行号和调试时的源文件function-g在 shell 函数中使用时创建全局变量否则忽视global-i使 指定变量名 具有“整数”属性属性选项integer-l在赋值时将 指定变量名 转换为小写属性选项lower case-p显示每个 指定变量名 的属性和值display-r 使 指定变量名 只读属性选项readonly-t使 指定变量名 具有“trace”属性属性选项trace-u在赋值时将 指定变量名转换为大写属性选项upper case-x使 指定变量名导出属性选项export
对于属性选项-可用来指定变量的属性则是取消变量所设的属性。
注意如果同时对多个变量进行操作变量名之间用空格分隔 2 用法和实例
2.1 declare 显示用户的shell变量 局部变量和环境变量和自定义函数 purpleEndurer bash $ declare | more BASH/usr/bin/bash BASHOPTScheckwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath BASH_ALIASES() BASH_ARGC() BASH_ARGV() BASH_CMDS() BASH_LINENO() BASH_SOURCE() BASH_VERSINFO([0]4 [1]2 [2]46 [3]2 [4]release [5]x86_64-redhat-linux-gnu) BASH_VERSION4.2.46(2)-release COLUMNS144 DIRSTACK() EUID1000 GROUPS() HISTFILE/home/csdn/.bash_history HISTFILESIZE500 HISTSIZE500 HOME/home/csdn HOSTNAMEedu HOSTTYPEx86_64 IFS$ \t\n LC_ALLzh_CN.UTF-8 LESS-R LINES32 LOGNAMEcsdn LSCOLORSGxfxcxdxbxegedabagacad LS_COLORSrs0:di01;34:ln01;36:mh00:pi40;33:so01;35:do01;35:bd40;33;01:cd40;33;01:or40;31;01:mi01;05;37;41:su37;41:sg30;43:ca30;41 :tw30;42:ow34;42:st37;44:ex01;32:*.tar01;31:*.tgz01;31:*.arc01;31:*.arj01;31:*.taz01;31:*.lha01;31:*.lz401;31:*.lzh01;31:*.lzma01;3 1:*.tlz01;31:*.txz01;31:*.tzo01;31:*.t7z01;31:*.zip01;31:*.z01;31:*.Z01;31:*.dz01;31:*.gz01;31:*.lrz01;31:*.lz01;31:*.lzo01;31:*.xz 01;31:*.bz201;31:*.bz01;31:*.tbz01;31:*.tbz201;31:*.tz01;31:*.deb01;31:*.rpm01;31:*.jar01;31:*.war01;31:*.ear01;31:*.sar01;31:*.rar0 1;31:*.alz01;31:*.ace01;31:*.zoo01;31:*.cpio01;31:*.7z01;31:*.rz01;31:*.cab01;31:*.jpg01;35:*.jpeg01;35:*.gif01;35:*.bmp01;35:*.pbm0 --More-- LS_COLORSrs0:di01;34:ln01;36:mh00:pi40;33:so01;35:do01;35:bd40;33;01:cd40;33;01:or40;31;01:mi01;05;37;41:su37;41:sg30;43:ca30;41 :tw30;42:ow34;42:st37;44:ex01;32:*.tar01;31:*.tgz01;31:*.arc01;31:*.arj01;31:*.taz01;31:*.lha01;31:*.lz401;31:*.lzh01;31:*.lzma01;3 1:*.tlz01;31:*.txz01;31:*.tzo01;31:*.t7z01;31:*.zip01;31:*.z01;31:*.Z01;31:*.dz01;31:*.gz01;31:*.lrz01;31:*.lz01;31:*.lzo01;31:*.xz 01;31:*.bz201;31:*.bz01;31:*.tbz01;31:*.tbz201;31:*.tz01;31:*.deb01;31:*.rpm01;31:*.jar01;31:*.war01;31:*.ear01;31:*.sar01;31:*.rar0 1;31:*.alz01;31:*.ace01;31:*.zoo01;31:*.cpio01;31:*.7z01;31:*.rz01;31:*.cab01;31:*.jpg01;35:*.jpeg01;35:*.gif01;35:*.bmp01;35:*.pbm0 1;35:*.pgm01;35:*.ppm01;35:*.tga01;35:*.xbm01;35:*.xpm01;35:*.tif01;35:*.tiff01;35:*.png01;35:*.svg01;35:*.svgz01;35:*.mng01;35:*.pcx01;35:*.mov01;35:*. mpg01;35:*.mpeg01;35:*.m2v01;35:*.mkv01;35:*.webm01;35:*.ogm01;35:*.mp401;35:*.m4v01;35:*.mp4v01;35:*.vob01;35:*.qt01;35:*.nuv01;35:*.wmv01;35:*.asf01; 35:*.rm01;35:*.rmvb01;35:*.flc01;35:*.avi01;35:*.fli01;35:*.flv01;35:*.gl01;35:*.dl01;35:*.xcf01;35:*.xwd01;35:*.yuv01;35:*.cgm01;35:*.emf01;35:*.axv01 ;35:*.anx01;35:*.ogv01;35:*.ogx01;35:*.aac01;36:*.au01;36:*.flac01;36:*.mid01;36:*.midi01;36:*.mka01;36:*.mp301;36:*.mpc01;36:*.ogg01;36:*.ra01;36:*.wav 01;36:*.axa01;36:*.oga01;36:*.spx01;36:*.xspf01;36: MACHTYPEx86_64-redhat-linux-gnu MAILCHECK60 OPTERR1 OPTIND1 OSTYPElinux-gnu PAGERless PATH/home/csdn/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PIPESTATUS([0]0) PPID50 PROMPT_COMMANDprintf \033]0;%s%s:%s\007 ${USER} ${HOSTNAME%%.*} ${PWD/#$HOME/~} PS1purpleEndurer bash $ PS2 PS4 PWD/home/csdn SHELL/bin/bash SHELLOPTSbraceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor SHLVL2 TERMxterm UID1000 ZSH/home/csdn/.oh-my-zsh _declare colors/home/csdn/.dircolors purpleEndurer bash $ 由于当前用户没有自定义函数所以目前只能看到变量的信息。 2.2 declare -i 变量名1[值1] ……声明整数型变量
例声明整数型变量a和b并初始化其值分别为1和2然后显示a和b的值再显示a与b的和 purpleEndurer bash $ declare -i a1 b2 purpleEndurer bash $ echo $a,$b 1,2 purpleEndurer bash $ echo $[ a b ] 3 2.3 declare 属性 变量名取消变量属性
例声明整数型变量a和b并初始化其值分别为1和2然后给a赋值a。 purpleEndurer bash $ declare -i a1 b1 #声明整数型变量a和b并初始化其值分别为1和2 purpleEndurer bash $ aa bb #给变量a赋值a变量b赋值b purpleEndurer bash $ echo $a $b #显示a和b的值 1 1 purpleEndurer bash $ declare i a #取消变量a的整数属性 purpleEndurer bash $ aa bb purpleEndurer bash $ echo $a $b #显示a和b的值 a 1 purpleEndurer bash $ 可以看到在 声明整数型变量a和b并初始化其值分别为1和2后直接给整型变量a和b赋字符串值是无效的。
需要取消变量a的整数属性后才能给变量a赋字符串值a。 2.4 declare -r 变量名[值]……定义只读变量
定义只读整数变量c并初始化值为1 purpleEndurer bash ~ $ declare -ri c1 purpleEndurer bash ~ $ echo $c 1 purpleEndurer bash ~ $ c2 bash: c: readonly variable purpleEndurer bash ~ $ declare r c bash: declare: c: readonly variable 只读变量的值无法更改。
无法用declare r 来去除变量c的只读属性。 2.5 [declare -a ]索引数组名[(值1 值2 ……) ]定义索引数组
例定义索引数组a并初始化然后显示其值 purpleEndurer bash ~ $ declare -a a(1 a b) purpleEndurer bash ~ $ echo ${a[*]} 1 a b 关于索引数组的更多知识可以回顾 Linux shell编程学习笔记15定义数组、获取数组元素值和长度、数组拼接或合并https://blog.csdn.net/Purpleendurer/article/details/134009982?spm1001.2014.3001.5501 2.6 declare -A 关联数组名[[字符串下标1]值1 [字符串下标2]值2 …… [字符串下标n]值n]定义关联数组
例定义关联数组b并初始化然后显示其值 purpleEndurer bash ~ $ declare -A b([apple]1 [boy]2 [cat]3) purpleEndurer bash ~ $ echo ${b[*]} 3 1 2 关于关联数组的更多知识可以回顾
Linux shell编程学习笔记16bash中的关联数组https://blog.csdn.net/Purpleendurer/article/details/134053506?spm1001.2014.3001.5501
2.7 declare -f[ 函数名]显示指定的用户自定义函数名称和定义
例定义函数a和b然后用 declare -f命令查看它们的名称和定义代码 purpleEndurer bash ~ $ function a(){ echo $0;} purpleEndurer bash ~ $ a bash purpleEndurer bash ~ $ function b(){ echo $#;} purpleEndurer bash ~ $ b 0 purpleEndurer bash ~ $ declare -f a () { echo $0 } b () { echo $# } purpleEndurer bash ~ $ declare -f a a () { echo $0 } purpleEndurer bash ~ $ declare -f b b () { echo $# } purpleEndurer bash ~ $ 如果在使用declare -f 命令时没有指定函数名将显示所有的函数名和定义代码。
2.8 declare -F[ 函数名]显示指定的用户自定义函数名称
例我们用 declare -F命令查看2.7中例子自定义函数a和b的名称 purpleEndurer bash ~ $ declare -F declare -f a declare -f b purpleEndurer bash ~ $ declare -F a a purpleEndurer bash ~ $ declare -F b b purpleEndurer bash ~ $ 与 declare -f 命令相似如果在使用declare -F 命令时没有指定函数名将显示所有的函数名。
3 declare命令与set命令在显示shell变量功能上的对比
set 显示用户的局部变量和用户环境变量。
declare 跟set一样显示用户的shell变量 局部变量和环境变量