bash编程脚本之二 整数测试及特殊变量

一.加法器

给脚本传递两个参数(整数),显示两者之和,之积

#!/bin/bash

#

if [ $# -lt 2];then                # $#表参数的总个数

    echo "The rulut is error"

else    

    echo "The and result is $[$1+$2]"              # $[$1+$2],两个参数相加,取其结果

    echo "The proc result is $[$1*$2]"


二.文件判断

给一个文件,是一个普通文件,就显示。如果是一个目录,亦显示之否则,此为无法识别文件

#!/bin/bash

#

file=/roo

if [ -f $file ];then                                    #-d -f 目录、普通文件判断

        echo "The file is common file

elif [ -d $file ];then

        echo "The file is Drectory"

else

        echo "This file cannot recogrize"

fi


三.能接受一个参数,此参数如果是一个存在的文件,就显示ok,否则显示“no such”

#!/bin/bash

#

if [ $# -lt 1 ];then

    echo "This is error"

if [ -e $1 ];then

    echo "OK"

else

    echo "NO such file"

fi


三.输入一个参数,判断参数的几种情型

#!/bin/bash

#

if [ $1 == 'q' ];then

echo "exit"

exit 0

elif [ $1 == 'quit' ];then

echo "exit"

exit 1

else 

echo $1

fi

shell输入的格式为:脚本名 参数。字符串测试两边有空格。


四.计算器的调用

    echo "scale=3;2111/46" | bc


五.1-100之间,所有整数的和,利用for循环。

    

你可能感兴趣的:(字符串,判断)