大家好,欢迎来到IT知识分享网。
shell脚本语言
1、shell的概述
shell 是一种脚本语言
脚本:本质是一个文件,文件里面存放的是 特定格式的指令,系统可以使用脚本解析器 翻译或解析 指令 并执行(它不需要编译)
shell 既是应用程序 又是一种脚本语言(应用程序 解析 脚本语言)
shell命令解析器:
系统提供 shell命令解析器: sh ash bash
查看自己linux系统的默认解析:echo $SHELL
shell脚本是一种脚本语言,我们只需使用任意文本编辑器,按照语法编写相应程序,增加可执行权限,即可在安装shell命令解释器的环境下执行
2、脚本的调用形式
打开终端时系统自动调用:/etc/profile 或 ~/.bashrc
/etc/profile
此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,系统的公共环境变量在这里设置
开机自启动的程序,一般也在这里设置
~/.bashrc
用户自己的家目录中的.bashrc
登录时会自动调用,打开任意终端时也会自动调用
这个文件一般设置与个人用户有关的环境变量,如交叉编译器的路径等等
用户手动调用:用户实现的脚本
3、shell语法初识
3.1、定义以开头:#!/bin/bash
#!用来声明脚本由什么shell解释,否则使用默认shell
3.2、单个”#”号代表注释当前行
第一步:编写脚本文件
第二步:加上可执行权限
chmod +x xxxx.sh
第三步:运行
三种执行方式 (./xxx.sh bash xxx.sh . xxx.sh)
三种执行方式的不同点(./xxx.sh bash xxx.sh . xxx.sh)
./xxx.sh :先按照 文件中#!指定的解析器解析
如果#!指定指定的解析器不存在 才会使用系统默认的解析器
bash xxx.sh:指明先用bash解析器解析
如果bash不存在 才会使用默认解析器
. xxx.sh 直接使用默认解析器解析(不会执行第一行的#!指定的解析器)但是第一行还是要写的
三种执行情况:
打开终端就会有以后个解释器,我们称为当前解释器
我们指定解析器的时候(使用 ./xxx.sh 或 bash xxx.sh)时会创建一个子shell解析 脚本
注意:windows下 写脚本 在linux下执行 注意
执行结果:
将windows文件 转换成 unix文件
方法一:dos2unix 如果没有该插件 需要安装
sudo apt-get install dos2unix
dos2unix shell脚本
转换成功就可以执行运行
方法二:
需要用vi打开脚本,在最后一行模式下执行
:set ff=unix
4、变量
定义变量
变量名=变量值
如:num=10
引用变量
$变量名
unset :清除变量值
运行结果:
从键盘获取值read
运行结果:
案例:
在一行上显示和添加提示 需要加上-p
运行结果:
案例:读取多个值
运行结果:
案例只读变量:
运行结果:
查看环境变量:env
导出环境变量 作用:(让其他shell脚本识别该变量,设为全局变量)
source 脚本文件
source命令用法:
source FileName
作用:在当前bash环境下读取并执行FileName中的命令。
注:该命令通常用命令“.”来替代。
如:source .bash_rc 与 . .bash_rc 是等效的。
注意:source命令与shell scripts的区别是,
source在当前bash环境下执行命令,而scripts是启动一个子shell来执行命令。这样如果把设置环境变量(或alias等等)的命令写进scripts中,就只会影响子shell,无法改变当前的BASH,所以通过文件(命令列)设置环境变量时,要用source 命令。
06_sh.sh
#!/bin/bash
expor DATA=250
用source 是文件生效
使用 env可以查看到环境变量中已经有 DATA
可以在终端直接中读取:
在其他sh脚本读取:
运行结果:
注意事项:
1、变量名只能包含英文字母下划线,不能以数字开头
1_num=10 错误
num_1=20 正确
2、等号两边不能直接接空格符,若变量中本身就包含了空格,则整个字符串都要用双引号、或单引号括起来
3、双引号 单引号的区别
双引号:可以解析变量的值
单引号:不能解析变量的值
运行结果:
如果想在PATH变量中 追加一个路径写法如下:(重要!!!!)
export PATH=$PATH:/需要添加的路径
5、预设变量
shell直接提供无需定义的变量
案例:
运行结果:
脚本标量的特殊用法
加-e转义 才起换行作用
()由子shell 完成
{}由当前的shell执行
6、变量的扩展
6.1、判断变量是否存在
6.2、字符串的操作
7、条件测试
test命令:用于测试字符串、文件状态和数字
test命令有两种格式:
test condition 或[ condition ]
使用方括号时,要注意在条件两边加上空格。
7.1、文件测试
7.2、字符串测试
7.3、数值测试
7.4、符合语句测试
8、控制语句
8.1、if控制语句
格式一:
if [条件1]; then
执行第一段程序
else
执行第二段程序
fi
格式二:
if [条件1]; then
执行第一段程序
elif [条件2];then
执行第二段程序
else
执行第三段程序
fi
案例:
案例:判断当前路径下有没有文件夹 有就进入创建文件 没有 就创建文件夹 再进入创建文件
运行结果:
案例:
运行结果:
8.2、case
8.3、for循环语句
案例:
案例:扫描当前文件
8.4、while
8.5、until
8.6、break continue
9、函数
所有函数在使用前必须定义,必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用
案例:求最值
案例:函数分文件
fun.sh
24_sh.sh
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/26271.html
![shell脚本语言(超全超详细)[通俗易懂]插图1 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/c1a99dc7720b434e96edad5bfcf271b2.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图3 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/913cba359c69444faca61c838b78c545.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图5 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/eee30501d9a349c785c1a3f335c06ad2.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图7 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/c2b822a3e06f4b40b483d340e58e367c.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图9 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/62a40fcb186546e3840687c7d3e4b690.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图11 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/a6be1ff086ce4c619b8fe5dea95b4276.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图13 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/3974729b0e9f4433a0a6cfd3fe28c4d3.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图15 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/12c7867bf1fd43d9a81bb6e27a90a53e.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图17 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/77e89f56d30e47a1966a474a58f2ba2f.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图19 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/8269e26028f84b3ea43c1d893085879e.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图21 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/8df3a015c3764145b9ca1440a4a6ed95.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图23 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/f628e834d6ab41e09440149772b6129d.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图25 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/31f69a3aad094ba9902a1fff534fb3b6.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图27 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/6937d462c3e745cdabbe1ba9868c85b4.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图29 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/9fb017af6ba44923b94eab2b3fe4b5f0.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图31 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/2f07e579c37c4cb8928a38e92e314152.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图33 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/6035b5c2b97042ddb03a870fc8417c5f.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图35 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/d3bb0d7e6bd1461387489aaae235798b.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图37 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/db10a018fb02404cab738033c8bfe822.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图39 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/019e8dcddc33493eb0413484f48c1fab.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图41 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/03055872c2154b7997448e3c6f687f40.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图43 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/ab9114b4e0da4cfa844dc599d798cdc8.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图45 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/f2afc845fb984f2db6687b10259db66d.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图47 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/f34d3cc61aee409eb6e8df95ab73a3d8.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图49 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/878a4b2738aa434b912bcceb8dc2a275.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图51 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/a6f33de57d3b4f6abbbcc9d506f8007a.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图53 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/d7cc1b6c67054c4cae432777b968732f.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图55 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/f48bd2faeabc4cc282d3c7d1b641c12d.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图57 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/c1a177c3380e4e4cb5621c8ed3ae65a9.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图59 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/15c8726e455047cf8fde792f423c0b8d.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图61 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/0e7375b456fd499d8c658a4167304f0e.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图63 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/d1e1ce3b1fcf4c6fa2aad4afb7072347.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图65 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/17c8881da2134dd689d96e21a3dab526.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图67 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/bbccc2018bc84f489f8749517e597c92.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图69 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/a6fca4c09c5c4db2b36c73c5f2ded486.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图71 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/dba58c6b5b7842cab926d9e320799ca6.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图73 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/cbb47846c79e4e979d4821b8c5471a9e.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图75 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/de875df65ace4783b25729884d27c419.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图77 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/68f2a91b8f1441f88322d4a4ff3f81b6.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图79 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/2060ce832c4a4090a497de85f880465a.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图81 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/4043bb4f53704d109ad6ca37b3b7dd42.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图83 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/44d86da4b45c4514b863d99ac0852678.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图85 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/c573c72d4d1a4dc6b5d341bb8ee6ed23.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图87 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/fa8ecbad06c54546aa15f3129859ae56.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图89 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/81dd237cb26c4c96a6beb9295b117031.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图91 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/ae2b59cc633644be9aef12a620a06a1f.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图93 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/b94aa5dbe7a54b6fb4df55db3398159f.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图95 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/c5532e51888345ad982f28b7849c78ac.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图97 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/a5739cfd53d44be5bfb1992efb00f75c.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图99 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/b353b9271db649968883ccd3c8dccf97.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图101 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/d711e3a4b9254d5bbf7c969d23c86964.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图103 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/ac5d97c657904144892cb6d0736da6d6.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图105 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/306784cbb3b942bba9caf30f4112795a.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图107 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/b0bc2be38d4d40589dead02840b1ed16.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图109 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/7843080cfb3642a1ac02b73f263eb5ff.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图111 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/4abdad740c894ba488dd5bf5c7ad82b5.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图113 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/43e4d1fd18a9435482cc7acdc3cadd56.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图115 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/d09e4eab57af4cc1875ca25742ffdf72.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图117 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/3e63ffccde854b819f055772ec44fa36.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图119 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/ded2b0a930884c218ff5f2eb28208a8c.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图121 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/0ea1c7d5861b4ebfb2fb9da18fb83a0a.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图123 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/69b11ceaef724feeb4e7ce7d589d48c1.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图125 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/5e5bb835814e4e62a5f1d4faa8aa80b7.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图127 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/f4ac93b5862d4262a7a84ef0b0b52cce.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图129 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/887d072bfbbb44a59f2a7e8e7995f191.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图131 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/061ec3ba44094eb79d9f1fec1d6ba82d.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图133 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/b72e2b22366341b6990a61258378f810.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图135 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/871ee7047604472091ea56017ce87036.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图137 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/94dc06a7848b4ff1ac9bab83ac5c9128.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图139 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/92ef37ee4ebe49d1adedb17770ba0425.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图141 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/1fbe225b75674785a24108513890524a.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图143 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/eb0c460a99e6497f9d8ea50fd4cea70c.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图145 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/a4edcd00837240cd992c56775a4c3738.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图147 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/085143cec2c74dffa26f3349461d6420.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图149 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/b84685e549a04e18a3ab3ed4a7d17fcb.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图151 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/3d82b07c6c22472fb7afd5ee09eec68b.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图153 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/a7559e383d5c41328ec8073ac3e20532.jpg)
![shell脚本语言(超全超详细)[通俗易懂]插图155 在这里插入图片描述](https://img.mushiming.top/app/yundeesoft_com/356635b689bb4825b555903a76a7015a.jpg)