Shell脚本初初识学习_01

shell

1.1、shell认识

  • 什么是shell

    Shell(外壳) 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言又是一种程序设计语言

    Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

  • 什么是脚本

  • 脚本简单地说就是一条条的文字命令,这些文字命令是可以看到的(如可以用记事本打开查看、编辑)。

    常见的脚本: JavaScript(JS,前端)**,VBScript, ASP,JSP,PHP(后端),SQL(数据库操作语言),Perl,**Shell,python,Ruby,JavaFX,Lua等。

  • 为什么要学习shell?

    Shell属于内置的脚本

    程序开发的效率非常高,依赖于功能强大的命令可以迅速地完成开发任务(批处理)

    语法简单,代码写起来比较轻松,简单易学

  • 常见的shell

    在linux中有很多类型的shell,不同的shell具备不同的功能,shell还决定了脚本中函数的语法,Linux中默认的shell是**/bin/bash(重点)**,流行的shell有ash、bash、ksh、csh、zsh等,不同的shell都有自己的特点以及用途

    bash大多数Linux系统默认使用的shell,bash shell 是 Bourne shell 的一个免费版本,它是最早的 Unix shell,bash还有一个特点,可以通过help命令来查看帮助。包含的功能几乎可以涵盖shell所具有的功能,所以一般的shell脚本都会指定它为执行路径。

1.2、shell入门

  • 编写规范

    #!/bin/bash				[指定告知系统当前这个脚本要使用的shell解释器]
    	Shell相关指令
    
    文件命名规范:
    	文件名.sh				.sh是linux下bash shell 的默认后缀
    
    
  • 使用流程

    • 创建.sh文件 touch/vim
    • 编写shell代码
    • 执行shell脚本 脚本必须得有执行权限
  • 测试

    [root@tedu ~]# echo hello word
    hello word
    #创建一个shell脚本、这个sh的后缀名补齐任何决定性的作用、只是标识用的、人一看到这个就知道他是一个shell脚本、里面都是一大堆代码
    [root@tedu ~]# vim /root/hello.sh
    echo hello world
    [root@tedu ~]# /root/hello.sh #以绝对路径运行这个文件
    bash: /root/hello.sh: 权限不够
    [root@tedu ~]# ls -l /root/hello.sh
    -rw-r--r--. 1 root root 17 3月 7 10:57 /root/hello.sh
    [root@tedu ~]# chmod ugo+x /root/hello.sh
    简写
    [root@tedu ~]# chmod +x /root/hello.sh
    [root@tedu ~]# ls -l /root/hello.sh
    -rwxr-xr-x. 1 root root 17 3月 7 10:57 /root/hello.sh
    [root@tedu ~]# /root/hello.sh
    hello world
    [root@tedu ~]# vim /root/b
    [root@tedu ~]# chmod ugo+x /root/b
    [root@tedu ~]# b
    bash: b: 未找到命令...
    [root@tedu ~]# /root/b #必须使用绝对路径
    hello word
    
  • 编写一个能输出系统/root/sysinfo脚本

    输出当前红帽版本的信息

    输出当前系统的主机名

    [root@tedu ~]# vim /root/sysinfo.sh
    [root@tedu ~]# cat /root/sysinfo.sh
    #!/bin/bash
    cat /etc/redhat-release
    hostname
    ifconfig | head -2
    

1.3、输出重定向

屏幕输出的文本类型

标准输出:命令执行正常显示的结果

标准错误:命令执行出错或异常时显示的结果

将屏幕显示的信息保存到文件中

例子:

[root@tedu ~]# ls --help >a.txt
[root@tedu ~]# echo hello world >>b.txt

重定向输出

>:只收集前面命令正确的信息

2>:只收集前面命令错误的信息

&>:正确和错误都收集

[root@tedu ~]# echo 123 > c.txt
[root@tedu ~]# cat c.txt
123
[root@tedu ~]# cat d.txt
cat: d.txt: 没有那个文件或目录
[root@tedu ~]# cat c.txt d.txt
123
cat: d.txt: 没有那个文件或目录
[root@tedu ~]# cat c.txt d.txt >e.txt
cat: d.txt: 没有那个文件或目录
[root@tedu ~]# cat e.txt
123
[root@tedu ~]# cat c.txt d.txt 2>f.txt
123
[root@tedu ~]# cat f.txt
cat: d.txt: 没有那个文件或目录
[root@tedu ~]# cat c.txt d.txt &>d.txt
[root@tedu ~]# cat d.txt
123
cat: d.txt:输入文件是输出文件
  • 案例:写一个创建用户并设设置密码

    [root@tedu ~]# vim /root/user1.sh
    #!/bin/bash
    useradd xiaofang
    echo 123 | passwd --stdin xiaofang
    [root@tedu ~]# chmod ugo+x user1.sh
    [root@tedu ~]# /root/user1.sh
    
  • 优化

    [root@tedu ~]# vim /root/user2.sh
    #!/bin/bash
    useradd xiaohua
    echo 用户xiaohua创建成功
    echo 123 | passwd --stdin xiaohua
    echo 用户xiaohua密码修改成功
    [root@tedu ~]# chmod +x user2.sh
    [root@tedu ~]# ls -l user2.sh
    -rwxr-xr-x. 1 root root 130 3月 7 15:59 user2.sh
    [root@tedu ~]# /root/user2.sh
    用户xiaohua创建成功
    更改用户 xiaohua 的密码 。
    passwd:所有的身份验证令牌已经成功更新。
    用户xiaohua密码修改成功
    
  • 再次优化

    [root@tedu ~]# userdel -r xiaohua
    [root@tedu ~]# vim /root/user2.sh
    #!/bin/bash
    useradd xiaohua
    echo 用户xiaohua创建成功
    echo 123 | passwd --stdin xiaohua &>/root/logo.txt
    echo 用户xiaohua密码修改成功
    [root@tedu ~]# /root/user2.sh
    useradd:用户“xiaohua”已存在
    用户xiaohua创建成功
    用户xiaohua密码修改成功
    
  • /dev/null 黑洞设备,专门收集不要的输出信息

    [root@tedu ~]# vim user
    user1.sh user2.sh user.sh
    [root@tedu ~]# vim user3.sh
    #!/bin/bash
    useradd xiaolong
    echo 用户xiaolong创建成功
    echo 123 | passwd --stdin xiaolong &>/dev/null
    echo 用户xiaolong的密码修改成功
    [root@tedu ~]# chmod +x user3.sh
    [root@tedu ~]# /root/user3.sh
    用户xiaolong创建成功
    用户xiaolong的密码修改成功
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值