shell编程与变量——编写你的第一个shell脚本

发布时间:2020-07-05 15:53:52 作者:wx5d3fd1efe40e3
来源:网络 阅读:273

首先了解shell的作用:

1、命令解释器:“翻译官”介于系统内核与用户之间,负责解释命令行

2、用户的登陆,登陆后默认使用的shell程序,一般为/bin/bash。不同shell的内部指令、运行环境会有所区别

[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/bin/nologin
......

编写第一个shell脚本:

1、编写脚本代码,完善脚本构成

使用vi编辑器

所有脚本扩展名为.sh

每一天linux命令,按执行顺序依次编写

养成良好习惯:开头第一件事脚本声明:#!/bin/bash

[root@localhost ~]# vi first.sh
#!/bin/bash
# this is first script
cd /boot
pwd
1s -lh vml*

shell编程与变量——编写你的第一个shell脚本

2、赋予可执行权限

root@localhost ~]# chmod +x first.sh


3、执行脚本文件

方法一:脚本文件路径(绝对路径与相对路径)[此方式脚本文件必须拥有可执行权限]

[root@localhost ~]# ./first.sh

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

方法二: sh脚本文件路径(此方式不会改变所在环境位置)

[root@localhost -]# sh /first.sh

shell编程与变量——编写你的第一个shell脚本

方法三: source 脚本文件路径

[root@localhost -]# source /first.sh

shell编程与变量——编写你的第一个shell脚本

方法四:".+空格"脚本文件路径

[root@localhost ~]# . first.sh

shell编程与变量——编写你的第一个shell脚本

重点符号介绍——重定向与管道

交互式硬件设备

标准输入:从该设备接收用户输入的数据:

标准输出通过该设备向用户输出数据

标准错误:通过该设备报告执行出错信息

shell编程与变量——编写你的第一个shell脚本shell编程与变量——编写你的第一个shell脚本shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

> 重定向:将输出结果保存到指定文件

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

| 管道符号: 将左侧的命令输出结果,作为右侧命令的处理对象

cmd1 | cmd2 [... | cmdn]


shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

shell变量的作用及类型

1、变量的作用

2、 变量的类型


自定义变量

定义一个新的变量:

变量名以字母或下划线开头、区分大小写,建议全大写

变量名=变量值

查看变量的值

echo $变量名

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

赋值时使用引号

双引号:允许通过$符号引用其他变量值
单引号:禁止引用其他变量值。$视为普通字符
反撇号:命令替换,提取命令执行后的输出结果

shell编程与变量——编写你的第一个shell脚本

从键盘输入内容为变量赋值

read [-p “提示信息”] 变量名

设置变量的作用范围

格式1:export 变量名 ...
格式2:enport 变量名+变量值 ...

整数变量的运算

expr 变量1 运算符 变量2 [运算符 变量3] ...
常用运算符
加法运算:+
减法运算:-
乘法运算:\*
除法运算:/
求模(取余)运算:%

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

特殊的shell变量

环境变量

常见的环境变量

shell编程与变量——编写你的第一个shell脚本

位置变量

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

预定义变量

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

shell编程与变量——编写你的第一个shell脚本

推荐阅读:
  1. shell脚本规范与变量运用
  2. shell编程之变量

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell脚本 编程 变量 he l脚本

上一篇:svn的日志文件丢失恢复

下一篇:Hive 对列通配查询

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》