shell脚本基础(简单if结构)

简单if结构
简单的if结构是:
if expression
then
command
command

fi
在使用这种简单if结构时,要特别注意测试条件后如果没有“;”,则then语句要换行,否则会产生不必要的错误。如果if和then可以处于同一行,则必须用“;”

if [ 1 -eq 1 ];then             -→ 不需要换行
echo Yes
fi

if [ 1 -eq 1 ]
then
echo Yes
fi

+++++++++++++++++++++++++

vim if_exam1.sh 
# if_exam1.sh:inputs a integer then compare with 15
#!/bin/bash
#if_exam1
echo "Please input a integer:"
read integer1
if [ "$integer1" -lt 15 ]
then echo "The integer which you input is lower than 15."
fi

+++++++++++++++++++++++++

vim if_exam2.sh 
#if_exam2.sh: first creates a file,and  test the file is existence,
# then test the file's authority.
#!/bin/bash
touch if_file1
if [ -e if_file1 ]
then echo "The file if_file1 is created successfully!"
fi
if [ -r if_file1 ]
then echo "The file can read."
fi
if [ -w if_file1 ]
then echo "The file can write."
fi
if [ -x if_file1 ]
then echo "The file can execute."
fi

+++++++++++++++++++++++++

# vim love.sh
#!/bin/bash
read -p "请输入你是否爱我,我爱你|我不爱你:" love

if [  我爱你 == "$love" ]
then
	echo "我也爱你!"
fi
# chmod +x love.sh
# ./love.sh
  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值