加减乘除
vim jjcc
#!/bin/bash
read -t 30 -p "Please input + and - and * and / : " jjcc
read -t 30 -p "Please input number : " num1
read -t 30 -p "Please input number : " num2
if [ "$jjcc" == + -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 + $num2))
echo "num1+num2=$sum"
elif [ "$jjcc" == - -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 - $num2))
echo "num1-num2=$sum"
elif [ "$jjcc" == "*" -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 * $num2))
echo "num1 x num2=$sum"
elif [ "$jjcc" == / -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 / $num2))
echo "num1/num2=$sum"
else
echo "num1 or num2 no number"
fi
str1 == str2 判断字符串是否相等
str1 != str2 判断字符串是否不相等
read -t 30 -p "please input a filename: " file
判断file的值是否存在,为普通文件,为目录文件等
#判断用户输入的是什么类型的文件
#! /bin/bash read -t 30 -p "please input a filename" file if [-z "$file"] then echo "error,please input a filename" exit 1 elif [!-e "$file"] then echo "your input is not a file" exit 2 elif [-f "$file"] then echo "this is a file" exit 3 elif[-d "$file"] then echo "this is a directory" exit 4 else echo "this is a other file" fi
#!/bin/bash
read -t 30 -p "Please input a file name:" file
if [ -z "$file" ]
then
echo "Error, please input a file name!"
exit 11
elif [ ! -e "$file" ]
then
echo "Your input is not a file name!"
exit 22
elif [ -f "$file" ]
then
echo "$file is a regular file!"
elif [ -d "$file" ]
then
echo "$file is a directory !"
else
echo "$file is another file!"
fi
例子:判断用书输入的是什么文件
#!/bin/bash
read -p "Please input a filename :" file
if [-z "$file" ]
#判断file变量是否为空
then
echo " Error,please input a filename"
exit 1
#对于多分之判断语句,如果第一个不成立,接着判断第二个,所以这里要加exit,第一个都是空了,不成了了,直接让他退出
elif [ ! -e "$file" ]
#判断file的值是否存在 -e判断文件是否存在,存在则为真
then
echo "Your input is not a file !"
exit 2
elif [ -f "$file" ]
#判断file的值是否为普通文件,-f判断文件是否存在,并且是否为普通 文件
then
echo "$file is a regulare file!"
elif[ -d "$file" ]
#判断file的值是否为目录文件,-d判断文件是否存在,并且是否为目录 文件
then
echo "file is a directory!'
else
echo "file is other file!"
fi
#!/bin/bash
read -t 30 -p "Please input a file name:" file
if [ -z "$file" ]
then
echo "Error, please input a file name!"
exit 11
elif [ ! -e "$file" ]
then
echo "Your input is not a file name!"
exit 22
elif [ -f "$file" ]
then
echo "$file is a regular file!"
elif [ -d "$file" ]
then
echo "$file is a directory !"
else
echo "$file is another file!"
fi
例子1.判断用户输入的是什么文件