海外主机测评

您现在的位置是:首页 > 云服务器知识 > 正文

云服务器知识

SHELLDECLARE的简单介绍

cds8202023-03-22云服务器知识95
本文目录一览:1、shell脚本某条语句中为什么要declare2、linuxshell关联数组的一个小问题3、Linuxshell脚本编程——本地语言设置为中文4、s

本文目录一览:

  • 1、shell脚本某条语句中为什么要declare
  • 2、linux shell 关联数组的一个小问题
  • 3、Linux shell脚本编程——本地语言设置为中文
  • 4、shell脚本中,无法获取“关联数组”(字典)的key,尝试过很多方法,输出都不对,求教

shell脚本某条语句中为什么要declare

declare用来定义后面的变量,比如变量A,变量B,变量sum_xx,如果没有declare,A B sum_xx就会当成一条命令语句,当然会找不到。

linux shell 关联数组的一个小问题

shell 并不支持关联数组。

只有awk才支持关联数组。

shell只支持index数字类型的数组,凡是不是数字的,都会当做0,或者-1,也就是最后的那个元素。

详细参见bash的man手册。

Arrays

Bash provides one-dimensiONal array variables. Any variable may be used as an array; the declare builtin

will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement

that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.

An array is created automatically if any variable is assigned to using the syntax name[subscript]=value.

The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal

to zero. To explicitly declare an array, use declare -a name (see SHELL BUILTIN COMMANDS below).

declare -a name[subscript] is also accepted; the subscript is ignored. Attributes may be specified for

an array variable using the declare and readonly builtins. Each attribute applies to all members of an

array.

Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value

is of the form [subscript]=string. Only string is required. If the optional brackets and subscript are

supplied, that index is assigned to; otherwise the index of the element assigned is the last index

assigned to by the statement plus one. Indexing starts at zero. This syntax is also accepted by the

declare builtin. Individual array elements may be assigned to using the name[subscript]=value syntax

introduced above.

Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid

conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name.

These subscripts differ only when the word appears within double quotes. If the word is double-quoted,

${name[*]} expands to a single word with the value of each array member separated by the first character

of the IFS special variable, and ${name[@]} expands each element of name to a separate word. When there

are no array members, ${name[@]} expands to nothing. If the double-quoted expansion occurs within a

word, the expansion of the first parameter is joined with the beginning part of the original word, and

the expansion of the last parameter is joined with the last part of the original word. This is analogous

to the expansion of the special parameters * and @ (see Special Parameters above). ${#name[subscript]}

expands to the length of ${name[subscript]}. If subscript is * or @, the expansion is the number of ele-

ments in the array. Referencing an array variable without a subscript is equivalent to referencing ele-

ment zero.

The unset builtin is used to destroy arrays. unset name[subscript] destroys the array element at index

subscript. Care must be taken to avoid unwanted side effects caused by filename generation. unset name,

where name is an array, or unset name[subscript], where subscript is * or @, removes the entire array.

The declare, local, and readonly builtins each accept a -a option to specify an array. The read builtin

accepts a -a option to assign a list of words read from the standard input to an array. The set and

declare builtins display array values in a way that allows them to be reused as assignments.

Linux shell脚本编程——本地语言设置为中文

#!/bin/bash

################################变量定义################################

defaultLanguage="zh_CN.utf8"

declare -A languagePacks

languagePacks=( ["zh_CN.utf8"]="kde-l10n-Chinese")

##语言包列表

languagePack=${languagePacks[$defaultLanguage]}

################################函数模块################################

##查看系统是否存在指定语言包

function FindLocalLanguagePackages(){

  locale -a|grep $defaultLanguage echo "find $defaultLanguage" return 0 || return 1

}

##安装指定语言包

function InstallsLanguagePack(){

  yum install $languagePack return 0 || return 1

}

##临时设置系统默认语言包

function TemporarilySetDefaultLanguagePack(){

  LANG=$defaultLanguage echo "Temporarily Set Default Language Pack to $defaultLanguage" return 0 || return 1

}

##永久设置系统默认语言包

function SetDefaultLanguagePackage(){

  localectl set-locale LANG=$defaultLanguage echo "Set Default Language Pack to $defaultLanguage" return 0 || return 1

}

################################程序流程################################

##查看系统当前语言包

[ $LANG == $defaultLanguage ] echo $LANG || FindLocalLanguagePackages

##安装指定语言包 或 临时设置系统默认语言包

[ $? == 0 ] TemporarilySetDefaultLanguagePack || InstallsLanguagePack

##永久设置系统默认语言包

[ $? == 0 ] SetDefaultLanguagePackage

##查看结果

[ $LANG == $defaultLanguage ] echo "/etc/locale.conf is `cat /etc/locale.conf`" echo "Default Language Pack to $defaultLanguage" || echo "Set Language error,Default Language Package $defaultLanguage"

##消除变量

unset defaultLanguage

unset languagePacks

unset languagePack

shell脚本中,无法获取“关联数组”(字典)的key,尝试过很多方法,输出都不对,求教

看下你使用的bash版本到没到4.0,使用指令bash --version查看

使用变量前先确认该变量是否在之前已经被定义过了 使用source 命令或  . 命令启动脚本时是不会开辟新线程的所以你在bash窗口里定义的没加local 变量 都会被脚本继承 此时你可以先删除这个变量 即定义phone 时 unset phone

SHELLDECLARE的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、SHELLDECLARE的信息别忘了在本站进行查找喔。

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~