Shell简单实例

实例 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值