shell脚本编程
shell脚本编程
1.执行的基本流程
(1) vi helloworld
---
#!/bin/bash
echo "hello world!"
(2) chmod +x
(3) ./helloworld
2.顺序执行
---
#!/bin/bash
echo "hello world!"
echo "1"
echo "2"
echo "3"
echo "4"
3.shell 变量
---
#!/bin/bash
read a
read b
c=`expr $a + $b`
echo $c
---
#!/bin/bash
echo $PATH
---
#!/bin/bash
read a
read b
c=$a+$b
echo $c
a=1 b=2 打印出结果1+2
---
#!/bin/bash
declare -i a
declare -i b
declare -i c
read a
read b
c=$a+$b
echo $c
1 1
结果2
---
#!/bin/bash
declare -i c
read a
read b
c=$a+$b
echo $c
数据类型由被赋值对象决定
4.shell特殊字符
#
---
#!/bin/bash
#!/bin/sh
#!/bin/sh
# 井号是一种代码注释
# echo "hello!"
$
---
#!/bin/bash
outputs = "hello world"
echo $outputs --- hello world
echo outputs --- outputs
---
vi ./add
./add 1 2 3 4
./add a "b c" d
#!/bin/bash
echo $*
echo $@
for arg in "$*"
do
echo "---"
echo $arg 只输出一次 1234
done
echo "=-=-=-=-=-"
for arg in "$@"
do
echo "---"
echo $arg 输出四次
done
for arg in $*
do
echo "---"
echo $arg 只输出一次 1234
done
echo "=-=-=-=-=-"
for arg in $@
do
echo "---"
echo $arg 输出四次
done
---
#!/bin/bash
echo $#
echo $0
echo $1
echo $2
echo $3
---
#!/bin/bash
echo 'date'
echo `date`
echo "hello \` "
echo "this is \$str "
5. if
---
#!/bin/bash
if [ $# = 1 ] ; then
_ _ _
echo "input num = 1"
fi
---
#!/bin/bash
if [ $# = 1 ]
then
_ _ _
echo "input num = 1"
fi
if [ $# = 1 ]
then
_ _ _
echo "input num = 1"
else
echo "input num != 1"
fi
---
#!/bin/bash
if [ $# = 1 ]
_ _ _
then
echo "input num = 1"
fi
if [ $# = 1 ]
_ _ _
then
echo "input num = 1"
elif [ $# = 2]
then
echo "input num = 2"
else
echo "input !=1 !=2"
fi
---
vi ./if
#!/bin/bash
if [ $1 = $2 ]
then
echo "$1 = $2"
fi
if [ $1 -eq $2] // ./if a a 会报错 要求参数能转换为数字
then
echo "$1 = $2"
fi
-le <=
-lt <
-ge >=
-gt >
---
#!/bin/bash
if [ -f $3 ]
then
echo "文件 \$3 存在"
fi
---
#!/bin/bash
if [ -x $3 ]
then
echo "文件是可执行文件"
fi
---
#!/bin/bash
if [ -d $3 ]
then
echo " \$3 目录存在"
fi
---
#!/bin/sh
if [ ! -d $3 ]
— — — — —
then
echo "\$3 目录不存在"
6. shell if 实现计算器脚本
./cal 1 add 2
./cal 1 sub 2
./cal 1 mul 2
./cal 1 div 2
---
#!/bin/bash
if [ $# = 3]
then
else
fi
---
if [ $2 = "add" ]
then
c=`expr $1 + $23`
elif [ $2 = "sub" ]
then
c=`expr $1 - $23 `
elif [ $2 = "mul" ]
then
c=`expr=$1 * $3`
elif [ $2 = "div" ]
then
c=`expr= $1 /$3`
else
echo "不能进行运算"
fi
---
#!/bin/bash
if [ #$ = 3 ]
then
op=$2
op_a=$1
op_b=$3
if [ $op = "add" ]
then
c=`expr $op_a + $op_b`
elif [ $op = "sub" ]
then
c=`expr $op_a - $op_b`
elif [ $op = "mul" ]
then
c=`expr $op_a \* $op_b` #转义字符
echo " \$c"
elif [ $op = "div" ]
c=`expr $op_a / $op_b`
else
echo "没有这个操作符"
fi
echo $c
else
echo "wrong param"
fi
if 的三个空格
7. case... 实现分支
---
#!/bin/sh
if [ #$ = 3 ]
then
op = $2
op_a = $1
op_b = $3
case $op in
add)
c=`expr $op_a + $op_b `
echo "$op_a + $op_b = $c"
;;
sub)
c=`expr $op_a - $op_b `
echo "$op_a - $op_b = $c"
;;
mul)
c=`expr $op_a \* $op_b ` # * 需转义
echo "$op_a * $op_b = $c"
;;
div)
c=`expr $op_a / $op_b `
echo "$op_a / $op_b = $c"
;;
*)
echo "no opt"
;;
esac
else
echo "wrong param"
fi
8. shell 循环 loop
集合:
ls 、 find
生成集合:
生成1-100 // seq 1-100
for
---
#!/bin/bash
declare sum -i = 0
for i in "seq 1 100"
do
sum=$sum+$i
done
for obj in `ls`
do
echo "this is $obj"
done
echo "1+2+3+... ...+100 = $sum"
---
for ((i=1; i<=100; i++))
do
sum=$sum+$i
done
while
---
#!/bin/bash
i=0
while [ $i -le 100 ]
do
sum=$sum+$i
i=`expr $i + 1 `
done
---
#!/bin/bash
i=0
sum=0
while [ i -le 100 ]
do
if [ $i -eq 3 ]
then
i=`expr $i + 1`
continue
fi
if [ $i = 5 ]
then
break
fi
sum=$sum+$i
i=`expr $i + 1`
done
---
9. 函数基础
实现 ./func 1 2 4 3 对输入的参数进行比较 按照从小到大输出
---
#!/bin/bash
if [ $# = 4 ]
then
if [ $1 -le $2 ]
then
echo "$1 $2"
else
echo "$2 $1"
fi
if [ $3 -le $4 ]
then
echo "$3 $4"
else
echo "$4 $3"
fi
else
fi
封装函数
---
#!/bin/bash
function min_max()
{
if [ $# = 2 ]
then
if [ $1 -le $2 ]
then
echo "$1 $2"
else
echo "$2 $1"
fi
return 1
else
return 0
fi
}
if [ $# = 4 ]
then
min_max $1 $2
echo "$?"
min_max $3 $4
echo "$?"
else
echo "useage ./func 1 2 4 3 "
fi
$? 表示函数的返回值,会表示最后一条命令的返回值
10. 函数的作用域
---
#!/bin/bash
function min_max()
{
local max #局部变量
echo $max #可以输出 max 是一个整体的变量
if [ $# = 2 ]
then
if [ $1 -le $2 ]
then
echo "$1 $2"
else
echo "$2 $1"
fi
return 1
else
return 0
fi
}
if [ $# = 4 ]
then
max=12345
min_max $1 $2
echo "$?"
min_max $3 $4
echo "$?"
else
echo "useage ./func 1 2 4 3 "
fi
全局变量
---
#!/bin/bash
function min_max()
{
local max #局部变量
echo $max #可以输出 max 是一个整体的变量
if [ $# = 2 ]
then
if [ $1 -le $2 ]
then
#echo "$1 $2"
max=$2
min=$1
else
#echo "$2 $1"
max=$2
min=$1
fi
return 1
else
return 0
fi
}
if [ $# = 4 ]
then
max=$1
min=$2
min_max $1 $2
echo "$?"
echo "$min $max"
min_max $3 $4
echo "$?"
echo "$min $max"
else
echo "useage ./func 1 2 4 3 "
fi
---
#!/bin/bash
function min_max()
{ #可以输出 max 是一个整体的变量
if [ $# = 2 ]
then
if [ $1 -le $2 ]
then
echo "$1 $2"
else
echo "$2 $1"
fi
return 1
else
return 0
fi
}
if [ $# = 4 ]
then
val=$(min_max $1 $2)
declare -a array=($val)
echo "array: ${array[0]} ${array[1]}"
echo "return value $?"
echo "array count : ${#array[@]}"
for val in ${array[*]}
do
echo "val $val "
done
min_max $1 $2
echo "$?"
min_max $3 $4
echo "$?"
11. 结束语

浙公网安备 33010602011771号