SHELL编程基础

目录

1. 概述

2 Shell 脚本应用场景

3 Linux 中的 shell 类型

4 脚本实例

2. shell 脚本编程基础

1 脚本执行

2 管道符号 |

3 交互式硬件设备


1. 概述

shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务,在用户和内核之间充当翻译官的角色
shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说的 shell 通常都是指 shell 脚本,shell 和 shell script 是两个不同的概念。

2 Shell 脚本应用场景

  • 重复性操作
  • 交互性任务
  • 批量事务处理
  • 服务运行状态监控
  • 定时任务执行

3 Linux 中的 shell 类型

[root@c7-1 ~]#cat /etc/shells
/bin/sh			#/bin/sh 是 bash 命令的软链接(已经被 /bin/bash 所替换)
/bin/bash		#基准于 GNU 的框架下发展出的 shell
/usr/bin/sh		#己经被 bash 所替换
/usr/bin/bash	#centos 和 redhat 系统默认使用 bash shell
/bin/tcsh		#csh 的增强版,与 csh 完全兼容,提供更多的功能
/bin/csh		#已经被 /bin/bash 所替换(整合 c shell,提供更多的功能)
/sbin/nologin	#奇怪的 shel1,这个 shell 可以让用户无法登录主机

4 脚本实例

[root@c7-1 ~]#curl -s http://47.117.130.238/hello.sh
# ------------------------------------------
# Filename: hello.sh
# Version: 1.0
# Date: 2021/06/06
# Author: gongboyi
# Email: 1520509800@qq.com
# Website: https://www.cnblogs.com/shenyuanhaojie/
# Description: This is the first script
# Copyright: 2021 zhou
# License: GPL
# ------------------------------------------
echo "hello world!"

[root@c7-1 ~]#curl -s http://47.117.130.238/hello.sh|bash
hello world!

2. shell 脚本编程基础

1 脚本执行

相对路径执行( ./script.sh )在脚本当前目录,脚本需要执行权限
绝对路径执行( /PATH/to/script.sh )无需在脚本目录,脚本需要执行权限
bash 执行( bash /PATH/to/script.sh )bash 后可跟绝对路径和相对路径,脚本无需执行权限
source 执行( source /PATH/to/script.sh )source 后可跟绝对路径和相对路径,脚本无需执行权限
bash < script.sh 或 cat script.sh | bash

示例

[root@aliyunhost01html]#./hello.sh 
hello world!
[root@aliyunhost01html]#/var/www/html/1.sh 
hello world
[root@aliyunhost01html]#bash /var/www/html/1.sh
hello world
[root@aliyunhost01html]#source /var/www/html/1.sh
hello world
[root@aliyunhost01html]#bash < /var/www/html/1.sh
hello world
[root@aliyunhost01html]#cat /var/www/html/1.sh | bash
hello world

2 管道符号 |

将左侧命令的输出结果作为右侧命令的处理对象

[root@c7-1 ~]#grep "/sbin/nologin" /etc/passwd | awk -F: '{print $1,$7}'
bin /sbin/nologin
daemon /sbin/nologin
adm /sbin/nologin
lp /sbin/nologin
mail /sbin/nologin
......

3 交互式硬件设备

标准输入:从该设备接收用户输入的数据
标准输出:通过该设备向用户输出数据
标准错误:通过该设备报告执行出错信息
[root@c7-1 ~]#ll /dev/std*
lrwxrwxrwx 1 root root 15 9月   2 10:02 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 9月   2 10:02 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 9月   2 10:02 /dev/stdout -> /proc/self/fd/1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值