#!/bin/bash
next_tips() #$0代表当前文件名 $1第一个参数 $2 第2个参数 ....
{
echo "---------$1-------------------"
}
next_tips "global or local varialbe test"
#变量赋值左右两边不能有空格
var_test()
{
# local 局部变量
echo "global v1:$v1"
local v1="456"
echo "local v1:$v1"
}
v1="123"
var_test
echo "global2 v1:"${v1}
next_tips "external varialbe test"
# $0 脚本文件名称 $1 第一个参数 $2 第二个参数
# $# 参数个数
# $? 前一个命令或函数返回的状态码
# $* 将所有参数通过字符串返回
# $$ 返回本进程PID
external_fun()
{
#$0 $#(参数个数) $?(上个命令状态码) $*(打印所用参数)
echo "parms total:$#"
echo "func name:$0"
test 1 -eq 2
echo "the return code:$?"
test 1 -ne 2
echo "the return code:$?"
echo "all parms is:$*"
echo "the pid is :$$"
}
external_fun a b c d e f g
next_tips "system vaiable test"
# PATH HOME LOGNAME SHELL PWD
system_var_func()
{
# PATH HOME LOGNAME SHELL PWD
echo "PATH is:$PATH"
echo "HOME is:$HOME"
echo "LOGNAME is:$LOGNAME"
echo "SHELL is:$SHELL"
echo "PWD is:$PWD"
}
system_var_func
next_tips "vaiable operate test"
# "${}" 可输出值
# '${}' 不可输出值,继续打印${}
# `pwd` 反引号,推荐使用$()
# \ 注释部分
specail_func()
{
var1="binfire"
echo "binfire var:${var1}"
echo 'binfire var:${var1}'
echo `pwd`
echo $(pwd)
echo "hello,\"binfire\""
}
specail_func
next_tips "condition string test"
# 检测测试表达式,如果成立返回0,否则返回非0
# 检测表达式有两种方式 test 或 [] 必须要用空格分开
# 字符串 = != -z (空字符串) -n (非空字符串)
# 数值 -eq -ne -gt -lt -ge -le
# 文件 -e -d (目录) -f(常规文件) -w -x -r -L(链接文件) -u (设置了suid)
# 逻辑运算符 ! -a (all) -o (or)
condition_string_test()
{
# 字符串 = != -z (空字符串) -n (非空字符串)
a="11"
b="11"
test "$a" = "$b"
echo "a=b:$?"
test "$a" != "$b"
echo "a!=b:$?"
test -z $a
echo "a为空:$?"
test -n $a
echo "a不为空:$?"
}
condition_string_test
next_tips "condition int test"
condition_int_test()
{
# 数值 -eq -ne -gt -lt -ge -le
a=1
b=1
test "$a" -eq "$b"
echo "a=b:$?"
test "$a" -ne "$b"
echo "a!=b:$?"
test "$a" -lt "$b"
echo "a<b:$?"
test "$a" -gt "$b"
echo "a>b:$?"
test "$a" -le "$b"
echo "a<=b:$?"
test "$a" -ge "$b"
echo "a>=b:$?"
}
condition_int_test
next_tips "condition file test"
condition_file_test()
{
# 文件 -e -d (目录) -f(常规文件) -w -x -r -L(链接文件) -u (设置了suid)
test -e /e/expr/shell/var.sh
echo "file exists:$?"
test -d /e/expr/shell
echo "dir exists:$?"
test -f /e/expr/shell/var.sh
echo "file attr-normal:$?"
test -w /e/expr/shell/var.sh
echo "file attr-w:$?"
test -x /e/expr/shell/var.sh
echo "file attr-x:$?"
test -r /e/expr/shell/var.sh
echo "file attr-r:$?"
test -L /e/expr/shell/var.sh
echo "file attr-L:$?"
test -u /e/expr/shell/var.sh
echo "file attr-u:$?"
}
condition_file_test
next_tips " -a -o ! test "
condition_and_or_test()
{
# -a (and) -o (or)
test 3 -eq 3 -a 4 -eq 4
echo "and test:$?"
test 3 -eq 4 -o 4 -eq 4
echo "or test:$?"
test ! 3 -eq 3
echo "! test:$?"
}
condition_and_or_test
next_tips " if xxx; then elif else if xxx then; fi"
ifelse_test()
{
#请输入个分数;分数必须>=0或<=100,如果输入错误,重新输入; A>=90 B >=80 C>=70 D>=60 E<60
echo "please input a score need ge 0 and le 100"
read score
while test "$score" -lt 0 -o "$score" -gt 100
do
echo "input error,please input a score need ge 0 and le 100"
read score
done
if test "$score" -ge 90; then
echo "A"
elif test "$score" -ge 80; then
echo "B"
elif test "$score" -ge 70; then
echo "C"
elif test "$score" -ge 60; then
echo "D"
else
echo "E"
fi
}
ifelse_test
next_tips " exit 状态码测试"
exit_test()
{
echo "hello,world"
echo "$?"
#exit 100 #返回码<255,外部接收时候即返回100
}
exit_test
echo "$?"
next_tips "case in ) esac"
case_test()
{
read num
case $num in
1)
echo "1"
;;
2)
echo "2"
;;
3)
echo "3"
;;
esac
}
case_test
next_tips "int operate test"
int_test()
{
a=1
b=2
let c=a+b
echo "c=$c"
let c=c-a
echo "c=$c"
let c=c*a
echo "c=$c"
let c=c/a
echo "c=$c"
let c=c**2 #2次方
echo "c=$c"
let c=c%a
echo "c=$c"
}
int_test
next_tips " for while brek continue"
forwhilebreakcontinue_test()
{
for((i=1;i<=5;i++)) #带个双括号
do
echo $i
done
#------
for file in $(ls)
do
echo $file
done
#------
i=1
while test $i -lt 5
do
echo "$i"
if test $i -eq 3 ; then
echo "fk will break"
break
fi
if test $i -eq 1 ; then
echo "fk will continue"
let i=i+1
echo "now i:$i"
continue
fi
let i=i+1
done
}
forwhilebreakcontinue_test
read