test:简单命令的组合
#!/bin/bash
date
who
结果:
2019年 11月 01日 星期五 10:34:27 CST
tty1 2019-10-17 01:40 (:0)
pts/2 2019-11-01 09:03 (10.4.205.170)
test1:echo的使用
#!/bin/bash
echo The time and date are:
date
echo Look who is on the system:
who
echo -----------------------------------------------------
echo -n "The time and date are: "
date
结果:
The time and date are:
2019年 11月 01日 星期五 10:34:41 CST
Look who is on the system:
tty1 2019-10-17 01:40 (:0)
pts/2 2019-11-01 09:03 (10.4.205.170)
-----------------------------------------------------
The time and date are: 2019年 11月 01日 星期五 10:34:41 CST
test2:$的使用
#!/bin/bash
echo User :$USER
echo UserID:$UID
echo HOME :$HOME
echo -------------------------
echo Show time $15
echo Show time \$15
结果:
User :
UserID:1588318
HOME :/home/
-------------------------
Show time 5
Show time $15
test3:自定义变量
#!/bin/bash
user1="XT"
user2="Sam"
day1=10
day2=$day1
echo $user1 checked in $day1 days ago!
echo $user2 checked in $day2 days ago!
结果:
XT checked in 10 days ago!
Sam checked in 10 days ago!
test4:数学运算应用
#!/bin/bash
var1=10
var2=5
var3=1
var4=$[$var1 * ($var2 - $var3)]
echo var4=$var4
结果:
var4=40
test5:if-then-fi
#!/bin/bash
if pwd
then
echo "if-then-fi worked!"
fi
结果:
/home//shell
if-then-fi worked!
test6:if-then-else-fi
#!/bin/bash
if psl
then
echo "if-then-fi worked!"
else
echo "Not work!"
fi
结果:
./test8:行2: psl: 未找到命令
Not work!
test7:if-then-elif-then-fi
#!/bin/bash
if psl
then
echo "psl worked!"
elif pwd
then
echo "pwd work!"
fi
结果:
./test9:行2: psl: 未找到命令
/home//shell
pwd work!
test8:if-test-then-else-fi
#!/bin/bash
var="Full"
if test $var
then
echo "var not NULL"
else
echo "var is NULL!"
fi
结果:
var not NULL