当前位置:编程学习 > 网站相关 >>

./ . 和#!/bin/bash 辨析Linux如何选择当前执行脚本的shell

最近遇到一个有趣的问题,让我搞清楚Linux如何选择当前执行脚本的shell
 
 
执行cts的的 media stress test 需要安装 android-cts-media-1.0.zip
 
把这个文件解开,发现有一个 copy_media.sh 脚本,就是用 adb push 向 android devices 复制文件,很简单的一个脚本,执行起来却有错
 
$./copy_media.sh 1920x1080



1920x1080  
./copy_media.sh: 22: [: 1920x1080: unexpected operator  
./copy_media.sh: 25: [: 1920x1080: unexpected operator  
./copy_media.sh: 28: [: 1920x1080: unexpected operator  
./copy_media.sh: 31: [: 1920x1080: unexpected operator  
./copy_media.sh: 34: [: 1920x1080: unexpected operator  
Usage: copy_media.sh [720x480|1280x720|1920x1080] [-s serial]  
  for testing up to 1280x720, copy_media.sh 1280x720  
  default resolution, when no argument is specified, is 480x360  
  copy_media.sh all will copy all the files.  

 

 
但如果我用
$. copy_media.sh 1920x1080
 
执行就没有问题, 为何?我的猜想是:
 
./ 是用 /bin/sh 执行 copy_media.sh , 它比较”原始“,因此解释不了 其中的 if [ ... ] 语句.  用 . 就是在当前的 bash 下执行,所以成功了。
 
 
下面验证我猜想是否正确
 
我用 $env | grep SHELL输出是bash,那为何 copy_media.sh 是用 /bin/sh 执行
 
SHELL=/bin/bash
 
 
再看文件开始部分,真相大白,原来 copy_media.sh 指定用 /bin/sh执行
 
 
head copy_media.sh   
#!/bin/sh  
# Copyright (C) 2012 The Android Open Source Project  
#  
# Licensed under the Apache License, Version 2.0 (the "License");  
# you may not use this file except in compliance with the License.  
# You may obtain a copy of the License at  
#  
#      http://www.apache.org/licenses/LICENSE-2.0  
#  
# Unless required by applicable law or agreed to in writing, software  

 

 
而用 . (就是 source 命令) 绕开了 #!/bin/sh;另一种方法是删除这行,就会使用$SHELL变量执行的shell;当然改成#!/bin/bash 也可以解决问题。
 
 
总结:
 
Linux 解释脚本按使用 shell 的顺序是
 
1)  脚本第一行中 #!指定
 
2) 没有指定就使用 $SHELL 
 
而使用 . 就是在当前 shell 中执行,当然就不会关注 !#/bin/sh 中的指定了.
 
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,