显示标签为“变量”的博文。显示所有博文
显示标签为“变量”的博文。显示所有博文

2011年12月12日星期一

Ubuntu 环境变量的设置


持久性的环境变量



到目前为止,我们只讨论临时设置一个环境变量的方式,当设置它时所在的shell会话被关闭时这个环境变量也就失效了。有人可能会问,是否有一种方法来永久设置一个环境变量为特定的值。

Note: 下面提到的Shell配置文件只为特定Shells和桌面环境所读取,不应该被以其他方式使用。

会话范围环境变量


只影响特定用户的环境变量 (而不是整个系统)应该在下面文件中设置:

  • ~/.pam_environment - 这个文件是特别为设置用户的环境变量.它并不是一个脚本文件,而是由每行一句声明表达式组成


Note:使用 .pam_environment 需要重新登录以便初始化环境变量. 仅仅重新打开 terminal 并不能够立即使用这些变量.

如果你正在使用 KDE, 请查看 the KDE User-base page on this topic.

不推荐在下面文件中设置:

  • ~/.profile - This is probably the best file for placing environment variable assignments, since it gets executed automatically by the Display Manager during the start-up process desktop session as well as by the login shell when one logs-in from the textual console.

  • ~/.bash_profile or ~./bash_login - If one of these file exist, bash executes it rather then "~/.profile" when it is started as a login shell. (Bash will prefer "~/.bash_profile" to "~/.bash_login"). However, these files won't influence a graphical session by default.

  • ~/.bashrc - Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. However, performance-wise this may not be the best thing to do since it will cause values to be unnecessarily set many times.



系统范围环境变量


影响整个系统的环境变量设置 (而不是仅仅一个用户) 不应该放在任何系统或桌面会话加载时被执行的系统级别的脚本中,而应该在下面文件中设置:

  • /etc/environment - 这个文件是特别为设置系统范围的环境变量. 它并不是一个脚本文件,而是由每行一句声明表达式组成. 特别地,这个文件存储了系统范围的 locale 和 path 设置.


不推荐在以下文件中设置:

  • /etc/profile - This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the Display Manager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more then invoke the /etc/bash.bashrc file.

  • /etc/bash.bashrc - This is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.


Note: 当处理多用户桌面系统时,把设置放在上面提到的用户的~/.pam_environment 文件中是比较合适的,而不是系统范围的文件,因为这些文件不需要root权限来编辑,并且很容易在多个系统中移动。

原文链接:https://help.ubuntu.com/community/EnvironmentVariables

2011年10月22日星期六

NCL语言基本语法

一、数据类型


1.数值类型:
integer、uint、short、ushort、byte、ubyte、long、ulong、double
2.非数值类型:
logical、string、character、graphic、file、list

字符串(string)可以用双引号包围的一些字符生成("This is an string"),和Javascript类似,字符串可以用加号(+)来连接。它本身不是数组,不可以用数组下标来获取子字符串,但是可以利用一些函数来操作和处理字符串。

二、变量


1.命名规则
和C语言一样,NCL语言中的变量名命名规则是:以字母或下划线开头的字母数字下划线组合。变量名区分大小写。

以下NCL关键字不可用于变量名:
begin break byte character continue create defaultapp do double else end enumeric external False file float function getvalues graphic group if integer int64 list load local logical long new _Missing Missing new noparent numeric procedure quit QUIT Quit record return setvalues short snumeric stop

2.定义和使用变量
NCL中的变量定义规则和Javascript语言相似:无需声明即可使用,其类型由其值决定。和Javascript变量相似的另一面是变量可以拥有属性。

a = (/27.2, -10.0/)
a@_FillValue = -10.0
b = a * 9.0/5.0 + 32.0
print(b)

3.变量维度

NCL中的多维数组使用 行 x 列 的方式储存,这点和C语言一致。按照约定,维度值为0 到 n-1,这里 n 是数据的维度。维度数值是有意义的,因为NCL允许将名称和维度进行关联。这样方便了坐标系下标操作和名称下标操作。变量维度以下列方式命名:
temperature!0 = "frtime"
temperature!1 = "lat"
temperature!2 = "lon"


4.坐标变量

坐标变量是一些一维向量,并且和它们被指定的维度拥有相同的名称和大小。这些向量代表了每一个命名维度的数据坐标。如果这些向量中的值是单调递增或者递减,它们可以在坐标系下标操作中被使用。坐标变量定义如下:
temperature&frtime = forecast_times
temperature&lat = lat_points
temperature&lon = lon_points


三、操作符和表达式


1.操作符和优先级如下
a.数值运算操作符







() 括号
- 取负(注意其优先级较高,和其他编程语言不同)
^ 乘方 数字的乘方或者数组的乘方 结果恒为浮点型,除非操作数为双精度型

* 乘
/ 除
% 取模 两边的操作数必须均为整数
# 矩阵乘法

+ 加 数值相加,如果两边是字符串,则连接两个字符串
- 减

< 小于选择符(注意不是逻辑上的小于) arrayA < arrayB 的结果为一个数组arrayC,arrayC[i]为arrayA[i]和arrayB[i]中较小的一个
> 大于选择符(注意不是逻辑上的大于) arrayA > arrayB 的结果为一个数组arrayC,arrayC[i]为arrayA[i]和arrayB[i]中较大的一个


b.逻辑操作符

.le. 小于等于(less-than-or-equal)
.lt. 小于(less-than)
.ge. 大于等于(greater-than-or-equal)
.gt. 大于(greater-than)
.ne. 不等于(not-equal)
.eq. 等于(equal)
.and. 且
.xor. 亦或(exclusive-or)
.or. 或
.not. 非


c.数组操作符

数组操作符可以组合一组标量值或其他数组为新的数组。一个数组操作符由逗号(,)分割并用'(/' 和 '/)' (称为数组设计符)包围。数组可以由任何基本类型或图形(graphical)对象组成,目前不支持文件数组。每一个被逗号分割的元素必须拥有相同的维度。如果元素类型不同,默认用强制转换规则将所有元素转换为相同的类型。

下面是一些数组操作符的例子:

(/ 1, 2, 3, 4, 5 /)
(/ (/ 1, 2, 3 /)^2, (/ 4, 5, 6 /)^3,(/ 7, 8, 9 /)^4 /)
(/ a - b, b + c, c / d /)

d.数组下标操作
NCL中的数组下标操作和Fortran类似,除了下面几点不同:
i)NCL下标范围为[0,n-1]
ii)NCL中对于坐标变量(coordinate variables)的下标操作是对坐标变量中的数据进行操作
iii)命名的下标使用维度的名字以允许对数组重排序

下标操作示例:
temperature(0,5,6)
temperature(1:3,5,6)
temperature(1:3,4:5,5:6)
temperature(0:4:2,0:5:3,0:6:4)
temperature(3:1,5,6)
temperature(3:1,4:5,5:6)
temperature(:2,:1,5:)
temperature(:,:,:)
temperature(:2:-1,:1:-1,5::-1)
temperature(::-1,::-1,::-1) 反转数组
temperature((/1,1,1,2,2,2,/),:,:) 使用向量作为第一个下标

坐标系下标操作
temperature(0,{20:60},{-95:-120})
temperature(0,{20},{-95})
temperature(0,{:20:2},{:-95:2})

名称下标操作
temperature( time | 0, lon | :, lat | : )
temperature( time | :, {lon | 20 : 60}, {lat | -95 : -120})


四、NCL 语句



1.块语句(Blocks)

块语句提供了一种将一系列命令分组的方式。

begin
statement list
end

当以加载脚本(loading scripts)或管道脚本(piping scripts)方式使用NCL时必须使用块语句

2.if 语句

if(scalar_logical_expression) then
statement list
end if

if( scalar_logical_expression) then
statement list
else
statement list
end if

3.do 循环语句

statement list
end do

do loop_identifier = scalar_start_expr , scalar_end_expr , scalar_stride_expr
statement list
end do

4.while 循环语句

do while (scalar_logical_expression)
statement list
end do

5.赋值语句

'->'、 '!'、 '&' 和 '@' 构造符被用来指定文件变量声明、维度名称声明、坐标变量声明和属性声明。没有这些构造符时,将进行普通的值-变量 赋值。

6.函数语句

NCL中有两种可重用的代码片段:函数(Function)和过程(procedure),这点和Fortran及Matlab语言类似。函数和过程有相似的定义语法,唯一的区别是function和procedure关键字。函数定义方法如下:

function function_name ( declaration_list )
local local_identifier_list
begin
statement list
return(return_value)
end


五、NCL 输入输出(IO)



1. 输入输出支持格式的文件
addfile

2. 输入输出ascii文件
asciiread asciiwrite

3.输入输出二维数组(矩阵)
write_matrix