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

时间: 2024-08-02 22:17:30

Shell 脚本介绍的相关文章

20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量

20.1 Shell脚本介绍 1. shell是一种脚本语言 aming_linux blog.lishiming.net 2. 可以使用逻辑判断.循环等语法 3. 可以自定义函数 4. shell是系统命令的集合 5. shell脚本可以实现自动化运维,能大大增加我们的运维效率 20.2 Shell脚本结构和执行 1. 开头(首行)需要加: #!/bin/bash 2. 以#开头的行作为解释说明: 3. 脚本的名字以.sh结尾,用于区分这是一个shell脚本 4. 执行.sh脚本方法有两种:

shell脚本介绍,shell脚本结构和执行方式,date命令的用法,shell脚本中的变量简介

笔记内容: 20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量 笔记日期:2017-11-21 20.1 shell脚本介绍 Shell Script,Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的.但是它比Windows下的批处理更强大,比用其他编程程序编辑的程序效率更高,它使用了Linux/Unix下的命令

20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量

- 20.1 shell脚本介绍 - 20.2 shell脚本结构和执行 - 20.3 date命令用法 - 20.4 shell脚本中的变量 # 20.1 Shell脚本介绍 -  shell是一种脚本语言  关注aming_linux  blog.lishiming.net -  可以使用逻辑判断.循环等语法 -  可以自定义函数 -  shell是系统命令的集合 -  shell脚本可以实现自动化运维,能大大增加我们的运维效率 # 20.2 Shell脚本结构和执行 - 开头需要加#!/b

20.1-4 shell脚本介绍 shell脚本结构和执行 date命令用法 shell脚本中的变量

20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法%w 星期几 %W今年的第几周cal是显示日历的时间戳可以相互查询 20.4 shell脚本中的变量 原文地址:http://blog.51cto.com/13450039/2104595

20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.

20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量 原文地址:http://blog.51cto.com/12058686/2104654

20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.

20.1 Shell脚本介绍 1. shell是一种脚本语言  aming_linux  blog.lishiming.net 2. 可以使用逻辑判断.循环等语法 3. 可以自定义函数 4. shell是系统命令的集合 5. shell脚本可以实现自动化运维,能大大增加我们的运维效率 20.2 Shell脚本结构和执行 1. 开头(首行)需要加 : #!/bin/bash 2. 以#开头的行作为解释说明 : 3. 脚本的名字以.sh结尾,用于区分这是一个shell脚本 4. 执行.sh脚本方法有

shell脚本介绍,shell脚本结构和执行,date命令用法,shell脚本中的变量

Shell脚本介绍 shell是一种脚本语言 blog.lishiming.net(阿铭的博客,可以去里面找shell习题)可以使用逻辑判断.循环等语法可以自定义函数,减少重复代码shell是系统命令的集合shell脚本可以实现自动化运维,能大大增加我们的运维效率 Shell脚本结构和执行 开头需要加#!/bin/bash 以#开头的行作为解释说明 脚本的名字以.sh结尾,用于区分这是一个shell脚本写一个简简单的脚本#!/bin/bash#Linletao#2018-5-29echo llt

六十七、shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

一.shell脚本介绍 shell是一种脚本语言  aming_linux  blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 定义函数的目的:为了简化,为了减少重复的代码. shell是系统命令的集合 shell脚本可以实现自动化运维,能大大增加我们的运维效率 二.shell脚本结构和执行 脚本示例: #!/bin/bash echo "123" w ls 开头需要加#!/bin/bash,这个脚本在当前机器执行能识别里面的命令,换一台机器也许就不能

shell脚本介绍 shell脚本结构和执行 date命令用法 shell脚本中的变量

一.shell脚本介绍shell脚本要想写好,必须通过不断地去练习写才能写好,没有捷径要在我们拿到一个需求的时候有一个脚本的大致思路,想到需求怎么去实现shell脚本可以大大提高我们的工作效率二.shell脚本结构和执行[[email protected] ~]# mkdir shell //创建一个shell文件夹,存放实验的shell脚本[[email protected] ~]# cd shell/[[email protected] shell]# ls[[email protected

shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

shell脚本介绍 shell脚本结构和执行 开头需要加#!/bin/bash 因为有了#!/bin/bash文件头所以需要给01.sh文件执行权限 [[email protected] shell]# chmod a+x 01.sh 以#开头的行作为解释说明 脚本的名字以.sh结尾,用于区分这是一个shell脚本 执行方法有两种chmod +x 1.sh; ./1.shbash 1.sh 查看脚本执行过程 bash -x 1.sh 查看脚本是否语法错误 bash -n 1.sh date命令用