免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5953 | 回复: 11
打印 上一主题 下一主题

[其他] SHELL是如何处理赋值语句的? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-01 18:13 |只看该作者 |倒序浏览
比如
  1. value=demo
  2. foo=$value
复制代码
那么在进行命令行处理时,$value会被展开。

但是如果这样:
  1. count=0
  2. a_${count}=filename
复制代码
就会报a_0=filename: command not found
从出错信息上看,a_${count}也被正确的展开了,但为何会报错?shell到底是怎么处理赋值语句的?我知道使用eval可以解决这个问题,但是使用eval无非是再进行一次命令行处理,为毛在第二次处理的时候就可以了呢?

论坛徽章:
3
水瓶座
日期:2014-03-25 17:08:042015亚冠之塔什干棉农
日期:2015-08-10 10:45:122015亚冠之萨济拖拉机
日期:2015-08-13 16:05:24
2 [报告]
发表于 2014-08-01 18:21 |只看该作者
a_${count}这样定义,我觉得不好。能用变量作为变量名称么?

论坛徽章:
0
3 [报告]
发表于 2014-08-01 18:39 |只看该作者
回复 2# su8610

这样子是为了动态的生成变量名。比如我想定义100个变量,分别是a_0 ... a_99,这时候就得动态生成了。


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 2014-08-01 18:45 |只看该作者
本帖最后由 jason680 于 2014-08-01 18:46 编辑

回复 1# iLRainyday

array is good idea for you ...
a[0], ...,a[99]

$ man bash
       ...

       A variable may be assigned to by a statement of the form

              name=[value]

       If value is not given, the variable is assigned the null  string.   All
       values  undergo tilde expansion, parameter and variable expansion, com‐
       mand substitution, arithmetic expansion, and quote removal (see  EXPAN‐
       SION below).  If the variable has its integer attribute set, then value
       is evaluated as an arithmetic expression even if the $((...)) expansion
       is  not  used  (see Arithmetic Expansion below).  Word splitting is not
       performed, with the exception of "$@" as explained below under  Special
       Parameters.   Pathname  expansion  is not performed.  Assignment state‐
       ments may also appear as arguments  to  the  alias,  declare,  typeset,
       export, readonly, and local builtin commands.

       In  the context where an assignment statement is assigning a value to a
       shell variable or array index, the += operator can be used to append to
       or add to the variable's previous value.  When += is applied to a vari‐
       able for which the integer attribute has been set, value  is  evaluated
       as  an arithmetic expression and added to the variable's current value,
       which is also evaluated.  When += is applied to an array variable using
       compound  assignment  (see  Arrays  below), the variable's value is not
       unset (as it is when using =), and new values are appended to the array
       beginning  at  one  greater than the array's maximum index (for indexed
       arrays) or added as additional key-value pairs in an associative array.
       When  applied  to  a  string-valued  variable,  value  is  expanded and
       appended to the variable's value.

   

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
5 [报告]
发表于 2014-08-01 19:14 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2014-08-01 23:35 |只看该作者
多谢jason680、zooyo。是这样的,我现在在看beginning protable shell scripting这本书,在The eval command一节中,作者说到since there are no arrays in standard shell ,programmer sometimes use sequence of variable names to similar effect。因此就用了类似于我举例中的方法来模拟数组,最主要还是要说明eval的用法。所以我的问题还是不太明白shell是如何处理赋值语句的。为什么变量在等号左右两端展开的时候,结果却不一致。

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
7 [报告]
发表于 2014-08-02 19:50 |只看该作者
回复 6# iLRainyday

>>...为什么变量在等号左右两端展开的时候,结果却不一致。
   
Left=Right
简单的理解方法....展开不适用在左边(Left)

论坛徽章:
0
8 [报告]
发表于 2014-08-02 20:26 |只看该作者
jason680 发表于 2014-08-02 19:50
回复 6# iLRainyday

>>...为什么变量在等号左右两端展开的时候,结果却不一致。


是的,我在bash manual的3.7.1小节中也看到了这个说法:只对等号右边进行一系列expansion操作。但是从出错信息中:a_0=filename: command not found 可以看到等号左边已经被展开成a_0了。而且如果我使用
  1. eval a_${count}=filename
复制代码
的话,是可以得到正确的结果,即a_0=filename。因为eval是将其参数再重新进行一次命令行处理,如果等号左边不会被展开的话,使用eval应该仍旧出错,但是结果却是正确的。所以搞不明白为什么。

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
9 [报告]
发表于 2014-08-02 20:49 |只看该作者
回复 8# iLRainyday

please refer "关于shell命令行的解释流程"
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1772129&fromuid=24785593

and below is my understood

first time will be:
  a_${count}=filename
  
secondary time, eval run it:
  a_0=filename

论坛徽章:
0
10 [报告]
发表于 2014-08-02 21:27 |只看该作者
回复 9# jason680

这幅图是引自learning bash shell一书的第181页,流程中唯独没有说明赋值语句是在哪个阶段处理以及如何处理的。man bash中"Simple Command Expansion"一节中倒是说到了赋值语句的处理是在进行各种expansion之前,但是又说只对等号右端进行expansion,左端如何处理并没有谈及。按照你上面的理解,在使用eval的时候,也就是第二次进行命令行处理的时候,对等号左端进行了expansion。这和你之前所讲到等号左端不进行处理又矛盾了啊~
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP