shell FAQ

【1.shell 双方括号】

Q1:在andriod 4.0项目上签名是遇到一个问题

调用脚本

./gen_private_keys

gen_private_keys脚本内容如下。

#sign key info
#C   --->  Country Name (2 letter code)
#ST  --->  State or Province Name (full name)
#L   --->  Locality Name (eg, city)
#O   --->  Organization Name (eg, company)
#OU  --->  Organizational Unit Name (eg, section)
#CN  --->  Common Name (eg, your name or your server’s hostname)
#emailAddress --->  Contact email address
AUTH='/C=CN/ST=FuJian/L=XiaMen/O=npjocj/OU=npjocj/CN=npjocj/emailAddress=npjocj@gmail.com'
sh ../../../../development/tools/make_key platform "$AUTH"
sh ../../../../development/tools/make_key media "$AUTH"
sh ../../../../development/tools/make_key shared "$AUTH"
sh ../../../../development/tools/make_key releasekey "$AUTH"
报错:

../../../../development/tools/make_key: 34: [[: not found
../../../../development/tools/make_key: 34: -e: not found

分析:

在提示的make_key下找到出问题的代码:

30  if [[ -e $1.pk8 || -e $1.x509.pem ]]; then
31    echo "$1.pk8 and/or $1.x509.pem already exist; please delete them first"
32    echo "if you want to replace them."
33    exit 1
34  fi

遇到这个问题的时候先了解一下双方括号“[[ ]]”和 "-e"
经过半天的查找发现在我自己电脑上运行生成公钥私钥的脚本却不提示该错误。在其他几台服务器上测试后发现报一样的错。同事的机子上也是一样的错无提示。
怀疑跟bash的版本有关系:

bash -version
结果发现bash版本一样。

GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
那会不会是linux版本的问题呢??

服务器的linux版本

Linux bs30_u64 2.6.32-33-generic #71-Ubuntu SMP Wed Jul 20 17:27:30 UTC 2011 x86_64 GNU/Linux
Ubuntu 10.04.3 LTS
我自己本机的版本
Linux ubuntu_64 2.6.32-45-generic #101-Ubuntu SMP Mon Dec 3 15:39:38 UTC 2012 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS

比较kernel和系统版本,大版本上都是一样,子版本上的区别,这个就要分析版本间的差别了。结果尝试着更新内核也没用

A1:目前的解决方法只能将将调用脚本的语句改成bash里传统的调用 ./xxx.sh

sh and bash are two different shells. While in the first case you are passing your script as an argument to the sh interpreter, in the second case you decide on the very first line which interpreter will be used.

因为 "[[" 在bash里只是个关键字 而不是个命令,Bash把[[ $a -lt $b ]]看作一个单独的元素, 并且返回一个退出状态码。【参考:Advanced Bash Scripting Guide 7.1 Test Constructs在ksh好像可以。

./../../../../development/tools/make_key platform "$AUTH"
./../../../../development/tools/make_key media "$AUTH"
./../../../../development/tools/make_key shared "$AUTH"
./../../../../development/tools/make_key releasekey "$AUTH"



注意:[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符:|| &&

【Q2】#! 我不是注释,我是引用解释。

A1:bash脚本开始总少不了一句

#! /bin/bash
这行是脚本的 sha-bang line(sharp (#) and bang (!))是要告诉大家这个脚本是由 /bin/bash来解释的

参考( Mendel Cooper 的 Advanced Bash Scripting Guide  ,第一章第二部分  Chapter 2. Starting Off With a Sha-Bang

The sha-bang ( #![1] at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [2] magic number, a special marker that designates a file type, or in this case an executable shell script (type man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (the line following the sha-bang line), and ignoring comments. [3]

#!叫做Magic number(幻数、魔数。含义为当一个普通字符含有特殊意义时就叫幻数), #! 用来指定解释脚本的程序,可以是 Shell,也可以是其他程序。

#!/bin/php
#!/bin/expect
#!/bin/perl
......

1) 如果shell脚本的第一个非空白字符不是“ #”,则它会使用Bourne shell。
2) 如果shell脚本的第一个非空白字符是“ #”,但不以“# !”开头时,则它会使用C shell。
3) 如果shell脚本以“# !”开头,则“ # !”后面所跟的字符串就是所使用的shell的绝对路径名。Bourne shell的路径名称为/bin/sh ,而C 外壳则为/bin/csh。



Q3 shell中函数的继承

shell脚本中父shell调用子shell ,变量是会被子进程继承,是否函数也可以被传递呢?

A3:可以直接使用。有些情况下可能需要继承函数,以方便透明使用,方法非常简单,使用“export -f”,注意参数“-f”,它表示函数的意思,不带参数的export只针对变量。

function FUN()
{
echo ":$1=$2"
}
export -f FUN



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值