来杯咖啡,读点源码:中了脚本的毒(1)

        好像也已经深夜了,额,整个世界都超级安静。刚睡了一觉醒来,看看窗外,听不到太多的杂音,武汉白日的喧嚣似乎已经沉寂。看看熟睡的女友,啊,我没有女友!~~  这样的夜,适合一杯咖啡,开始搞点事情~

       以前没太注意,突然看到Android Studio 创建的项目下,有这样两个文件,一个linux的shell脚本,和一个windows的bat脚本,实现的功能基本一致。那么,比较下,研究研究。噶事。

翠花,洗脚(`shell脚` 方言像不像 xi jiao,我开始恶趣了)

        脚本贴一贴,然后我们在代码里逐行解释和分析(我是有劳模潜质的~)

#!/usr/bin/env sh

##############################################################################
##
##  Gradle start up script for UN*X
##
##############################################################################

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn () {
    echo "$*"
}

die () {
    echo
    echo "$*"
    echo
    exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
  CYGWIN* )
    cygwin=true
    ;;
  Darwin* )
    darwin=true
    ;;
  MINGW* )
    msys=true
    ;;
  NONSTOP* )
    nonstop=true
    ;;
esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD="$JAVA_HOME/jre/sh/java"
    else
        JAVACMD="$JAVA_HOME/bin/java"
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD="java"
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ] ; then
        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
            MAX_FD="$MAX_FD_LIMIT"
        fi
        ulimit -n $MAX_FD
        if [ $? -ne 0 ] ; then
            warn "Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
    JAVACMD=`cygpath --unix "$JAVACMD"`

    # We build the pattern for arguments to be converted via cygpath
    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
    SEP=""
    for dir in $ROOTDIRSRAW ; do
        ROOTDIRS="$ROOTDIRS$SEP$dir"
        SEP="|"
    done
    OURCYGPATTERN="(^($ROOTDIRS))"
    # Add a user-defined pattern to the cygpath arguments
    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
    fi
    # Now convert the arguments - kludge to limit ourselves to /bin/sh
    i=0
    for arg in "$@" ; do
        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option

        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
        else
            eval `echo args$i`="\"$arg\""
        fi
        i=$((i+1))
    done
    case $i in
        (0) set -- ;;
        (1) set -- "$args0" ;;
        (2) set -- "$args0" "$args1" ;;
        (3) set -- "$args0" "$args1" "$args2" ;;
        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
    esac
fi

# Escape application args
save () {
    for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
    echo " "
}
APP_ARGS=$(save "$@")

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
  cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem  Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega

来按个摩,脚本开揉

【un*x shell】

1、shell脚本解释器

#!/usr/bin/env sh

不难理解,这个其实是指定脚本的解释器(解释程序),我们可能见得比较多的是

#!/bin/sh
ubuntu@VM-0-11-ubuntu:/bin$ ls -alh sh
lrwxrwxrwx 1 root root 4 Aug  8  2018 sh -> dash

看到没,我在我的ubuntu上看到,sh实际上是/bin/dash 的一个软连接.

那么,/bin/sh  、/bin/bash 、/bin/dash 有什么区别呢?这篇链接讲的还可以  《sh 、bash和dash的区别 》

既然说了是解释器,那么他跟gcc、javac、python、lua 在解释并执行程序时,是不是有异曲同工之处呢?当然。

ubuntu@VM-0-11-ubuntu:~/shell$ cat test.sh 
#!/bin/sh

echo "hello world"

看上面的例子,我们写了个shell版的"hello world"(其实真想写hello girl~~~)。

首先,我们不给此脚本可执行权限,但是尝试运行此脚本怎么样?

ubuntu@VM-0-11-ubuntu:~/shell$ ls -alh test.sh 
-rw-rw-r-- 1 ubuntu ubuntu 30 Jul  7 01:54 test.sh
ubuntu@VM-0-11-ubuntu:~/shell$ ./test.sh     
-bash: ./test.sh: Permission denied                 #### 显然,不加权限是无法运行的
ubuntu@VM-0-11-ubuntu:~/shell$ sh test.sh 
hello world                                         #### 通过sh解释器,运行脚本可以
ubuntu@VM-0-11-ubuntu:~/shell$ 

我们发现使用 “./test.sh”的方法,提示权限不允许;使用 "sh test.sh"则正常打印脚本,并输出了全世界程序员公知的“hello world”。nice,心中困惑以解,果然编程语言的解释执行方法都是一个卵样。

如果仍想要固执的使用“./test.sh”的方式运行脚本,那么执行 “chmod +x 脚本名” ,给予其可执行权限(权限的rwx - 124 就不多讲了)就行

ubuntu@VM-0-11-ubuntu:~/shell$ ls -alh test.sh 
-rw-rw-r-- 1 ubuntu ubuntu 30 Jul  7 01:54 test.sh
ubuntu@VM-0-11-ubuntu:~/shell$ chmod u+x test.sh 
ubuntu@VM-0-11-ubuntu:~/shell$ ls -alh test.sh
-rwxrw-r--  1 ubuntu ubuntu   30 Jul  7 01:54 test.sh    ### rwx ,表示user组已经对此脚本拥有可执行权限 
ubuntu@VM-0-11-ubuntu:~/shell$ ./test.sh 
hello world
ubuntu@VM-0-11-ubuntu:~/shell$ 

2、shell脚本的注释方法

##############################################################################
##
##  Gradle start up script for UN*X
##
##############################################################################

很明显,别看搞了很多行,#### 的一大堆,但是,实际上还是用的单行注释

# 这是shell 的单行注释方法

shell的多行注解,我比较喜欢用如下的方法:

:<<COMMENT
@ Date:2021/07/06
@ Author: Mr.Sugarcane
@ Function: love
@ param: 
@ Desc : this is a function to check wether the love between tow is real in deep.
COMMENT

想想,注释还真是有趣的事情,不同的语言在注释上不尽相同,下面几种注释方法,你能猜出是什么语言?

;;

--

--[[  
--]]


//

/* */

#if 0
#endif 

#

:<<COMMENTS
这是多行注解
COMMENTS


'''
这是多行注解
'''

~~~ 困了,咖啡不顶用,明天继续~~~~ 才扒了两句 ~~~  

-----------------   现在已经是第二天的中午,吃过午饭。午休前巴拉个15分钟 ------------------------

3、shell脚本中参数传递(简称传参)

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"

PRG=“$0” 是什么鬼? 其实,$0 表示的是脚本执行的第一个参数,实质上为该脚本的名称,后面接的参数我们还可以通过 $1 、$2... 等等来表示。这样的用法,在shell函数中会更加常见。我们举个栗子(鸭梨举不动,小栗子还阔以~~)

$ cat 01.sh
#!/bin/sh

love(){
        echo "$1 love $2"
}


love I xiaohong

$ sh 01.sh
I love xiaohong

如上面的示例,参数1的值实际是 “I” ,参数2的值是“xiaohong” ,所以在使用echo输出时,结果为“I love xiaohong”。(我这怕是想姑娘快疯了吧~~)

4、shell中的条件判断

shell条件判断语法我们这里不过多赘述,在我们shell编程专栏里,已经有很详细的介绍过了。我们这里只说一下没见到过的。

while [ -h "$PRG" ] ; do
    # do something here
done

"-h  文件路径" 这是什么鬼?好像第一次见,平时关于文件的判断,常用的不都是 -e -d -f -x -r -w 之类的么, -h 是干哈?

-h其实是在判断这个文件是否软连接(symbolic link),是的话就返回真

验证下:


$ ls -alh
lrwxrwxrwx  1     5 7月   7 14:57 01-l.sh -> 01.sh   # 01-l.sh 是01.sh的软链接
-rwxrw-r--  1    60 7月   7 12:50 01.sh
$
$ test -h 01.sh
$ echo $?   # 返回值为1,说明不是01.sh软连接
1
$ test -h 01-l.sh
$ echo $?    # 返回值为0,说明01-l.sh 是软连接
0

test 指令基本等价于 [[ ]] ,这里也不是我们说明的重点。不发散的引出更多的内容哈。不然这一个脚本几乎要把整个shell编程要串起来撸了~~ 管有点粗

---午休了我,梦中去追寻那(些)可爱的姑娘。我们之间建个软连接也行~~~~~~~~~~~~~~~~~~ 

 5、如何找到软连接的源文件?

while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done

这里稍微复杂点,先说说他干了什么?oh,fuck,他居然是通过循环一层层的找到软连接的源目标文件,直到最后不是软连接,然后将值(文件路径)赋值给PRG。

譬如:A->B, B->C,C->D  , 在此处理之后,最终PRG=D

6、shell中的循环

说说具体细节

(1)这里用到了while循环,格式如:

while 条件测试
do
  执行命令
done

(2)常用的循环还有for循环

for 变量 in 串行
do
   执行命令
done

(3)其次,还可以使用until的方法

until 条件测试
do
    执行命令
done

需要注意的是,不同于while循环条件测试真值则进入循环体,当为假时跳出循环(发现错误,立即停止,看来while老兄适合即时止损);

until循环则是测假值,知道条件为真,才会停止循环(有点像蛮牛,不达目的不罢休)

7、expr: 超强的计算指令之一

expr能干啥?肯定不是吃干饭的。expr命令可以实现数值运算、数值或字符串比较、字符串匹配、字符串提取、字符串长度计算等功能。它还具有几个特殊功能,判断变量或参数是否为整数、是否为空、是否为0等。

ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`

先一起来回顾下expr的语法规则:

ubuntu@VM-0-11-ubuntu:~/shell$ expr --help
Usage: expr EXPRESSION
  or:  expr OPTION

      --help     display this help and exit
      --version  output version information and exit

Print the value of EXPRESSION to standard output.  A blank line below
separates increasing precedence groups.  EXPRESSION may be:

  ARG1 | ARG2       ARG1 if it is neither null nor 0, otherwise ARG2

  ARG1 & ARG2       ARG1 if neither argument is null or 0, otherwise 0

  ARG1 < ARG2       ARG1 is less than ARG2
  ARG1 <= ARG2      ARG1 is less than or equal to ARG2
  ARG1 = ARG2       ARG1 is equal to ARG2
  ARG1 != ARG2      ARG1 is unequal to ARG2
  ARG1 >= ARG2      ARG1 is greater than or equal to ARG2
  ARG1 > ARG2       ARG1 is greater than ARG2

  ARG1 + ARG2       arithmetic sum of ARG1 and ARG2
  ARG1 - ARG2       arithmetic difference of ARG1 and ARG2

  ARG1 * ARG2       arithmetic product of ARG1 and ARG2
  ARG1 / ARG2       arithmetic quotient of ARG1 divided by ARG2
  ARG1 % ARG2       arithmetic remainder of ARG1 divided by ARG2

  STRING : REGEXP   anchored pattern match of REGEXP in STRING

  match STRING REGEXP        same as STRING : REGEXP
  substr STRING POS LENGTH   substring of STRING, POS counted from 1
  index STRING CHARS         index in STRING where any CHARS is found, or 0
  length STRING              length of STRING
  + TOKEN                    interpret TOKEN as a string, even if it is a
                               keyword like 'match' or an operator like '/'

  ( EXPRESSION )             value of EXPRESSION

Beware that many operators need to be escaped or quoted for shells.
Comparisons are arithmetic if both ARGs are numbers, else lexicographical.
Pattern matches return the string matched between \( and \) or null; if
\( and \) are not used, they return the number of characters matched or 0.

Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null
or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/expr>
or available locally via: info '(coreutils) expr invocation'

需要中文翻译么?放心吧,我不会翻译的。哈哈哈😢

在此之前,我们补充点shell下正则表达的知识:

 这一套正则在 grep、sed、awk (shell 三剑客~ 嗯,好贱)通通试用。当然,expr中使用也是大同小异的。

有一篇讲的还不错,expr中文手册及使用示例

expr详细介绍

字符串表达式
-------------------------
'expr'支持模式匹配和字符串操作。字符串表达式的优先级高于数值表达式和逻辑关系表达式。
 
'STRING : REGEX'
     执行模式匹配。两端参数会转换为字符格式,且第二个参数被视为正则表达式(GNU基本正则),它默认会隐含前缀"^"。随后将第一个参数和正则模式做匹配。
 
     如果匹配成功,且REGEX使用了'\('和'\)',则此表达式返回匹配到的,如果未使用'\('和'\)',则返回匹配的字符数。
 
     如果匹配失败,如果REGEX中使用了'\('和'\)',则此表达式返回空字符串,否则返回为0。
 
     只有第一个'\(...\)'会引用返回的值;其余的'\(...\)'只在正则表达式分组时有意义。
 
     在正则表达式中,'\+','\?'和'\|'分表代表匹配一个或多个,0个或1个以及两端任选其一的意思。
 
'match STRING REGEX'
     等价于'STRING : REGEX'。
 
'substr STRING POSITION LENGTH'
     返回STRING字符串中从POSITION开始,长度最大为LENGTH的子串。如果POSITION或LENGTH为负数,0或非数值,则返回空字符串。
 
'index STRING CHARSET'
     CHARSET中任意单个字符在STRING中最前面的字符位置。如果在STRING中完全不存在CHARSET中的字符,则返回0。见后文示例。
    
'length STRING'
     返回STRING的字符长度。
 
'+ TOKEN'
     将TOKEN解析为普通字符串,即使TOKEN是像MATCH或操作符"/"一样的关键字。这使得'expr length + "$x"'或'expr + "$x" : '.*/\(.\)''可以正常被测试,即使"$x"的值可能是'/'或'index'关键字。这个操作符是一个GUN扩展。
     通用可移植版的应该使用'" $token" : ' \(.*\)''来代替'+ "$token"'。
 
   要让expr将关键字解析为普通的字符,必须使用引号包围。
 
 算术表达式
--------------------------
 
'expr'支持普通的算术操作,算术表达式优先级低于字符串表达式,高于逻辑关系表达式。
 
'+ -'
     加减运算。两端参数会转换为整数,如果转换失败则报错。
 
'* / %'
     乘,除,取模运算。两端参数会转换为整数,如果转换失败则报错。
 
16.4.3 逻辑关系表达式
---------------------------
 
'expr'支持普通的逻辑连接和逻辑关系。它的优先级最低。
 
'|'
     如果第一个参数非空且非0,则返回第一个参数的值,否则返回第二个参数的值,但要求第二个参数的值也是非空或非0,否则返回0。如果第一个参数是非空或非0时,不会计算第二个参数。
    
     经过测试,以上手册内容是错误的。正确的应该是:如果第一个参数非0,则返回第一个参数的值,否则返回第二个参数。但如果任意一个参数为空,则报错。除非空字符串使用引号包围,此时将和0的处理方式一样。
 
'&'
     如果两个参数都非空且非0,则返回第一个参数,否则返回0。如果第一个参为0或为空,则不会计算第二个参数。
    
     经过测试,以上手册内容是错误的。正确的应该是:如果两个参数都非0,则返回第一个参数,否则返回0。但任意一个参数为空,则报错。除非空字符串使用引号包围,此时将和0的处理方式一样。
 
'< <= = == != >= >'
     比较两端的参数,如果为true,则返回1,否则返回0。"=="是"="的同义词。"expr"首先尝试将两端参数转换为整数,并做算术比较,如果转换失败,则按字符集排序规则做字符比较。
 
括号'()'可以改变优先级,但使用时需要使用反斜线对括号进行转义。
 
16.4.4 'expr'使用示例
-------------------------------
 
以下为expr的一些示例,其中有将shell的元字符使用引号包围的示例。
 
   将shell中变量'foo'的值增加1:
 
     foo=$(expr $foo + 1)
 
   输出变量路径变量'$fname'中不包含'/'的文件名部分:
 
     expr $fname : '.*/\(.*\)' '|' $fname
    
     解释:其中的'|'是expr中的连接符,只不过是被引号包围防止被shell解析。例如$fname=/etc/hosts,则此表达式返回hosts,如果$fname=/usr/share/,则此表达式'|'的左边为空,所以返回'|'右边的值,即$fname,即返回/usr/share/。
 
   An example showing that '\+' is an operator:
 
     expr aaa : 'a\+'    # 解释:因为REGEX部分没有使用\(\),所以返回匹配的字符数
     => 3
 
     expr abc : 'a\(.\)c'  # 解释:因为REGEX部分使用了\(\),所以返回匹配的字符
     => b
     expr index abcdef cz
     => 3
     expr index index a    # 解释:因为第二个index是关键字
     error-> expr: syntax error
     expr index + index a  # 解释:使用+将index关键字解析为普通字符串
     => 0

expr 使用示例

下面将使用示例来介绍expr的用法,在介绍之前,需要注意三点:

(1).数值表达式("+ - * / %")和比较表达式("< <= = == != >= >")会先将两端的参数转换为数值,转换失败将报错。所以可借此来判断参数或变量是否为整数。

(2).expr中的很多符号需要转义或使用引号包围。

(3).所有操作符的两边,都需要有空格。

以下是expr示例。

(1)."string : REGEX"字符串匹配示例。要输出匹配到的字符串结果,需要使用"\("和"\)",否则返回的将是匹配到的字符串数量。

[root@xuexi ~]# expr abcde : 'ab\(.*\)'
cde

[root@xuexi ~]# expr abcde : 'ab\(.\)'
c

[root@xuexi ~]# expr abcde : 'ab.*'  
5

[root@xuexi ~]# expr abcde : 'ab.'   
3

[root@xuexi ~]# expr abcde : '.*cd*'
4

注意,由于REGEX中隐含了"^",所以使得匹配时都是从string首字符开始的。

[root@xuexi ~]# expr abcde : 'cd.*'  
0

之所以为0,是因为真正的正则表达式是"^cd.*",而abcde不是c开头而是a开头的,所以无法匹配到任何结果。因此,任何字符串匹配时,都应该从首字符开始。

字符串匹配时,会先将两端参数转换为字符格式。

(2)."index string chars"用法示例。

该表达式是从string中搜索chars中某个字符的位置,这个字符是string中最靠前的字符。例如:

[root@xuexi ~]# expr index abcde dec
3

该命令将对字符串"dec"逐字符分解,首先分解得到第一个字符d,从abcde中搜索到d的位置为4,再分解得到第二个字符e,该字符在abcde中的位置为5,最后得到的字符是c,该字符在abcde中的位置为3。其中3是最靠前的字符,所以命令返回的结果为3。

[root@xuexi ~]# expr index abcde xdc
3

如果chars中的所有字符都不存在于string中,则返回0。

[root@xuexi ~]# expr index abcde 1
0

[root@xuexi ~]# expr index abcde 1x
0

(3)."substr string pos len"用法示例。

该表达式是从string中取出从pos位置开始长度为len的子字符串。如果pos或len为非正整数时,将返回空字符串。

[root@xuexi ~]# expr substr abcde 2 3
bcd

[root@xuexi ~]# expr substr abcde 2 4
bcde

[root@xuexi ~]# expr substr abcde 2 5
bcde

[root@xuexi ~]# expr substr abcde 2 0

[root@xuexi ~]# expr substr abcde 2 -1

(4)."length string"用法示例。该表达式是返回string的长度,其中string不允许为空,否则将报错,所以可以用来判断变量是否为空。

[root@xuexi ~]# expr length abcde
5

[root@xuexi ~]# expr length 111
3

[root@xuexi ~]# expr length $xxx
expr: syntax error

[root@xuexi ~]# if [ $? -ne 0 ];then echo '$xxx is null';fi
$xxx is null

(5)."+ token"用法示例。

expr中有些符号和关键字有特殊意义,如"match"、"index"、"length",如果要让其成为字符,使用该表达式将任意token强制解析为普通字符串。

[root@xuexi ~]# expr index index d
expr: syntax error

[root@xuexi ~]# expr index length g
expr: syntax error

[root@xuexi ~]# expr index + length g
4

对值为关键字的变量来说,则无所谓。

[root@xuexi ~]# len=lenght

[root@xuexi ~]# expr index $len g
4

(6).算术运算用法示例。

[root@xuexi ~]# expr 1 + 2
3

[root@xuexi ~]# a=3
[root@xuexi ~]# b=4

[root@xuexi ~]# expr $a + $b
7

[root@xuexi ~]# expr 4 + $a
7

[root@xuexi ~]# expr $a - $b
-1

算术乘法符号"*"因为是shell的元字符,所以要转义,可以使用引号包围,或者使用反斜线。

[root@xuexi ~]# expr $a * $b
expr: syntax error

[root@xuexi ~]# expr $a '*' $b
12

[root@xuexi ~]# expr $a \* $b
12

[root@xuexi ~]# expr $b / $a    # 除法只能取整数
1

[root@xuexi ~]# expr $b % $a
1

任意操作符两端都需要有空格,否则:

[root@xuexi ~]# expr 4+$a 
4+3
[root@xuexi ~]# expr 4 +$a
expr: syntax error

由于expr在进行算术运算时,首先会将操作符两边的参数转换为整数,任意一端转换失败都将会报错,所以可以用来判断参数或变量是否为整数。

[root@xuexi ~]# expr $a + $c
expr: non-integer argument

[root@xuexi ~]# if [ $? != 0 ];then echo '$a or $c is non-integer';fi          
$a or $c is non-integer

(7).比较操作符< <= = == != >= >用法示例。其中"<"和">"是正则表达式正的锚定元字符,且"<"会被shell解析为重定向符号,所以需要转义或用引号包围。

这些操作符会首先会将两端的参数转换为数值,如果转换成功,则采用数值比较,如果转换失败,则按照字符集的排序规则进行字符大小比较。比较的结果若为true,则expr返回1,否则返回0。

[root@xuexi ~]# a=3

[root@xuexi ~]# expr $a = 1
0

[root@xuexi ~]# expr $a = 3
1

[root@xuexi ~]# expr $a \* 3 = 9
1

[root@xuexi ~]# expr abc \> ab
1

[root@xuexi ~]# expr akc \> ackd
1

(8).逻辑连接符号"&"和"|"用法示例。这两个符号都需要转义,或使用引号包围。

以下是官方文档中给出的解释,但实际使用过程中是不完全正确的。

"&"表示如果两个参数同时满足非空且非0,则返回第一个参数的值,否则返回0。且如果发现第一个参数为空或0,则直接跳过第二个参数不做任何计算。

"|"表示如果第一个参数非空且非0,则返回第一个参数值,否则返回第二个参数值,但如果第二个参数为空或为0,则返回0。且如果发现第一个参数非空或非0,也将直接跳过第二个参数不做任何计算。

正确的应该是:

"&"表示如果两个参数都非0,则返回第一个参数,否则返回0。但任意一个参数为空,则expr报错。除非空字符串使用引号包围,则处理方法和0一样。

"|"表示如果第一个参数非0,则返回第一个参数的值,否则返回第二个参数。但如果任意一个参数为空,则expr报错。除非空字符串使用引号包围,则处理方法和0一样。

[root@xuexi ~]# expr $abc '|' 1
expr: syntax error

[root@xuexi ~]# expr "$abc" '|' 1
1

[root@xuexi ~]# expr "$abc" '&' 1 
0

[root@xuexi ~]# expr $abc '&' 1 
expr: syntax error

[root@xuexi ~]# expr 0 '&' abc
0

[root@xuexi ~]# expr abc '&' 0
0

[root@xuexi ~]# expr abc '|' 0
abc

[root@xuexi ~]# expr 0 '|' abc  
abc

[root@xuexi ~]# expr abc '&' cde
abc

[root@xuexi ~]# expr abc '|' cde
abc

回到我们研究的脚本中来,看看这里的expr具体完成了什么?

    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi

link=`expr "$ls" : '.*-> \(.*\)$'`

这里很明显使用了 字符串匹配模式 ‘expr  string : REGEX’ ,

解读下正则的部分,

. 单个匹配

* 前一个字符匹配0次或者任意次数

-> 软连接的标识符

到这儿,实际已经把软件接符号及前面的所有字符匹配了,那么重点来了

\(\)    用于输出表达。上面我们也介绍过, 如果匹配成功,且REGEX使用了'\('和'\)',则此表达式返回匹配到的,如果未使用'\('和'\)',则返回匹配的字符数。 如果匹配失败,如果REGEX中使用了'\('和'\)',则此表达式返回空字符串,否则返回为0。

\(.*\)不难理解,即为输出匹配的一个或者多个字符==》换言之,输出  软连接后面的文件名

expr "$link" : '/.*' > /dev/null

在if 条件判断中,再次使用expr,又是干哈呢? 这儿还是用了字符串匹配模式 ‘expr  string : REGEX’ 。不过这里相对简单,匹配首字符为'/',后面接一个或者多个字符。不过,这里使用expr并不会将匹配的字符串输出,而是返回的匹配到的字符串数量。重定向,数量输出到 /dev/null .

如果该条指令执行成功,为true, `echo $?` 为0;否则为执行失败,值为1

8、dirname

 PRG=`dirname "$PRG"`"/$link"

 dirname实际上就是获取文件的上一层目录。这里要记住linux下一切皆文件的思想,文件夹也是文件~别闹。放个使用说明吧,多的就不研究了,毕竟真的胸大胸小,一目了然(我不是说小就不好啊~~~)

dirname --help
Usage: dirname [OPTION] NAME...
Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current directory).

  -z, --zero     end each output line with NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

Examples:
  dirname /usr/bin/          -> "/usr"
  dirname dir1/str dir2/str  -> "dir1" followed by "dir2"
  dirname stdio.h            -> "."

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/dirname>
or available locally via: info '(coreutils) dirname invocation'

继续源代码:

SAVED="`pwd`"                        # 保存当前路径
cd "`dirname \"$PRG\"`/" >/dev/null  #切换目录
APP_HOME="`pwd -P`"                  
cd "$SAVED" >/dev/null


------------------------------------------
pwd --help
pwd: pwd [-LP]
    Print the name of the current working directory.
    
    Options:
      -L        print the value of $PWD if it names the current working
                directory
      -P        print the physical directory, without any symbolic links
    
    By default, `pwd' behaves as if `-L' were specified.
    
    Exit Status:
    Returns 0 unless an invalid option is given or the current directory
    cannot be read.

 9、basename

basename的用法跟dirname正好相反,一个取头,一个取尾。看下面例子,不多说。

basename --help
Usage: basename NAME [SUFFIX]
  or:  basename OPTION... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.

Mandatory arguments to long options are mandatory for short options too.
  -a, --multiple       support multiple arguments and treat each as a NAME
  -s, --suffix=SUFFIX  remove a trailing SUFFIX; implies -a
  -z, --zero           end each output line with NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

Examples:
  basename /usr/bin/sort          -> "sort"
  basename include/stdio.h .h     -> "stdio"
  basename -s .h include/stdio.h  -> "stdio"
  basename -a any/str1 any/str2   -> "str1" followed by "str2"

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/basename>
or available locally via: info '(coreutils) basename invocation'

啊!这楼盖的太高了。从开一篇继续吧~~~  翠花,俺来啦,哈哈哈

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘会跳舞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值