免费注册 查看新帖 |

Chinaunix

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

求解:关于bash行命令解释的基本问题,有脚本例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-14 15:11 |只看该作者 |倒序浏览
直接上脚本例子:
##### my_test.sh #####
#!/bin/bash
function my_fun()
{
    MY_VAR=abc \
    echo "$1$MY_VAR$1"
    MY_VAR=abc  echo "$1$MY_VAR$1"
    MY_VAR=abc  make -f my.mk
    echo $MY_VAR
}
my_fun hello

####### my.mk ######
# makefile : my.mk
TARGET := target-$(MY_VAR)

all:
    @echo $(TARGET)

执行my_test.sh,输出
hellohello
hellohello
target-abc

在 my_fun中,同一行内定义变量MY_VAR和执行echo命令,MY_VAR变量没有输出。第一和第二句是相同的,而第三句执行make程序,则变量有定义。

这是为什么呢?shell执行一行命令时有什么讲究或者玄机呢?

论坛徽章:
0
2 [报告]
发表于 2010-07-14 16:46 |只看该作者
在线等了好久,没人理我吗

论坛徽章:
0
3 [报告]
发表于 2010-07-14 16:51 |只看该作者
回复 1# sanxiao


   看看基础先,没有玄机

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
4 [报告]
发表于 2010-07-14 16:51 |只看该作者
回复 2# sanxiao


    答案在这里,慢慢看。http://bbs.chinaunix.net/viewthr ... p;extra=&page=1

论坛徽章:
0
5 [报告]
发表于 2010-07-14 16:56 |只看该作者
回复 4# blackold


    多谢了,正是我想要的。

论坛徽章:
0
6 [报告]
发表于 2010-07-14 17:03 |只看该作者
AA=1 eval echo \$AA

1

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
7 [报告]
发表于 2010-07-14 17:33 |只看该作者
有鬼?

论坛徽章:
0
8 [报告]
发表于 2010-07-15 09:49 |只看该作者
多谢上面各位的答复,后又参考了man手册,摘录如下:

ENVIRONMENT
       When a program is invoked it is given an array of strings called the environment.  This is a list of name-value pairs, of
       the form name=value.

       The  shell  provides  several ways to manipulate the environment.  On invocation, the shell scans its own environment and
       creates a parameter for each name found, automatically marking it for  export  to  child  processes.   Executed  commands
       inherit  the  environment.   The export and declare -x commands allow parameters and functions to be added to and deleted
       from the environment.  If the value of a parameter in the environment is modified, the new  value  becomes  part  of  the
       environment,  replacing the old.  The environment inherited by any executed command consists of the shell's initial envi©\
       ronment, whose values may be modified in the shell, less any pairs removed by the unset command, plus any  additions  via
       the export and declare -x commands.

       The  environment  for  any simple command or function may be augmented temporarily by prefixing it with parameter assign©\
       ments, as described above in PARAMETERS.  These assignment statements affect only the environment seen by that command.

       If the -k option is set (see the set builtin command below), then all parameter assignments are placed in the environment
       for a command, not just those that precede the command name.

       When bash invokes an external command, the variable _ is set to the full file name of the command and passed to that com©\
       mand in its environment.

又学会了一点
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP