Shell笔记学习原文连接尚硅谷

Shell概述

Shell是命令解释器,然后调用操作系统内核
Centos的解析器bash

1)Linux提供的Shell解析器有:
[atguigu@hadoop101 ~]$ cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

Shell脚本格式

脚本以#!/bin/bash开头

[atguigu@hadoop101 datas]$ touch helloworld.sh
[atguigu@hadoop101 datas]$ vi helloworld.sh

在helloworld.sh中输入如下内容
#!/bin/bash
echo "helloworld"

脚本执行方式
第一种 采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)

[atguigu@hadoop101 datas]$ sh helloworld.sh 
Helloworld

[atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh 
helloworld

[atguigu@hadoop101 datas]$ bash helloworld.sh 
Helloworld

[atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh 
Helloworld

第二种 采用输入脚本的绝对路径或相对路径执行脚本**(必须具有可执行权限+x)**

[atguigu@hadoop101 datas]$ chmod +x helloworld.sh

[atguigu@hadoop101 datas]$ ./helloworld.sh 
Helloworld

[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh 
Helloworld

Shell变量

定义变量 直接令值 容错性和灵活性很高
撤销变量:unset变量
声明静态变量:randomly
在bash中,变量默认类型为字符串类型,无法直接数值运算

[atguigu@hadoop101 datas]$ A=8
[atguigu@hadoop101 datas]$ echo $A
8

[atguigu@hadoop102 ~]$ C=1+2
[atguigu@hadoop102 ~]$ echo $C
1+2

[atguigu@hadoop102 ~]$ D=I love banzhang
-bash: world: command not found
[atguigu@hadoop102 ~]$ D="I love banzhang"
[atguigu@hadoop102 ~]$ echo $D
I love banzhang

$n (功能描述:n为数字,$0代表该脚本名称,$1-9 代 表 第 一 到 第 九 个 参 数 , 十 以 上 的 参 数 , 十 以 上 的 参 数 需 要 用 大 括 号 包 含 , 如 9代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如9{10})

$# (功能描述:获取所有输入参数个数,常用于循环)

∗ ( 功 能 描 述 : 这 个 变 量 代 表 命 令 行 中 所 有 的 参 数 , * (功能描述:这个变量代表命令行中所有的参数,*把所有的参数看成一个整体)

@ ( 功 能 描 述 : 这 个 变 量 也 代 表 命 令 行 中 所 有 的 参 数 , 不 过 @ (功能描述:这个变量也代表命令行中所有的参数,不过@@把每个参数区分对待)

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

运算符和条件判断

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

-lt 小于(less than) -le 小于等于(less equal)
-eq 等于(equal) -gt 大于(greater than)
-ge 大于等于(greater equal) -ne 不等于(Not equal)

-r 有读的权限(read) -w 有写的权限(write)
-x 有执行的权限(execute)

流程控制

if判断

[atguigu@hadoop101 datas]$ touch if.sh
[atguigu@hadoop101 datas]$ vim if.sh

#!/bin/bash

if [ $1 -eq "1" ]
then
        echo "banzhang zhen shuai"
elif [ $1 -eq "2" ]
then
        echo "cls zhen mei"
fi

[atguigu@hadoop101 datas]$ chmod 777 if.sh 
[atguigu@hadoop101 datas]$ ./if.sh 1
banzhang zhen shuai

case语句

[atguigu@hadoop101 datas]$ touch case.sh
[atguigu@hadoop101 datas]$ vim case.sh

!/bin/bash

case $1 in
"1")
        echo "banzhang"
;;

"2")
        echo "cls"
;;
*)
        echo "renyao"
;;
esac

[atguigu@hadoop101 datas]$ chmod 777 case.sh
[atguigu@hadoop101 datas]$ ./case.sh 1
1

for循环

[atguigu@hadoop101 datas]$ touch for1.sh
[atguigu@hadoop101 datas]$ vim for1.sh

#!/bin/bash

s=0
for((i=0;i<=100;i++))
do
        s=$[$s+$i]
done
echo $s

[atguigu@hadoop101 datas]$ chmod 777 for1.sh 
[atguigu@hadoop101 datas]$ ./for1.sh 
“5050”

while循环

[atguigu@hadoop101 datas]$ touch while.sh
[atguigu@hadoop101 datas]$ vim while.sh

#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done

echo $s

[atguigu@hadoop101 datas]$ chmod 777 while.sh 
[atguigu@hadoop101 datas]$ ./while.sh 
5050

函数

basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。

[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt 
banzhang.txt
[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt
banzhang

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

[atguigu@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt 
/home/atguigu

Shell工具

cut

cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。

选项参数 功能
-f 列号,提取第几列
-d 分隔符,按照指定分隔符分割列
-c 指定具体的字符

[atguigu@hadoop101 datas]$ touch cut.txt
[atguigu@hadoop101 datas]$ vim cut.txt
dong shen
guan zhen
wo  wo
lai  lai
le  le

[atguigu@hadoop101 datas]$ cut -d " " -f 2,3 cut.txt 
shen
zhen
wo
lai
le

awk

sort


版权声明:本文为qq_43016111原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。