简单if结构
简单的if结构是:
if expression
then
command
command
…
fi
在使用这种简单if结构时,要特别注意测试条件后如果没有“;”,则then语句要换行,否则会产生不必要的错误。如果if和then可以处于同一行,则必须用“;”
if [ 1 -eq 1 ];then -→ 不需要换行
echo Yes
fiif [ 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 "请输入你是否爱我,我爱你|我不爱你:" loveif [ 我爱你 == "$love" ]
thenecho "我也爱你!"
fi
# chmod +x love.sh
# ./love.sh