1.算数运算符
#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
2 .关系运算符
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
3. 布尔运算符
#!/bin/sh
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a -lt 100 -a $b -gt 15 : returns true"
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
4.字符串运算符
#!/bin/sh
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
5.文件测试运算符
#!/bin/sh
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
6.遍历数组
#!/bin/bash
array=(a b c d)
for arr in ${array[@]}
do
echo $arr
done
7.ifelse语句
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
8.while循环
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 5 ]
do
COUNTER='expr $COUNTER + 1'
echo $COUNTER
done
echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: '
while read FILM
do
echo "Yeah! great film the $FILM"
done
9.for循环
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
10.函数
#!/bin/bash
funSum(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read bNum
echo "The two numbers are $aNum and $bNum "
return `expr $aNum + $bNum`
}
funSum
# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"
#!/bin/bash
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # 参数个数
echo "The string of the parameters is $* !" # 传递给函数的所有参数
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
11.java程序启动脚本示例
#!/bin/bash
#-----------------------------------------------
#Author:test
#
#-----------------------------------------------
JVM_OPTIONS="-server -Xms20m -Xmx20m -XX:MaxPermSize=20M
-XX:+HeapDumpOnOutOfMemoryError"
#
#帮助信息输出函数
#
usage(){
echo "用法: ./appdemo.sh [options]
其中的选项包括:
--start 启动app
--stop 停止app"
}
error_info(){
echo $1
}
start(){
JVM_OPTIONS=$JVM_OPTIONS" -Duser.dir=$APPDEMO_HOME"
echo "jvm options:$JVM_OPTIONS"
java $JVM_OPTIONS -jar $APPDEMO_HOME/app-demo.jar start
}
stop(){
java -jar $APPDEMO_HOME/app-demo.jar stop
}
casage(){
case $1 in
--help)
usage
;;
--start)
start
;;
--stop)
stop
;;
*)
error_info "错误,找不到该选项,请使用--help察看可用选项"
;;
esac
}
if [ -z $APPDEMO_HOME ]
then
APPDEMO_BIN=`pwd`
APPDEMO_HOME=$(dirname $APPDEMO_BIN)
fi
if [ $# == 0 ] || [ $# -gt 1 ]
then
usage
else
command=$1
casage $command
fi