标签:shell 脚本介绍
1.当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
2.使用条件语句时,变量是必不可少的
3.引用某个命令的结果时,用变量替代
4.写和用户交互的脚本时,变量也是必不可少的
内置变量 $0, $1, $2…
数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]
测试样例:写一个交互脚本:
[root@OBird shell]# vim 2.sh
#!/bin/bash
read -p "please input anumber:" number
echo $number
[root@OBird shell]# sh 2.sh #输入什么就输出什么
please input anumber:99999
99999
[root@OBird shell]# vim 2.sh
#!/bin/bash
read -t 3 -p "please input anumber:" number #“-t 3”加入等待3秒
echo $number
[root@OBird shell]# sh 2.sh #超时会跳出
please input anumber:
[root@OBird shell]#
---------------------分割线------------------------------
[root@OBird shell]# vim 3.sh
#!/bin/bash
##
##
echo $1 $2 $0 $3
[root@OBird shell]# sh 3.sh
3.sh
[root@OBird shell]# vim 3.sh
#!/bin/bash
##
##
echo "\$1=$1"
echo "\$2=$2"
echo "\$0=$0"
echo "\$3=$3"
[root@OBird shell]# sh 3.sh
$1=
$2=
$0=3.sh
$3=
[root@OBird shell]# sh 3.sh 11 22 33
$1=11
$2=22
$0=3.sh
$3=33
[root@OBird shell]# sh 3.sh 11 22 #写两个值$3 是没有的
$1=11
$2=22
$0=3.sh
$3=
数学运算
[root@OBird shell]# a=1;b=2
[root@OBird shell]# c=$a+$b
[root@OBird shell]# echo $c
1+2
[root@OBird shell]# c=$[$a+$b] #数学运算应该这样写
[root@OBird shell]# echo $c
3
------------------------分割线------------------------------
if 语句
[root@OBird shell]# vim if.sh
#!/bin/bash
a=5
if [ $a -gt 3 ] #-gt 大于 < 小于 -lt ==等于 -eq != 不等于 -ne >= 大于等于 -ge <= 小于等于 -le
then
echo "a>3"
fi
[root@OBird shell]# sh if.sh
a>3
[root@OBird shell]# sh -x if.sh # -x 查看详细过程
+ a=5
+ ‘[‘ 5 -gt 3 ‘]‘
+ echo ‘a>3‘
a>3
[root@OBird shell]# vim if.sh
#!/bin/bash
a=5
if [ $a -gt 10 ] #如果
then
echo "a>10"
else #否则
echo "a<=10"
fi
~
[root@OBird shell]# sh if.sh
a<=10
[root@OBird shell]# vim if.sh
#!/bin/bash
a=5
if [ $a -gt 10 ]
then
echo "a>10"
elif [$a -lt 4 ]
then
echo "a<4"
else
echo "4<a<10"
fi
[root@OBird shell]# sh -x if.sh
+ a=5
+ ‘[‘ 5 -gt 10 ‘]‘
+ ‘[‘ 5 -lt 4 ‘]‘
+ echo ‘4<a<10‘
4<a<10
-------------------------分割线-----------------------
if 的几种用法:
[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
[root@OBird shell]# if [ -f 1.txt ]; then echo ok; fi
[root@OBird shell]# touch 1.txt
[root@OBird shell]# if [ -f 1.txt ]; then echo ok; fi #判断是否存在1.txt
ok
[root@OBird shell]# if [ -r 1.txt ]; then echo ok; fi # -r 判断文件是否可读
ok
[root@OBird shell]# if [ -d /tmp/ ]; then echo ok; fi #判断是否存在 目录
ok
[root@OBird shell]# vim if2.sh
#!/bin/bash
read -p "please input a number:" n
m=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$m" ] # -n 表示判断变量是否不为空
then
echo "the character you input is not a number,please retry."
else
echo $n
fi
[root@OBird shell]# sh if2.sh
please input a number:iiiiiiiiii
the character you input is not a number,please retry.
[root@OBird shell]# sh if2.sh
please input a number:90909090
90909090
[root@OBird shell]# sh -x if2.sh #实例分解
+ read -p ‘please input a number:‘ n
please input a number:oiu00909
++ echo oiu00909
++ sed ‘s/[0-9]//g‘
+ m=oiu
+ ‘[‘ -n oiu ‘]‘
+ echo ‘the character you input is not a number,please retry.‘
the character you input is not a number,please retry.
[root@OBird shell]# sh -x if2.sh #实例分解
+ read -p ‘please input a number:‘ n
please input a number:9909090909
++ sed ‘s/[0-9]//g‘
++ echo 9909090909
+ m=
+ ‘[‘ -n ‘‘ ‘]‘
+ echo 9909090909
9909090909
[root@OBird shell]# vim if2.sh
#!/bin/bash
read -p "please input a number:" n
m=`echo $n|sed ‘s/[0-9]//g‘`
if [ -z "$m" ] # -z 表示判断变量是否 为空,与-n 相反。
then
echo $n
else
echo "the character you input is not a number,please retry."
fi
[root@OBird shell]# sh -x if2.sh
+ read -p ‘please input a number:‘ n
please input a number:90909090
++ sed ‘s/[0-9]//g‘
++ echo 90909090
+ m=
+ ‘[‘ -z ‘‘ ‘]‘
+ echo 90909090
90909090
[root@OBird shell]# sh -x if2.sh
+ read -p ‘please input a number:‘ n
please input a number:jki0000
++ sed ‘s/[0-9]//g‘
++ echo jki0000
+ m=jki
+ ‘[‘ -z jki ‘]‘
+ echo ‘the character you input is not a number,please retry.‘
the character you input is not a number,please retry.
---------------------分割线-------------------------------
case 的用法:不是太常用。在启动脚本中用的比较多。
---------------------分割线-------------------------------
for循环:
[root@OBird shell]# vim for.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
done
[root@OBird shell]# sh for.sh
1
2
3
4
5
6
7
8
9
10
[root@OBird shell]# vim for.sh
#!/bin/bash
sum=0
for i in `seq 1 10`
do
sum=$[$sum+$i]
done
echo $sum
~
[root@OBird shell]# sh -x for.sh
+ sum=0
++ seq 1 10
+ for i in ‘`seq 1 10`‘
+ sum=1
+ for i in ‘`seq 1 10`‘
+ sum=3
+ for i in ‘`seq 1 10`‘
+ sum=6
+ for i in ‘`seq 1 10`‘
+ sum=10
+ for i in ‘`seq 1 10`‘
+ sum=15
+ for i in ‘`seq 1 10`‘
+ sum=21
+ for i in ‘`seq 1 10`‘
+ sum=28
+ for i in ‘`seq 1 10`‘
+ sum=36
+ for i in ‘`seq 1 10`‘
+ sum=45
+ for i in ‘`seq 1 10`‘
+ sum=55
+ echo 55
55
---------------------分割线-------------------------------
while 循环
[root@OBird ~]# vim while.sh
#!/bin/bash
n=0
while [ $n -le 10 ]
do
echo $n
n=$[$n+1]
done
~
[root@OBird ~]# sh -x while.sh #测试结果
+ n=0
+ ‘[‘ 0 -le 10 ‘]‘
+ echo 0
0
+ n=1
+ ‘[‘ 1 -le 10 ‘]‘
+ echo 1
1
+ n=2
+ ‘[‘ 2 -le 10 ‘]‘
+ echo 2
2
+ n=3
+ ‘[‘ 3 -le 10 ‘]‘
+ echo 3
3
+ n=4
+ ‘[‘ 4 -le 10 ‘]‘
+ echo 4
4
+ n=5
+ ‘[‘ 5 -le 10 ‘]‘
+ echo 5
5
+ n=6
+ ‘[‘ 6 -le 10 ‘]‘
+ echo 6
6
+ n=7
+ ‘[‘ 7 -le 10 ‘]‘
+ echo 7
7
+ n=8
+ ‘[‘ 8 -le 10 ‘]‘
+ echo 8
8
+ n=9
+ ‘[‘ 9 -le 10 ‘]‘
+ echo 9
9
+ n=10
+ ‘[‘ 10 -le 10 ‘]‘
+ echo 10
10
+ n=11
+ ‘[‘ 11 -le 10 ‘]‘
---------------------分割线-------------------------------
shell 中断继续退出:
[root@OBird shell]# vim for2.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
break
fi
echo $i
done
~
[root@OBird shell]# sh -x for2.sh
++ seq 1 10
+ for i in ‘`seq 1 10`‘
+ echo 1
1
+ ‘[‘ 1 -eq 4 ‘]‘
+ echo 1
1
+ for i in ‘`seq 1 10`‘
+ echo 2
2
+ ‘[‘ 2 -eq 4 ‘]‘
+ echo 2
2
+ for i in ‘`seq 1 10`‘
+ echo 3
3
+ ‘[‘ 3 -eq 4 ‘]‘
+ echo 3
3
+ for i in ‘`seq 1 10`‘
+ echo 4
4
+ ‘[‘ 4 -eq 4 ‘]‘
+ break #退出整个循环
[root@OBird shell]# vim for2.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
continue #结束本次循环
fi
echo $i
done
echo "for done"
~
[root@OBird shell]# sh for2.sh
1
1
2
2
3
3
4
5
5
6
6
7
7
8
8
9
9
10
10
for done
[root@OBird shell]# vim for2.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
exit
fi
echo $i
done
echo "for done"
[root@OBird shell]# sh for2.sh
1
1
2
2
3
3
4
#此处无for done ,直接退出整个shell
本文出自 “CBO#Boy_Linux之路” 博客,请务必保留此出处http://20151213start.blog.51cto.com/9472657/1890665
标签:shell 脚本介绍
原文地址:http://20151213start.blog.51cto.com/9472657/1890665