Chinaunix首页 | 论坛 | 博客
  • 博客访问: 94128
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 142
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-22 14:20
个人简介

会挽雕弓如满月,西北望,射天狼

文章分类

全部博文(14)

文章存档

2020年(2)

2015年(10)

2014年(2)

我的朋友

分类: LINUX

2015-06-08 15:02:34

自己写的脚本,怕自己也看不懂了,详细描述一下

#!/bin/bash  
这个你懂得,是bash shell的标记,以前我不知道这个有什么用处,如果不写一样可以执行。直道有一天我把脚本放到redhat enterprise 6 的桌面,有标记的和没有标记的显示不同的图标才发现是有用处的。上网搜了一下解释如下,主要是文件游客执行权限时选定究竟是哪种shell的。

这个符号(#!)的名称,叫做”Shebang”或者”Sha-bang”。


Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件的解释程序。

1. 如果脚本文件中没有#!这一行,那么它执行时会默认用当前Shell去解释这个脚本(即:$SHELL环境变量)。


2. 如果#!之后的解释程序是一个可执行文件,那么执行这个脚本时,它就会把文件名及其参数一起作为参数传给那个解释程序去执行。

3. 如果#!指定的解释程序没有可执行权限,则会报错“bad interpreter: Permission denied”。
    如果#!指定的解释程序不是一个可执行文件,那么指定的解释程序会被忽略,转而交给当前的SHELL去执行这个脚本。

4. 如果#!指定的解释程序不存在,那么会报错“bad interpreter: No such file or directory”。
    注意:#!之后的解释程序,需要写其绝对路径(如:#!/bin/bash),它是不会自动到$PATH中寻找解释器的。
5. 当然,如果你使用”bash test.sh”这样的命令来执行脚本,那么#!这一行将会被忽略掉,解释器当然是用命令行中显式指定的bash。




#set -x
set +e

set 设置脚本执行方式 
-x  执行指令后,会先显示该指令及所下的参数。
+<参数>  取消某个set曾启动的参数。
-e  若指令传回值不等于0,则立即退出shell。  ###############################################################################
#   Check CPU core number
###############################################################################


core=$(cat /proc/cpuinfo | grep "cpu cores"| wc -l)
let core++;
检测cpu核数,$取shell 执行结果,即命令的输出
cat /proc/cpuinfo 显示文件;| 管道
 grep "cpu cores"   选取包含 字符串的行
wc -l  打印行的个数

加起来就是
1. 打印cpu信息文件
2. 选取grep "cpu cores"包含的行

3. 判断共有几行,此值为cpu的核数,这个方法仅针对单核cpu

core这个参数的作用是为了多核编译make -j(cpucores + 1) 时使用

###############################################################################
#   Set envionment variables
###############################################################################


source  ~/.bashrc

设置环境变量


if [ ! -d "*" ];then
…… ……

-d 文件夹是否存在, 存在为真


if [ ! -L "*.so" ];then

-L 符号链接是否存在,存在为真


###############################################################################
#   Set build.sh input parameters struct
###############################################################################


TEMP=`getopt -o c:h::m:v::  -n 'build.sh' -- "$@"`
getopt 取输入参数
-o :-o, --options shortopts 设置短参数    
Each short option character in shortopts may be followed by one colon to indicate
it  has a required argument, and by two colons to indicate it has an optional argument(可选参数).
后面一个冒号是必选参数, 即-参数后面必须有选项;后面两个冒号可选参数

-n  :出错时报告错误文件的名字
-- : 后接参数
$@所有参数

if [ $? != 0 ] ; then echo "Terminating..." >&2 ;echo "-h help";exit 1 ; fi
eval set -- "$TEMP"

eval set: Without  options,  the name and value of each shell variable are displayed in a format that can be
reused as input for setting or resetting the currently-set variables.  Read-only variables  cannot
be  reset.  In posix mode, only shell variables are listed.  The output is sorted according to the
current locale.  When options are specified, they set or unset shell  attributes.   Any  arguments
remaining  after  option  processing  are  treated as values for the positional parameters and are
assigned, in order, to $1, $2, ...  $n. 
shell 参数重新排列, 排列规则 "$TEMP", 我的解释.


VERSION=0.1.0
while true ; do
            case "$1" in

            -c|--c-long)
             参数-c  

        case "$2" in
            "all") make_all clean ;shift 2 ;;
            shift 2的作用 参数向前移两个, sh build.sh -c all 变为   sh build.sh 从而退出while
            参数 -c all
            ... ...
            ... ...

             esac
    esac




done
echo "Remaining arguments:"
for arg do
   echo '--> '"\`$arg'" ;
done


set -e


阅读(1636) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~