1. 使用 shift 来移动参数; 遇用在 -a -b -c,不要求用户按 -a -b -c 来输入的场合;
2. getopts, 比 shift 还要做的多的是可以写成 -bca 来得到-a -b -c 所做的事情,就像 tar -zcvf 这样;
3. 类型的定义
4. 数组的使用
对数据的循环:
2. getopts, 比 shift 还要做的多的是可以写成 -bca 来得到-a -b -c 所做的事情,就像 tar -zcvf 这样;
3. 类型的定义
Option | Meaning |
-a | The variables are treated as arrays |
-f | Use function names only |
-F | Display function names without definitions |
-i | The variables are treated as integers |
-r | Makes the variables read-only |
-x | Marks the variables for export via the environment |
$
val1
=
12
val2
=
5
$ result1 = val*val2
$ echo $ result1
val1*val2
$
$ declare -i val3 = 12 val4 = 5
$ declare -i result2
$ result2 = val3*val4
$ echo $ result2
60
$ result1 = val*val2
$ echo $ result1
val1*val2
$
$ declare -i val3 = 12 val4 = 5
$ declare -i result2
$ result2 = val3*val4
$ echo $ result2
60
数字的比较 | |
Operator | Meaning |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
== | Equal to |
!= | Not equal to |
&& | Logical and |
|| | Logical or |
数字比较2
| |
Test
|
Comparison
|
-lt
|
Less than
|
-le
|
Less than or equal
|
-eq
|
Equal
|
-ge
|
Greater than or equal
|
-gt
|
Greater than
|
-ne
|
Not equal
|
#
!
/
bin
/
bash
for dir in $ {*:- . } ; do
if [ -e $ dir ] ; then
result =$( du -s $ dir | cut -f 1 )
let total =$ result* 1024
echo -n " Total for $dir = $total bytes "
if [ $ total -ge 1048576 ] ; then
echo " ($((total/1048576)) Mb) "
elif [ $ total -ge 1024 ] ; then
echo " ($((total/1024)) Kb) "
fi
fi
done
for dir in $ {*:- . } ; do
if [ -e $ dir ] ; then
result =$( du -s $ dir | cut -f 1 )
let total =$ result* 1024
echo -n " Total for $dir = $total bytes "
if [ $ total -ge 1048576 ] ; then
echo " ($((total/1048576)) Mb) "
elif [ $ total -ge 1024 ] ; then
echo " ($((total/1024)) Kb) "
fi
fi
done
4. 数组的使用
对数据的循环:
for i in "${names[@]}"; do
echo $i
done