免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: FH
打印 上一主题 下一主题

关于 bash 模拟 sh 时针对环境变量的讨论 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-01-04 00:18 |只看该作者
原帖由 FH 于 2007-1-3 17:08 发表
看清楚好不好,俺给出了解决方案,而不是像某人那样问题都没有看清也不实验就开始信口开河。


看不出来你给了啥解决方案 ?

我等你好消息啊,不要忘记对 GNU coreutils mailing list 反应后的结果呈报一下。

--

论坛徽章:
0
12 [报告]
发表于 2007-01-04 08:58 |只看该作者
原帖由 kenduest 于 2007-1-4 00:18 发表


看不出来你给了啥解决方案 ?

我等你好消息啊,不要忘记对 GNU coreutils mailing list 反应后的结果呈报一下。

--


晕!4楼不是么?
就你这种看帖方式还申请版主?
但愿不要误杀、误导大家,阿门!

论坛徽章:
0
13 [报告]
发表于 2007-01-04 09:40 |只看该作者
原帖由 FH 于 2007-1-4 08:58 发表
晕!4楼不是么?
就你这种看帖方式还申请版主?
但愿不要误杀、误导大家,阿门!


嗯,你说的好啊,所以我不申请版主了


--

论坛徽章:
3
CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43
14 [报告]
发表于 2007-01-04 15:42 |只看该作者
汗S
FH不觉得确实有些过分么
人家好心好意,就算错了不是也得跟人说声谢谢
虽然我不懂这个,不知道谁对谁错

论坛徽章:
0
15 [报告]
发表于 2007-01-04 15:58 |只看该作者
原帖由 bdwy 于 2007-1-4 15:42 发表
汗S
FH不觉得确实有些过分么
人家好心好意,就算错了不是也得跟人说声谢谢
虽然我不懂这个,不知道谁对谁错


俺就这脾气,如果俺错了,俺从来都勇于承认错误。
俺最看不上的就是自以为高人一头就胡乱指手画脚的,见着这样的俺没有好脾气。
俺该谢什么?是帮俺解决了问题还是指出了俺的错误?也没见着别人跟你胡搅蛮缠的时候你还谢过人家啊。谁站着说话都不腰疼。
既然你承认自己不懂这个,就不要添乱了,谢谢!


[ 本帖最后由 FH 于 2007-1-4 16:01 编辑 ]

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
16 [报告]
发表于 2007-01-04 16:08 |只看该作者
原帖由 FH 于 2007-1-2 21:19 发表

晕!
你自己试一下就知道了
俺Unix玩了十多年,Linux也玩了十年了,什么问题俺还看不清?

看不看得清,跟玩儿了几年没关系!

晕!你没看懂俺的问题吧?知道啥叫交互非login模式不?
你进vi,然后:!sh,再告诉俺有没有PS1!
真是你说的那么简单的事,俺都不好意思发帖子!


这个问题确实不复杂,学知识没什么好意思不好意思的。
你的态度确实过分!应该学孔夫子三省乎己了。

论坛徽章:
0
17 [报告]
发表于 2007-01-04 16:14 |只看该作者
原帖由 yjh777 于 2007-1-4 16:08 发表

看不看得清,跟玩儿了几年没关系!



这个问题确实不复杂,学知识没什么好意思不好意思的。
你的态度确实过分!应该学孔夫子三省乎己了。


以后向你学习

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
18 [报告]
发表于 2007-01-09 15:28 |只看该作者
原帖由 FH 于 2007-1-2 08:33 发表
当bash以sh为名运行时,在交互非login模式下,其继承了所有环境变量(包括任何用户定义的变量),唯独不继承PS*!
号称是靠向POSIX,但是俺用过的所有Unix(AIX、HPUX、SCO)的sh,在该模式下都是继承PS*变量的。 ...


不知道 FH 所定義的“交互非login模式”跟我們所理解的是否一致的?還是有特定所指?
如下是 man bash 的內容:
       A login shell is one whose first character of argument zero is a -,  or
       one started with the --login option.

       An  interactive  shell  is one started without non-option arguments and
       without the -c option whose standard input and error are both connected
       to  terminals  (as determined by isatty(3)), or one started with the -i
       option.  PS1 is set and $- includes i if bash is interactive,  allowing
       a shell script or a startup file to test this state.


由此看來,只要完成 login 只後,在 command line 輸入 bash 或 sh 都應該是“交互非login模式”囉。
上面有提到 PS1 會被設定,我猜,這裡的設定應該是會被“重設”。而兄的問題在於 PS1 是否會從 parent shell 繼承下來吧? 如果我猜得對,那你看到的現象是正常的。

再看 man:
        When  an  interactive  shell that is not a login shell is started, bash
       reads and executes commands from ~/.bashrc, if that file exists.   This
       may  be inhibited by using the --norc option.  The --rcfile file option
       will force bash to read and  execute  commands  from  file  instead  of
       ~/.bashrc.

所以,除了從 parent 的 env/export 所繼承外,~/.bashrc 也是會影響環境結果的。
但不同的 linux distro 都會去修改 /etc/skel/.bashrc 的內容,
這時,最好請兄台說一下你目前试用的版本,還有 ~/.bashrc 的實際代碼才好分析。

有些版本,會從 ~/.bashrc 去 source /etc/bashrc,然後再去 source /etc/profile.d/*.sh 。
而有些則不一定會。
而 FH 兄前面的所謂解決方法,就是在 /etc/profile.d/*.sh 裡去執行,那是可以的。

不過,上面是用 bash 做 command ,如果換成 sh 的話:
       If bash is invoked with the name sh, it  tries  to  mimic  the  startup
       behavior  of  historical  versions  of sh as closely as possible, while
       conforming to the POSIX standard as well.  When invoked as an  interac‐\r
       tive  login  shell, or a non-interactive shell with the --login option,
       it first attempts to read and execute commands  from  /etc/profile  and
       ~/.profile,  in  that  order.   The  --noprofile  option may be used to
       inhibit this behavior.  When invoked as an interactive shell  with  the
       name  sh,  bash  looks for the variable ENV, expands its value if it is
       defined, and uses the expanded value as the name of a file to read  and
       execute.  Since a shell invoked as sh does not attempt to read and exe‐\r
       cute commands from any other startup files, the --rcfile option has  no
       effect.   A  non-interactive  shell  invoked  with the name sh does not
       attempt to read any other startup files.   When  invoked  as  sh,  bash
       enters posix mode after the startup files are read.

       When  bash  is  started in posix mode, as with the --posix command line
       option, it follows the POSIX standard for startup files.  In this mode,
       interactive  shells  expand  the ENV variable and commands are read and
       executed from the file whose name is  the  expanded  value.   No  other
       startup files are read.

所以看來是有 $ENV 這個變數來決定要執行的 script。
如果 ENV 沒設定,那就不執行了。
以我的 openSuSE10.2(GNU bash, version 3.1.17(1)-release )為例,
ENV 是設為 /etc/bash.bashrc 。裡面有多行關於 PS1 的設定,因此也會影響實際的 prompt 結果。

結論就是:不同的發行版本,可能得到的結果都不盡相同,只能以實際的 initial script 作參考。

论坛徽章:
0
19 [报告]
发表于 2007-01-09 19:19 |只看该作者
楼上的确很细致,佩服!
俺的是RHEL-AS4,俺也是man了以后找到的这个方法。
俺试了一下,如果直接在shell下面执行sh,是能够继承PS1的,但是进入了vi就不行了,从其它程序访问sh应该也是这样(推理),但是同样的情况俺在AIX和HP-UX上都做了试验,没有这个问题,俺还特意在HP-UX上man了sh-posix,里面没有提到这个问题。

论坛徽章:
0
20 [报告]
发表于 2007-01-09 19:49 |只看该作者
几个牛人在讨论,我怎么也看不懂,MS没体会到
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP