【Linux入门到精通系列讲解】shell脚本语法入门教程(看一篇就够了)
文章目录
#!/bin/bash
#假如在/tep/abc文件夹 那么创建一个
if [ ! -d ~/Desktop/work/tmp ]
then
mkdir -v ~/Desktop/work/tmp
echo "123"
echo "create /tmp/abc ok"
fi
#!/bin/bash
if [ $USER == 'root' ]
then
echo "hello,root"
else
echo "hello,guest"
fi
#!/bin/bash
if [ $1 -eq $2 ]
then
echo "$1==$2"
else
if [ $1 -gt $2 ]
then
echo "$1>$2"
else
echo "$1<$2"
fi
fi
~
~
#!/bin/bash
if [ $1 -eq $2 ]
then
echo "$1==$2"
elif [ $1 -gt $2 ]
then
echo "$1>$2"
else
echo "$1<$2"
fi
~
~
数学表达式-双括号
#!/bin/bash
if (( 100%3+1>1 ));
then
echo "yes"
else
echo "no"
fi
双方括号可以做通配符。
#!/bin/bash
for var in ab ac rx bx rvv vt
do
if [[ "$var" == r* ]]
then
echo "$var"
fi
done
~
~
~
#/bin/bash
for var in `seq 1 9`
do
echo $var
sleep 1
done
#!/bin/bash
for (( i=1;i<10;i++ ))
do
echo $i
done
#!/bin/bash
read -p "NUM:" num1
while [ $num1 -gt 0 ]
do
echo ">"
sleep 3
done
#!/bin/bash
read -p "num:" n
case $n in
1)
echo haha
;;
2)
echo hehe
;;
*)
echo "1|2"
;;
esac
##########################################################################
# File Name: do11.sh
# Author: LiXiang
# mail: [email protected]
# Created Time: Wed 24 Mar 2021 08:18:44 PM CST
#########################################################################
#!/bin/bash
start(){
echo "Apache start...."
}
stop(){
echo "Apache stop....."
}
start