QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 902|回复: 7

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

[复制链接]
发表于 2003-10-4 22:34:34 | 显示全部楼层 |阅读模式
[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 是完全可以的啊。
发表于 2003-10-5 01:38:06 | 显示全部楼层
有x权限吗?
回复

使用道具 举报

发表于 2003-10-5 02:19:56 | 显示全部楼层
变量脚本内有效,这么作是不行的。
改配置文件吧
回复

使用道具 举报

发表于 2003-10-6 13:13:42 | 显示全部楼层
脚本确实是没有写错。
你看看文件是否有X权限。
对于变量的有效范围,这个export就是干这行的。不用担心。
回复

使用道具 举报

发表于 2003-10-6 14:46:28 | 显示全部楼层

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

[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.
回复

使用道具 举报

发表于 2003-10-8 17:40:47 | 显示全部楼层
$chmod u+x ./test
$./test
回复

使用道具 举报

发表于 2003-10-10 10:52:31 | 显示全部楼层

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

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

export前面加个点".  export"试试
回复

使用道具 举报

发表于 2003-10-11 10:45:37 | 显示全部楼层
DISPLAY=162.105.89.214:0.0
export DISPLAY
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-9-20 16:47 , Processed in 0.058857 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表