发新话题
打印

[shell脚本] 初学shell编程高手帮帮忙啊

初学shell编程高手帮帮忙啊

if [[ $a1 = *1 ]]; then
其中a1是一个赋值的变量里面的值是任意的整数,这个if语句判断后面*1是什么意思啊????
条件是要判断什么
我知道-eq这些,但是这个写法第一次见不知道是什么意思,双[]号也可以进行模式匹配这个也知道但是上面是什么意思?谢谢了啊
希望大家多多支持,共同进步
http://blog.sina.com.cn/heaven1985tree

TOP

-eq是比较数值的,不是太理解这个*1有什么深意

TOP

是啊,我也是不太清除*1是什么意思,我想的是不是模式匹配,匹配以1结尾的,希望高手进来解答啊
希望大家多多支持,共同进步
http://blog.sina.com.cn/heaven1985tree

TOP

tux@tux-desktop:~$ a1=1
tux@tux-desktop:~$ [[ $a1 =  *1 ]]
tux@tux-desktop:~$ echo $?
0
tux@tux-desktop:~$ a1=21
tux@tux-desktop:~$ [[ $a1 =  *1 ]]
tux@tux-desktop:~$ echo $?
0
tux@tux-desktop:~$ a1=2
tux@tux-desktop:~$ [[ $a1 =  *1 ]]
tux@tux-desktop:~$ echo $?
1
tux@tux-desktop:~$

据此猜测,星号的作用应该是匹配

TOP

谢谢楼上的呵呵,看来猜对了啊
希望大家多多支持,共同进步
http://blog.sina.com.cn/heaven1985tree

TOP

引用:
       [[ expression ]]
              Return  a  status  of  0 or 1 depending on the evaluation of the
              conditional expression expression.  Expressions are composed  of
              the  primaries  described  below  under CONDITIONAL EXPRESSIONS.
              Word splitting and pathname expansion are not performed  on  the
              words  between  the  [[  and  ]]; tilde expansion, parameter and
              variable expansion, arithmetic expansion, command  substitution,
              process  substitution,  and quote removal are performed.  Condi‐
              tional operators such as -f must be unquoted to be recognized as
              primaries.

              When  the  == and != operators are used, the string to the right
              of the operator is considered a pattern and matched according to
              the  rules described below under Pattern Matching.
  If the shell
              option nocasematch is enabled, the match  is  performed  without
              regard  to  the case of alphabetic characters.  The return value
              is 0 if the string matches (==) or does not match (!=) the  pat‐
              tern, and 1 otherwise.  Any part of the pattern may be quoted to
              force it to be matched as a string.

              An additional binary operator, =~, is available, with  the  same
              precedence  as  ==  and  !=.  When it is used, the string to the
              right of the operator is considered an extended regular  expres‐
              sion and matched accordingly (as in regex(3)).  The return value
              is 0 if the string matches the pattern, and 1 otherwise.  If the
              regular  expression  is syntactically incorrect, the conditional
              expression’s return value is 2.  If the shell option nocasematch
              is enabled, the match is performed without regard to the case of
              alphabetic  characters.   Substrings  matched  by  parenthesized
              subexpressions  within  the  regular expression are saved in the
              array variable BASH_REMATCH.  The element of  BASH_REMATCH  with
              index 0 is the portion of the string matching the entire regular
              expression.  The element of BASH_REMATCH with  index  n  is  the
              portion  of the string matching the nth parenthesized subexpres‐
              sion.

              Expressions may  be  combined  using  the  following  operators,
              listed in decreasing order of precedence:

              ( expression )
                     Returns  the  value  of  expression.  This may be used to
                     override the normal precedence of operators.
              ! expression
                     True if expression is false.
              expression1 && expression2
                     True if both expression1 and expression2 are true.
              expression1 || expression2
                     True if either expression1 or expression2 is true.

              The && and || operators do not evaluate expression2 if the value
              of  expression1  is  sufficient to determine the return value of
              the entire conditional expression.
读 man page 就好了,里面解释得很清楚。
bash$ :(){ :|:;};:&

TOP

TOP

嗯,应该学会多利用man
love linux, love life ,love travelling

TOP

yeah, besides the 'man', you can also use the 'info'

TOP

发新话题