bash 学习笔记(四) -流程控制

 

一、IF/ELSE

if  condition
then
    statements
[elif condition
    
then  statements ... ]
[
else  
    statements]
fi

 

字符串比较操作
Operator
True if...
str1 = str2a
str1 matches str2
str1 != str2
str1 does not match str2
str1 < str2
str1 is less than str2
str1 > str2
str1 is greater than str2
-n str1
str1 is not null (has length greater than 0)
-z str1
str1 is null (has length 0)
 
文件

Operator

True if...

-d file

file exists and is a directory

-e file

file exists

-f file

file exists and is a regular file (i.e., not a directory or other special type of file)

-r file

You have read permission on file

-s file

file exists and is not empty

-w file

You have write permission on file

-x file

You have execute permission on file, or directory search permission if it is a directory

-O file

You own file

-G file

file's group ID matches yours (or one of yours, if you are in multiple groups).

file1 -nt file2

file1 is newer than file2a

file1 -ot file2

file1 is older than file2

 

二、FOR

语法:

Type   1 :
for  x : =   1  to  10   do
begin
    statements
...
end

Type2:
for  name [in list]
do
    statements that can 
use   $ name ...
done

例子,打印文件目录结构的代码: 

# ! / bin / bash  + xv

MAX_PRINT_COUNTS
= 8
recdir 
()
{
        seperate
=$ repeat_seperate $ seperate

        local current_count
        current_count
= 0
        
for  file in  " $@ " ;   do
                thisfile
=$ thisfile /$ file
                
if  [ -d  " $thisfile "  ] ;   then
                        
# print  blank after any  path .
                        
echo  -e  $ {seperate% $ {path_seperate}} $ repeat_seperate

                        
# print  the  path
                        
echo  -e  $ seperate $ file  
                        recdir 
$( command  ls  $ thisfile )
                
else
                        
if  [  $ current_count -eq  $ MAX_PRINT_COUNTS ]  ;   then
                                
echo  -e  $ {seperate} ......
                        elif [ 
$ current_count -lt  $ MAX_PRINT_COUNTS ]  ;   then
                                
echo  -e  $ seperate $ file
                        fi
                        current_count
=$(( current_count  +   1 ))
                fi

                thisfile
=$ {thisfile% / *}
        done

        
# attention: must using  / not  the  t or  $ repeat_seperate .
        seperate
=$ {seperate # | / t}
}

recls 
()
{
        path_seperate
= " |------- "
        repeat_seperate
= " | "
        seperate
=$ path_seperate
        
        
for  tryfile in  " $@ " ;   do
                
if  [ -d  " $tryfile "  ] ;   then
                        thisfile
=$ tryfile
                        
echo  -e  $ seperate $ thisfile
                        recdir 
$( command  ls  $ tryfile )
                
else
                        
echo  -e  $ path_seperate $ tryfile
                fi
        done
 
        unset 
dir  path_seperate repeat_seperate seperate
}

file
=$ 1
echo   $ file
 
if  [ -d  " $file "  ] ;   then
    
cd   $ file
    recls 
$( ls )
    
cd   ..
fi

>./bashfile /home/setup/mysql++2.1.1

......more result

|-------COPYING
|-------CREDITS
|-------doc
|       |-------mysqlpp-refman.pdf
|       |-------mysqlpp-userman.pdf
|       |-------README.devel
|       |-------README.mysql++
|       |
|       |-------refman
|       |       |
|       |       |-------html
|       |       |       |-------annotated.html
|       |       |       |-------classmysqlpp_1_1BadConversion__coll__graph.png
|       |       |       |-------classmysqlpp_1_1BadConversion.html
|       |       |       |-------classmysqlpp_1_1BadConversion__inherit__graph.png
|       |       |       |-------classmysqlpp_1_1BadConversion-members.html
|       |       |       |-------classmysqlpp_1_1BadFieldName__coll__graph.png
|       |       |       |-------classmysqlpp_1_1BadFieldName.html
|       |       |       |-------classmysqlpp_1_1BadFieldName__inherit__graph.png
|       |       |       |-------......
|       |-------ssqls-pretty
|       |
|       |-------userman
|       |       |-------common.xsl
|       |       |-------fo.xsl
|       |       |
|       |       |-------html
|       |       |       |-------breakages.html
|       |       |       |-------index.html
|       |       |       |-------licenses.html
|       |       |       |-------overview.html
|       |       |       |-------ssqls.html
|       |       |       |-------tquery.html
|       |       |       |-------tutorial.html
|       |       |       |-------unicode.html
|       |       |       |-------......
|       |       |-------html.xsl

....... more result

 三.case

The syntax of case is as follows:

case expression in
    pattern1 )
        statements ;;
    pattern2 | pattern3 )
        statements ;;
    ...
esac

四.select

主要是将一串字符串用来生成菜单,然后可以返回用户选择结果,可以做成单选多选等.如:

# ! / bin / bash
selected
()
{
        PS3
= 'file / directory? '
        
select  selection in  " $@ " ;   do
                
if  [  $ selection ] ;   then
                        
echo  user selected:  $ selection
                        
break
                
else
                        
echo  'invalid selection . '
                fi
        done

}

file
=$ { 1 }
file
=$ {file:? "  Missing. " }
if  [ -d  " $file "  ] ;   then
        
cd   $ file
        selected 
$( ls )

else
        selected 
$ file
fi

[root@localhost bash]# ./select.sh /usr/local/
1) bin                      7) libexec
2) etc                      8) man
3) games                    9) mysql
4) include                 10) sbin
5) jakarta-tomcat-5.0.28   11) share
6) lib                     12) src
file/directory? 5
user selected: jakarta-tomcat-5.0.28

五.while/until

The syntax for while is:

while conditiondo
    statements...done

and until:
until command; do
    statements...done

例子不使用IFS来实现 ls 每个PATH里的目录:

#./bin/bash
path=$PATH:

while [ $path ]; do
    ls -ld ${path%%:*}
    path=${path#*:}
done

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值