shell 判断语句
2018-02-22 18:28
316 查看
语法一:
if []
then
命令序列 1
else
命令序列 2
fi
语法二:
if [条件表达式];
then
命令序列
fi
语法三:
if test 条件表达式 1
then
命令序列 1
elif [条件表达式 2]
then
命令序列 2
else
命令序列 3
fi
判断当前目录下是否存在某文件:
#!/bin/bash
echo “Enter a file name:”
read file
if [-f $file]
then
echo “File $file exists”
fi
判断当前用户名是否和输入的用户名一致:
#!/bin/bash
echo -n “Enter your login name:”
read name
if [“$name” = “$USER”];
then
echo “Hello,$name.How are you today?”
else
echo “You are not $USER,so who are you?”
fi
比较两个数的大小:
#!/bin/bash
echo “Enter the first integer:”
read first
echo “Enter the second integer:”
read second
if [“$first” -gt “$second”]
then
echo “$first is greater than $second”
elif [“$first” -lt “$second”]
then
echo “$first is less than $second”
else
echo “$first is equal to $second”
fi
在文本文件中查找字符串:
#!/bin/bash
if grep “GNU” myfile > /dev/null
then
echo “\”GNU\” occurs in myfile”
else
echo “\”GNU\” does not occurs in myfile”
fi
if []
then
命令序列 1
else
命令序列 2
fi
语法二:
if [条件表达式];
then
命令序列
fi
语法三:
if test 条件表达式 1
then
命令序列 1
elif [条件表达式 2]
then
命令序列 2
else
命令序列 3
fi
判断当前目录下是否存在某文件:
#!/bin/bash
echo “Enter a file name:”
read file
if [-f $file]
then
echo “File $file exists”
fi
判断当前用户名是否和输入的用户名一致:
#!/bin/bash
echo -n “Enter your login name:”
read name
if [“$name” = “$USER”];
then
echo “Hello,$name.How are you today?”
else
echo “You are not $USER,so who are you?”
fi
比较两个数的大小:
#!/bin/bash
echo “Enter the first integer:”
read first
echo “Enter the second integer:”
read second
if [“$first” -gt “$second”]
then
echo “$first is greater than $second”
elif [“$first” -lt “$second”]
then
echo “$first is less than $second”
else
echo “$first is equal to $second”
fi
在文本文件中查找字符串:
#!/bin/bash
if grep “GNU” myfile > /dev/null
then
echo “\”GNU\” occurs in myfile”
else
echo “\”GNU\” does not occurs in myfile”
fi
相关文章推荐
- 22_Shell语言――――双分支条件判断语句、表达式判断、嵌套
- shell脚本编程之判断语句
- shell流程控制语句之一if判断
- shell基础及流程控制语句之一case判断
- shell case语句 判断一个字符是字母还是数字
- shell中if语句中判断 [: too many arguments
- Shell变量,判断和控制语句详解
- shell 脚本之判断语句 if 详解
- shell判断语句
- shell中的循环语句、判断语句实例
- shell if语句之判断参数
- Shell中的条件判断语句if~then~fi
- shell中用case语句判断输入的一个键是大写字母、小写字母还是数字
- shell 判断语句
- Shell IF判断语句
- shell 编程中使用到得if语句内判断参数
- shell 判断语句
- shell中的if判断语句
- shell 编程中使用到得if语句内判断参数
- shell 编程中使用到得if语句内判断参数