您的位置:首页 > 运维架构 > Shell

shell---if语句

2016-01-07 12:41 1026 查看
shell小知识:

1.引用变量时需要加‘$’
2.shell的变量查看与删除
set | grep 变量名     //查看设置的变量
unset 变量名     //删除变量
3.shell中的单引号与双引号区别



注:单引号原封不动的赋值给变量;双引号取消空格的作用
4.反引号,将shell命令赋值给变量



5.read:读入数据,赋值给变量




6.expr:对整数型变量进行算术运算



注:注意空格,\是转义,取消*的特殊含义

if语句:
vim if.sh

#!/bin/bash     //指定运行脚本的shell   //脚本开始
#File: if.sh

echo 'Hello, how old are you?'
echo 'Please input your age:'
read age                 //读入数据

if [ $age -eq 28 ]; then
echo "You're at the age of marriage!"
elif [ $age -gt 28 -a $age -lt 35 ]; then     //判断年龄在28到35之间
echo "You must go on a blind date now."
elif [ $age -lt 28 ]; then
echo "You're still young and good to learn!"
else
echo "Sorry, I don't kown your age!"
fi                                      //脚本结束

chmod +x if.sh     //赋予执行权限

./if.sh     //脚本当前路径下执行脚本


本文出自 “经验来自痛苦” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: