实例 1-2-6
if [ -z $1 ]||[ -z $2 ]
then
echo "please input two numbers"
exit
else if [ $1 -gt $2 ] ; then
echo "Param1 > Param2"
else if [ $1 -lt $2 ] ; then
echo "Param1 < Param2"
else
echo "Param1 == Param2"
fi
fi
fi
结果
[root@localhost code_linux]# ./1-2-6
please input two numbers
[root@localhost code_linux]# ./1-2-6 3 3
Param1 == Param2
[root@localhost code_linux]# ./1-2-6 3 4
Param1 < Param2
[root@localhost code_linux]# ./1-2-6 3 1
Param1 > Param2
实例 1-2-7
#!/bin/bash
counter=0
for files in *
do
if [ -f "$files" ] ; then
counter=`expr $counter + 1` #等价于counter=$( ($counter + 1))
fi
done
echo "There are $counter files in `pwd`"
结果
[root@localhost code_linux]# ls
1-2-6 1-2-7
[root@localhost code_linux]# ./1-2-7
There are 2 files in /home/lidong/code_linux
例1-2-8
#!/bin/bash
echo -n "Please enter number:"
read n
sd=0
rev=""
on=$n
echo "You put number is $n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
rev="$rev$sd"
done
echo "$on in a reverse order $rev"
结果
[root@localhost code_linux]# ./1-2-8
Please enter number:12345
You put number is 12345
12345 in a reverse order 54321