Shell脚本学习

shell是人机交互的翻译

注意的是,shell和Linux内核合在一起才是Linux。

以上的解释只是shell作为命令语言来解释或者是执行用户输入的命令或者自动解释和执行预先设定好的一连串的指令。与此同时,shell还可以作为程序设计语言,它能够定义各种参数,并提供了和许多高级语言才有的控制结构,如条件和训循环。同时,在排序算法中,Shell是希尔排序的名称。

shell 提供了你和操作系统之间的通讯方式。

 

Shell命令由两种工作方式,

一种是交互式,写一句命令,执行一句命令

一种是批处理,一次执行多个命令,先把命令写好,然后在执行

 举个例子:

[root@hadoop102 ~]# vim first.sh


#!/bin/bash
#The first shell script by isea_you
pwd
ls -l

其中#!表示使用哪种shell解释器来执行脚本,#表示对这个脚本进行说明

保存并退出,之后执行该脚本,结果如下:

[root@hadoop102 ~]# bash ./first.sh 
/root
总用量 96
-rw-------. 1 root root  1186 10月 16 22:18 anaconda-ks.cfg
-rw-r--r--. 1 root root    59 10月 18 23:49 first.sh
-rw-r--r--. 1 root root 38951 10月 16 22:17 install.log
-rw-r--r--. 1 root root  8481 10月 16 22:15 install.log.syslog
drwxr-xr-x. 2 root root  4096 10月 16 22:24 公共的
drwxr-xr-x. 2 root root  4096 10月 16 22:24 模板
drwxr-xr-x. 2 root root  4096 10月 16 22:24 视频
drwxr-xr-x. 2 root root  4096 10月 16 22:24 图片
drwxr-xr-x. 2 root root  4096 10月 16 22:24 文档
drwxr-xr-x. 2 root root  4096 10月 16 22:24 下载
drwxr-xr-x. 2 root root  4096 10月 16 22:24 音乐
drwxr-xr-x. 2 root root  4096 10月 16 22:24 桌面

Shell解析器

查看Linux下提供的shell解释器有哪些:

[root@hadoop102 shell]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

sh的本质也是bash,证明如下:

[root@hadoop102 bin]# ll /bin|grep bash
-rwxr-xr-x. 1 root root 941880 5月  11 2016 bash
lrwxrwxrwx. 1 root root      4 10月 16 22:10 sh -> bash

查看当前的Linux使用的是哪个解释器:

[root@hadoop102 bin]# echo $shell

[root@hadoop102 bin]# echo $SHELL
/bin/bash

如何执行脚本:

1,使用bash + 脚本路径(相对路径或者是绝对路径)

2,直接执行该脚本文件(./ 文件名) 前提是当前该脚本必须有可执行的权限。

第一种执行的方式,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限;第二种执行脚本需要自己执行,所以需要执行的权限。

例子:写一个脚本,并执行该脚本:

[root@hadoop102 shell]# cat helloworld.sh 
#!/bin/bash
#print the helloworld
echo hello world
[root@hadoop102 shell]# 


[root@hadoop102 shell]# sh helloworld.sh 
hello world
[root@hadoop102 shell]# bash helloworld.sh 
hello world
[root@hadoop102 shell]# cd 
[root@hadoop102 ~]# bash /root/shell/helloworld.sh 
hello world
[root@hadoop102 ~]# cd shell/
[root@hadoop102 shell]# ll
总用量 12
-rwxr-xr-x. 1 root root 30 10月 24 09:22 a.sh
-rw-r--r--. 1 root root 43 10月 24 09:03 helloworld_python.sh
-rwxr-xr-x. 1 root root 51 10月 24 09:01 helloworld.sh
[root@hadoop102 shell]# ./helloworld.sh 
hello world
[root@hadoop102 shell]# 

Shell变量

系统变量,直接输入 set ,可以得到所有的变量,系统变量比如下面的这些;

[root@hadoop102 shell]# echo $HOME
/root
[root@hadoop102 shell]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@hadoop102 shell]# echo $SHELL
/bin/bash

用户自定义变量,需要注意的几点:

定义变量:变量=值  (等号两侧不能有空格)

获取该变量的值并输出: echo $变量

撤销变量:unset 变量

静态变量的声明:readonly 变量,注意不能unset 静态变量

bash中,所有的类型默认都是字符串类型,无法直接进行数值的运算。

[root@hadoop102 shell]# A=1
[root@hadoop102 shell]# echo $A
1
[root@hadoop102 shell]# readonly B=2
[root@hadoop102 shell]# echo $B
2
[root@hadoop102 shell]# B=3
-bash: B: readonly variable

无法进行数值的直接运算
[root@hadoop102 shell]# C=1+2
[root@hadoop102 shell]# echo $C
1+2

如果将一个带空格的字符串赋值给变量:
[root@hadoop102 shell]# D=i love you
-bash: love: command not found

必须加上双引号,或者是单引号
[root@hadoop102 shell]# D="i love you"
[root@hadoop102 shell]# echo $D
i love you

[root@hadoop102 shell]# D='i love you'
[root@hadoop102 shell]# echo $D
i love you

将变量升级为全局的变量,这样的话其他的shell就能够使用,但是如果我们重新连接一个终端的话,该变量将不会在生效:

[root@hadoop102 shell]# echo $A
1

[root@hadoop102 shell]# cat helloworld.sh 
#!/bin/bash
#print the helloworld
echo hello world
echo $A


[root@hadoop102 shell]# sh helloworld.sh 
hello world

[root@hadoop102 shell]# export A=1
[root@hadoop102 shell]# sh helloworld.sh 
hello world
1
[root@hadoop102 shell]# 

特殊变量:

$n  获取执行脚本时候,传入的参数,n表示第几个参数

$#  获取执行脚本的时候,传入的所有参数的个数

$*  代表命令行中所有的参数,$*把所有的参数看成一个整体

$@   代表命令行中所有的参数,不过$@把每个参数区分对待

$?   最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了

[root@hadoop102 ~]# vim a.sh

#!/bin/bash
for i in "$*"
do
        echo $i
done

echo "--------------------------"
for i in $*
do
        echo $i
done

echo "--------------------------"

for i in $@
do
        echo $i
done
echo "--------------------------"

for i in "$@"
do
        echo $i
done


[root@hadoop102 ~]# ./a.sh a b c d
a b c d
--------------------------
a
b
c
d
--------------------------
a
b
c
d
--------------------------
a
b
c
d
[root@hadoop102 ~]# vim a.sh

#!/bin/bash
for i in '$*'
do
        echo $i
done


[root@hadoop102 ~]# ./a.sh a b c d
$*

 

运算符号

(1)“$((运算式))”或“$[运算式]”

(2)expr  + , - , *,  /,  %    加,减,乘,除,取余

[root@hadoop102 shell]# A=$[(2+3)*4]
[root@hadoop102 shell]# echo $A
20

[root@hadoop102 ~]# vim a.sh
#!/bin/bash
echo $1 $2 $3
echo $#




[root@hadoop102 ~]# ./a.sh 1 2 3
-bash: ./a.sh: 权限不够
[root@hadoop102 ~]# chmod +x a.sh
[root@hadoop102 ~]# ./a.sh 1 2 3 4 5 6
1 2 3
6

条件判断:

基本语法: [ condition ] 注意condition后面都是有空格的

例子:

[root@hadoop102 shell]# [  ]
[root@hadoop102 shell]# echo $?
127
[root@hadoop102 shell]# [ isea_you ]
[root@hadoop102 shell]# echo $?
0
[root@hadoop102 shell]# 

(1)两个整数之间比较

= 字符串比较

-lt 小于(less than)                  -le 小于等于(less equal)

-eq 等于(equal)                     -gt 大于(greater than)

-ge 大于等于(greater equal)   -ne 不等于(Not equal)

[root@hadoop102 ~]# vim ari.sh

#!/bin/bash
if [ $1 -gt $2 ]
then
        echo "succeess"
else
        echo "fail"
fi


[root@hadoop102 ~]# chmod +x ari.sh 
[root@hadoop102 ~]# ./ari.sh 
succeess
[root@hadoop102 ~]# ./ari.sh 1 2
fail
[root@hadoop102 ~]# ./ari.sh 4 2
succeess

 

(2)按照文件权限进行判断

-r 有读的权限(read)              -w 有写的权限(write)

-x 有执行的权限(execute)

(3)按照文件类型进行判断

-f 文件存在并且是一个常规的文件(file)

-e 文件存在(existence)          -d 文件存在并是一个目录(directory)

[root@hadoop102 shell]# [ 23 -ge 22 ]
[root@hadoop102 shell]# echo $?
0
[root@hadoop102 shell]# [ -w helloword.sh ]
[root@hadoop102 shell]# echo $?
1
[root@hadoop102 shell]# [ -w helloworld.sh ]
[root@hadoop102 shell]# echo $?
0
[root@hadoop102 shell]# [ -e /root/shell/helloworld.sh ]
[root@hadoop102 shell]# echo $?
0

流程控制:

例子:

if 的后面要有空格。 

[root@hadoop102 shell]# sh if.sh
if.sh: line 2: [: -eq: unary operator expected
if.sh: line 5: [: -eq: unary operator expected
[root@hadoop102 shell]# sh if.sh 1 
isea_you
[root@hadoop102 shell]# sh if.sh 2
ifseayou

for循环

[root@hadoop102 shell]# vim for.sh
[root@hadoop102 shell]# sh for.sh
5050

while循环

 

[root@hadoop102 shell]# vim while.sh
[root@hadoop102 shell]# sh while.sh
5050
[root@hadoop102 shell]# vim case.sh
#!/bin/bash
case $1 in
        "1")
                echo 111
        ;;
        "2")
                echo 222
        ;;
        *)
                echo other
        ;;
esac



[root@hadoop102 shell]# chmod +x case.sh
[root@hadoop102 shell]# ./case.sh 
other
[root@hadoop102 shell]# ./case.sh 1
111
[root@hadoop102 shell]# ./case.sh 2
222

 

Read读取控制台输入

选项:

-p:指定读取值时的提示符;

-t:指定读取值时等待的时间(秒)。

参数

       变量:指定读取值的变量名

 

[root@hadoop102 shell]# vim read.sh

#!/bin/bash
read -t 7 -p "Enter your name in 7 seconds:" NAME
echo $NAME

[root@hadoop102 shell]# sh read.sh 
Enter your name in 7 seconds:isea_you
isea_you

系统函数

basename基本语法

basename [string / pathname] [suffix]   (功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。

选项:suffix为后缀,如果suffix被指定了,basename会将pathname或string中的suffix去掉。

dirname基本语法

       dirname 文件绝对路径   (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))

[root@hadoop102 shell]# basename /root/shell/if.sh
if.sh
[root@hadoop102 shell]# dirname /root/shell/if.sh 
/root/shell

自定义函数

 

[root@hadoop103 shell]# function f1(){
> echo $1 $2
> }
[root@hadoop103 shell]# f1 1 2
1 2

(1)必须在调用函数地方之前,先声明函数,shell脚本是逐行运行。不会像其它语言一样先编译。

(2)函数返回值,只能通过$?系统变量获得,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。return后跟数值n(0-255)

[root@hadoop103 shell]# vim f1.sh

#!/bin/bash
function f1(){
        echo $1 $2
}
f1 a b

[root@hadoop103 shell]# sh f1.sh
a b

-----------------------------------

[root@hadoop103 shell]# vim f1.sh

#!/bin/bash
f1(){
        echo $1 $2
}
f1 a b

[root@hadoop103 shell]# sh f1.sh
a b

---------------------------------

[root@hadoop103 shell]# vim f1.sh

#!/bin/bash
function f1
{#注意此处的{必须换行
        echo $1 $2
}
f1 a b

[root@hadoop103 shell]# sh f1.sh
a b

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值