「学习笔记-Linux」学习Shell Script
学习Shell Script
Table of Contents
- 1 什么是Shell Scipt
- 1.1 程序书写
- 1.2 程序执行
- 2 简单Shell练习
- 2.1 例1 接收用户输入
- 2.2 例2 按日期建立相似名字的文件
- 3 判断式
- 3.1 测试文件是否存在
- 3.2 test常用选项
- 3.2.1 文件类型
- 3.2.2 权限
- 3.2.3 文件新旧比较
- 3.2.4 整数,字符串,多重条件判断
- 3.3 使用[]判断
- 4 Shell Script 参数
- 5 条件表达式
- 5.1 if 结构
- 5.2 if else 结构
- 5.3 case
- 6 函数
- 7 循环
- 7.1 while
- 7.2 for
- 8 shell script的追踪与Debug
1 什么是Shell Scipt
使用指令和基本程序设计结构写成的程序,可以完成复杂的处理流程
1.1 程序书写
#!/bin/bash # Program: # This program shows "Hello Wrold" in your screen. # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATH echo -e "Hello World!\a\n" exit 0
- 第一行 #!/bin/bash 说明使用的shell类型,不同shell语法可能不同,所以要说明使用的是哪种shell
- 其它#开始的表示注释,注释一般需要说明
- 程序功能
- 版本历史
- 作者及联系方式
- 设置好PATH变量,以便直接可以调用相应路径下的命令
- 程序主体部分
- exit 0 表示程序执行成功,向环境返回0
1.2 程序执行
- bash $bash sh01.sh #如果用sh sh01.sh而sh又不是指向bash,那么sh01.sh内的语法就会不一致,因为用 #sh去解释了bash语法写的shell script,针对这个程序,如果 #$type sh #得到sh is hashed (/bin/sh) #那么会输出-e Hello world!,而非Hello world!
- $./xxx.sh $chmod +x sh01.sh $./sh01.sh
- source $ source sh01.sh
注:用bash和用source的不同在于,用bash执行时,shell script其实是在在父程序bash下新建了一个 bash子程序,这个子程序中执行,当程序执行完后,shell script里定义的变量都会随子程序的结束而消失, 而用source执行时,是在父程序bash中执行,shell script里定义的变量都还在。
2 简单Shell练习
2.1 例1 接收用户输入
# !/bin/bash # Program: # This program is used to read user's input # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATH read -p "Your first name:" firstname # tell user to input read -p "Your last name:" lastname # tell user to input echo -e "\nYour full name: $firstname $lastname" exit 0
$ bash sh02.sh Your first name:Minix Your last name:007Your full name: Minix 007
2.2 例2 按日期建立相似名字的文件
# !/bin/bash # Program: # This program is used to create files according to date # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATH# Get filename from user echo -e "I will use 'touch' to create three files." read -p "Please input your filename:" tmpfilename# Prevent the user input [Enter] # Check whether filename exists or not filename=${tmpfilename:-"filename"}# Get the final filename according to date date1=$(date --date='2 days ago' +%Y%m%d) # date of 2 days ago date2=$(date --date='1 days ago' +%Y%m%d) # date of yesterday date3=$(date +%Y%m%d) # date of today filename1=${filename}${date1} filename2=${filename}${date2} filename3=${filename}${date3}# Create file touch "$filename1" touch "$filename2" touch "$filename3"exit 0
$ bash sh03.sh I will use 'touch' to create three files. Please input your filename:WhoKnows $ ls W* WhoKnows20130201 WhoKnows20130202 WhoKnows20130203
3 判断式
3.1 测试文件是否存在
test -e filename会根据filename是否存在返回0或1,再交由echo显示结果
$ test -e sh01.sh && echo "Exists" || echo "Not exists" Exists $ test -e sh0x.sh && echo "Exists" || echo "Not exists" Not exists
3.2 test常用选项
3.2.1 文件类型
- -e file :file是否存在
- -f file :file是否存在且为文件
- -d file :file是否存在且为目录
3.2.2 权限
- -r file :file是否有读的权限
3.2.3 文件新旧比较
- -nt file1 file2 : file1 是否比 file2新
3.2.4 整数,字符串,多重条件判断
- -z string: string是否为空
例:输出指定文件类型及属性
# !/bin/bash # Program: # This program is used to output type and permission of the target file # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATH# Get filename from user echo -e "Input name of the file that you want to check.\n" read -p "Filename:" filename test -z $filename && echo "You must input a filename." && exit 0# Check whether the file exists or not test ! -e $filename && echo "The file '$filename' DO NOT exists" && exit 0# Check type and permission of the file test -f $filename && filetype="regular file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writable" test -x $filename && perm="$perm executable"# Output result echo "The filename:$filename is a $filetype" echo "And Permissions are :$perm"exit 0
$ bash sh04.sh Input name of the file that you want to check.Filename:sh01.sh The filename:sh01.sh is a regular file And Permissions are :readable writable executable
3.3 使用[]判断
- 测试文件是否存在
$ [ -e "sh01.sh" ] ; echo $? 0 $ [ -e "sh0x.sh" ] ; echo $? 1
- 注意[]内空格必须有
- 这种方法和test的test -e "sho1.sh" ; echo $? 是一致的
4 Shell Script 参数
# !/bin/bash # Program: # This program is used to ouput parameter of the shell script # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHecho "The script's name is ==> $0" echo "Total parameter number is ==> $#"# Check whether number of the parameter is less than 2 [ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stop here." && exit 0echo "The whole parameter is ==> '$@'" echo "The first parameter is ==> $1" echo "The first parameter is ==> $2"exit 0
$ bash sh05.sh 1a 2b 3c 4d The script's name is ==> sh05.sh Total parameter number is ==> 4 The whole parameter is ==> '1a 2b 3c 4d' The first parameter is ==> 1a The first parameter is ==> 2b
注:从以上程序可以看出与参数有关的预设变量如何表示
5 条件表达式
5.1 if 结构
# !/bin/bash # Program: # This program is used to show if else expression # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHread -p "Please input [Y/N]" choice if [ "$choice" == "Y" ] || [ "$choice" == "y" ];thenecho "OK, continue"exit 0 fi if [ "$choice" == "N" ] || [ "$choice" == "n" ];thenecho "Oh, interupt"exit 0 fiexit 0
$ bash sh06.sh Please input [Y/N]y OK, continue $ bash sh06.sh Please input [Y/N]n Oh, interupt
5.2 if else 结构
# !/bin/bash # Program: # This program is used to show if else expression # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHread -p "Please input [Y/N]" choice if [ "$choice" == "Y" ] || [ "$choice" == "y" ];thenecho "OK, continue"exit 0 elif [ "$choice" == "N" ] || [ "$choice" == "n" ];thenecho "Oh, interupt"exit 0 elseecho "Input [Y/N]" fiexit 0
5.3 case
# !/bin/bash # Program: # This program is used to show case expression # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHread -p "Tell me your choice:[1-3]=>" choice case $choice in"1")echo "Your choice is ONE";;"2")echo "Your choice is TWO";;"3")echo "Your choice is THREE";; esacexit 0
$ bash sh08.sh Tell me your choice:[1-3]=>2 Your choice is TWO $ bash sh08.sh Tell me your choice:[1-3]=>1 Your choice is ONE $ bash sh08.sh Tell me your choice:[1-3]=>3 Your choice is THREE
6 函数
# !/bin/bash # Program: # This program is used to test function # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHfunction myprint(){echo -n "Your choice is " }read -p "Tell me your choice:[1-3]=>" choice case $choice in"1")myprint;echo "ONE";;"2")myprint;echo "TWO";;"3")myprint;echo "THREE";; esacexit 0
$ bash sh09.sh Tell me your choice:[1-3]=>1 Your choice is ONE $ bash sh09.sh Tell me your choice:[1-3]=>2 Your choice is TWO $ bash sh09.sh Tell me your choice:[1-3]=>3 Your choice is THREE
7 循环
7.1 while
# !/bin/bash # Program: # This program shows while expression # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHwhile [ "$choice" != "yes" ] doread -p "Give your choice [yes/no]:" choice doneexit 0
$ bash sh10.sh Give your choice [yes/no]:no Give your choice [yes/no]:no Give your choice [yes/no]:nx Give your choice [yes/no]:yes
7.2 for
# !/bin/bash # Program: # This program is used to demo for expression # History: # 2013/2/3 on_1y First releasePATH=$PATH export PATHfor choice in 1 2 3 doecho "your choice is $choice" doneexit 0
$ bash sh11.sh your choice is 1 your choice is 2 your choice is 3
8 shell script的追踪与Debug
- sh -n xx.sh # 语法检查
- sh -x xx.sh # 列出xx.sh的执行过程
转载于:https://www.cnblogs.com/Iambda/archive/2013/02/15/3933507.html
「学习笔记-Linux」学习Shell Script相关推荐
- 「Vue 学习笔记 1」Vue 项目快速搭建,初始项目各个文件夹作用介绍和启动代码执行流程分析
「Vue 学习笔记 1」Vue 项目快速搭建,初始项目各个文件夹作用介绍和启动代码执行流程分析 前言 一.我的开发环境 二.使用 Vue CLI (Vue 脚手架)快速搭建项目 三.初始项目的目录结构 ...
- git学习笔记——Linux和Windows下git的基本操作
git学习笔记--Linux和Windows下git的基本操作 github主页:https://github.com/Taot-chen 一.Linux环境 1.安装git sudo apt-get ...
- 多路径配置udev_学习笔记:Linux多路径配置 multipath实现设备用户组绑定详细设置...
天萃荷净 Linux多路径软件配置,通过multipath实现设备用户组绑定详细设置 现在的Linux系统中,很多都会使用系统自带的multipath多路径软件,在以前的版本中,我们一般通过multi ...
- 51CTO学习笔记--Linux运维故障排查思路与系统调优技巧视频课程(高俊峰)
51CTO学习笔记--Linux运维故障排查思路与系统调优技巧视频课程 第一课 Linux运维经验分享与思路 1.一般把主机名,写到hosts下 127.0.0.1 hostname,因为 ...
- 树莓派学习笔记——Linux I2C驱动说明
1.前言 [linux内核说明] 通常情况下,I2C设备由内核驱动控制,但是某些情况下I2C设备也可由用户空间控制.如果在用户空间控制I2C设备,需要访问/dev目录中所提供的接口,在使用I2C设备之 ...
- 【长篇博文】Docker学习笔记与深度学习环境的搭建和部署(二)
长篇博文记录学习流程不容易,请关注.转发.点赞.评论,谢谢! 上一篇文章:Docker学习笔记与深度学习环境的搭建和部署(一) 文章末尾附加nvidia455.23.cuda11.1.cudnn8.0 ...
- 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环
目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...
- 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用
目录 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用 16.1 LVM的工作原理 16.1.1 LVM常用术语 16.1.2 LVM优点 16.2 创建LVM的基本步骤 16.2 ...
- MATLAB学习笔记0:学习须知
阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...
最新文章
- push代码到github时,每次都要输入用户名和密码的问题
- 一元二次方程实根java_请依次输入一元二次方程的三个系数,并点击计算显示实根...
- 一维数组的求平均成绩 Day06
- 给你一个网站你是如何来渗透测试的
- 利用GPU训练网络时遇到的一些问题
- 数据库脱机和分离的区别
- java实验二答案天津商业大学_天津商业大学信息安全实验一
- 浅入深出Vue:子组件与数据传递
- C语言ASM汇编内嵌语法【转】
- Visual studio 2017 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包...
- 单片机涡轮流量传感器_青天仪表为您介绍安装涡轮流量计需要注意问题
- 装修软件平台开发的优势
- 分布式机器学习主要笔记
- 腾讯云人脸识别 活体检测 人员库管理
- pythonscrapy爬虫ip代理池_Scrapy 框架插件之 IP 代理池
- 钢笔墨水能否代替打印机墨水_喷墨打印机该用染料墨水还是颜料墨水?
- c++获取电脑mac地址
- LSVGlobal Mapper应用----影像裁剪
- SQL复杂查询 高速数目和城市数目
- QOpenGLWidget显示视频流数据
热门文章
- 猜数游戏python_Hello,Python!小鲸教你学Python(九)之文件操作
- Java 内存 关系_内存一致性 – 发生在Java之前的关系
- 适合win7的python版本_windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2...
- html生成的超级链接预览功能,超链接特效
- 【java】Maven工程引入各种jar包的功能
- 数据库及页面乱码问题
- 手机号码输入历史记录匹配
- 蓝桥杯 扑克序列(全排列)
- 自己动手,打造轻量级VSCode/C#环境代替LinqPad
- linux下的vi与vim