Shell 脚本介绍
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell 脚本介绍相关的知识,希望对你有一定的参考价值。
1.当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
2.使用条件语句时,变量是必不可少的
3.引用某个命令的结果时,用变量替代
4.写和用户交互的脚本时,变量也是必不可少的
内置变量 $0, $1, $2…
数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]
测试样例:写一个交互脚本:
[[email protected] shell]# vim 2.sh
#!/bin/bash
read -p "please input anumber:" number
echo $number
[[email protected] shell]# sh 2.sh #输入什么就输出什么
please input anumber:99999
99999
[[email protected] shell]# vim 2.sh
#!/bin/bash
read -t 3 -p "please input anumber:" number #“-t 3”加入等待3秒
echo $number
[[email protected] shell]# sh 2.sh #超时会跳出
please input anumber:
[[email protected] shell]#
---------------------分割线------------------------------
[[email protected] shell]# vim 3.sh
#!/bin/bash
##
##
echo $1 $2 $0 $3
[[email protected] shell]# sh 3.sh
3.sh
[[email protected] shell]# vim 3.sh
#!/bin/bash
##
##
echo "\$1=$1"
echo "\$2=$2"
echo "\$0=$0"
echo "\$3=$3"
[[email protected] shell]# sh 3.sh
$1=
$2=
$0=3.sh
$3=
[[email protected] shell]# sh 3.sh 11 22 33
$1=11
$2=22
$0=3.sh
$3=33
[[email protected] shell]# sh 3.sh 11 22 #写两个值$3 是没有的
$1=11
$2=22
$0=3.sh
$3=
数学运算
[[email protected] shell]# a=1;b=2
[[email protected] shell]# c=$a+$b
[[email protected] shell]# echo $c
1+2
[[email protected] shell]# c=$[$a+$b] #数学运算应该这样写
[[email protected] shell]# echo $c
3
------------------------分割线------------------------------
if 语句
[[email protected] shell]# vim if.sh
#!/bin/bash
a=5
if [ $a -gt 3 ] #-gt 大于 < 小于 -lt ==等于 -eq != 不等于 -ne >= 大于等于 -ge <= 小于等于 -le
then
echo "a>3"
fi
[[email protected] shell]# sh if.sh
a>3
[[email protected] shell]# sh -x if.sh # -x 查看详细过程
+ a=5
+ ‘[‘ 5 -gt 3 ‘]‘
+ echo ‘a>3‘
a>3
[[email protected] shell]# vim if.sh
#!/bin/bash
a=5
if [ $a -gt 10 ] #如果
then
echo "a>10"
else #否则
echo "a<=10"
fi
~
[[email protected] shell]# sh if.sh
a<=10
[[email protected] 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
[[email protected] 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 ] 判断文件是否可执行
[[email protected] shell]# if [ -f 1.txt ]; then echo ok; fi
[[email protected] shell]# touch 1.txt
[[email protected] shell]# if [ -f 1.txt ]; then echo ok; fi #判断是否存在1.txt
ok
[[email protected] shell]# if [ -r 1.txt ]; then echo ok; fi # -r 判断文件是否可读
ok
[[email protected] shell]# if [ -d /tmp/ ]; then echo ok; fi #判断是否存在 目录
ok
[[email protected] 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
[[email protected] shell]# sh if2.sh
please input a number:iiiiiiiiii
the character you input is not a number,please retry.
[[email protected] shell]# sh if2.sh
please input a number:90909090
90909090
[[email protected] 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.
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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循环:
[[email protected] shell]# vim for.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
done
[[email protected] shell]# sh for.sh
1
2
3
4
5
6
7
8
9
10
[[email protected] shell]# vim for.sh
#!/bin/bash
sum=0
for i in `seq 1 10`
do
sum=$[$sum+$i]
done
echo $sum
~
[[email protected] 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 循环
[[email protected] ~]# vim while.sh
#!/bin/bash
n=0
while [ $n -le 10 ]
do
echo $n
n=$[$n+1]
done
~
[[email protected] ~]# 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 中断继续退出:
[[email protected] 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
~
[[email protected] 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 #退出整个循环
[[email protected] 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"
~
[[email protected] 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
[[email protected] 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"
[[email protected] 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 脚本介绍的主要内容,如果未能解决你的问题,请参考以下文章
shell脚本介绍,shell脚本结构和执行,date命令用法,shell脚本中的变量
shell脚本介绍shell脚本结构和执行date命令用法shell脚本中的变量
shell脚本介绍,shell脚本结构和执行方式,date命令的用法,shell脚本中的变量简介