请看我写的第一个脚本错在哪里

请看我写的第一个脚本错在哪里

[code:1]
#!/bin/sh
# this will set display to my dormitory
export DISPLAY=162.105.89.214:0.0
[/code:1]

这是我写的第一个脚本,可是,为什么不能用呢?(很受打击啊!)执行这个脚本以后,DISPLAY变量的值仍然没有改变。

但是我在命令行里面敲 export DISPLAY=162.105.89.214:0.0 是完全可以的啊。
有x权限吗?
变量脚本内有效,这么作是不行的。
改配置文件吧
脚本确实是没有写错。
你看看文件是否有X权限。
对于变量的有效范围,这个export就是干这行的。不用担心。
[quote:d1112a58c5="dypang"][code:1]
#!/bin/sh
# this will set display to my dormitory
export DISPLAY=162.105.89.214:0.0
[/code:1]

这是我写的第一个脚本,可是,为什么不能用呢?(很受打击啊!)执行这个脚本以后,DISPLAY变量的值仍然没有改变。

但是我在命令行里面敲 export DISPLAY=162.105.89.214:0.0 是完全可以的啊。[/quote]

The problem is that when a shell tries to run a script, it generally forks a child process to do this. When you run your shell script, the script in fact runs in a child shell of your command shell. Your script is correct. By using "export DISPLAY=162.105.89.214:0.0", the DISPLAY env var should take effect in the child shell and all the child processes of the child shell. But after your script exits, you are back to your command shell which is the parent process of the child shell. So DISPLAY value set in your child shell will not be kept.

There is one way to solve your problem. You can use

. ./test

Adding a "." and a space means (1) enforcing the excutive right of the script. (2) running the script in current shell instead of creating a child shell.
$chmod u+x ./test
$./test
[code:1]
#!/bin/sh
# this will set display to my dormitory
export DISPLAY=162.105.89.214:0.0
[/code:1]

export前面加个点".  export"试试
DISPLAY=162.105.89.214:0.0
export DISPLAY