一、IF/ELSE
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)
|
二、FOR
语法:
for x : = 1 to 10 do
begin
statements ...
end
Type2:
for name [in list]
do
statements that can use $ name ...
done
例子,打印文件目录结构的代码:
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 / t 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
主要是将一串字符串用来生成菜单,然后可以返回用户选择结果,可以做成单选多选等.如:
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