linux下shell script学习(一)
来源:互联网 发布:生命世界网络构图 编辑:程序博客网 时间:2024/05/17 02:59
对于以后想从事嵌入式软件开发工作的人来说,至少要能看懂shell脚本,最好能自己编写shell脚本,因为当你在linux操作系统做开发你会发现,shell脚本能够解决你总是重重复复做的一些事,给你节约很多宝贵的时间!那么什么是shell script呢?其实shell脚本是利用shell的功能所写的一个“程序”,这个程序是使用纯文本文件,将一些shell的语法与命令写在里面,搭配正则表达式,管道命令与数据流重定向,达到我们想要的处理目的。shell script本质上是shell命令的集合。
下面我们来学习最简单的shell script编写与执行:
编写:
[zoulei@CentOS test]$ vim test.sh 1 #!/bin/sh 2 a="hello world!" 3 num=2 4 echo "a is : $a num is ${num}"
***************************************************************************************************************************************************************************************************
注意:第一行#!/bin/sh声明这个script使用的shell名称,因为我们使用的是bash,所以必须要以#!/bin/sh来声明这个文件内的语法使用bash的语法,那么当这个程序被执行时,它就能加载bash的相关配置文件,并且执行bash来使下面的命令能够执行。
***************************************************************************************************************************************************************************************************
执行:[zoulei@CentOS test]$ sh test.sha is : hello world! num is 2[zoulei@CentOS test]$ chmod a+x test.sh[zoulei@CentOS test]$ ./test.sha is : hello world! num is 2nd[zoulei@CentOS test]$ source test.sha is : hello world! num is 2
******************************************************************************************************************************************************************************************************
注意:这里执行test.sh脚本文件有三种方式,一种是sh test.sh,这里的sh命令其实是bash的符号连接。还有一种是当作可执行文件来执行,不过执行之前得将test.sh文件赋予可读可写可执行权限才行,第三种是source test.sh,它与前面两种的区别是,当我们写个脚本来改变环境变量,前面两种只是在子进程bash中执行,而第三种则会在父进程中执行,最终会使环境变量生效的是第三种执行方式。
******************************************************************************************************************************************************************************************************
下面在运用条件判断式if...then...fi 编写执行一个shell脚本:
编写与执行
[zoulei@CentOS test]$ vim test.sh#!/bin/bashecho -e "Hello world"if [ "SHELL" = "/bin/bash" ];thenecho "your login shell is bash"elseecho "your login shell is not bash but $SHELL"fi[ -f "/etc/shadow" ] && echo "this computer uses shadow passwords"if [ -f "/etc/shadow" ];thenecho "电脑密码"elseecho "并没有"fi[zoulei@CentOS test]$ sh test.shHello worldyour login shell is not bash but /bin/bashthis computer uses shadow passwords电脑密码**************************************************************************************************************************************************************************
注意:1.中括号【】使用格外小心,”[“后面必须跟一个空格,再跟“”,"】"前面也必须紧跟一个空格,否则会出错。
2.echo语句也必须紧跟一个空格,不然也会出错。
3.[ -f "/etc/shadow" ]语句是判断/etc/shadow是否是一个文件。
4.上面的shell脚本中只有一个判断式,可以这样看:
if [ 条件判断式 ]; then
当判断式成立时,可以进行命令的工作内容;
fi <==将if反过来写,成为fi,结束if 之意!
5.等号"="两边都必须要有空格,否则出错,在bash中一个等号与两个等号结果是一样的,通常一个等号代表“变量的设置”,两个等号则是代表
“逻辑判断”,由于中括号内重点在于“判断而非设置变量”,因此最好使用“==”较佳!
***************************************************************************************************************************************************************************
- linux下shell script学习(一)
- linux下shell script学习(二)
- Linux shell script学习笔记一
- Linux shell script学习
- linux 下 shell 学习摘要(一)
- Linux的Shell Script学习
- Linux学习记录--shell script
- Linux shell script基础学习
- Linux学习之shell script
- Linux shell编程 14 ---- 学习shell script
- 怎样编写Linux shell script(一)
- Linux学习笔记之:学习Shell Script
- Linux学习笔记(三)vim与shell script
- shell script的学习(一)常用命令
- Linux 下shell script 中使用EXPECT
- Linux重新学习--shell script--控制结构
- Linux重新学习--shell script--函数
- Linux学习笔记(9)----shell script
- 水题 第一站 关于521
- 牛客网_华为机试_021_简单密码破解(字符映射)
- Android动画
- Android ExpandableListView实现仿好友列表
- STL的部分实现
- linux下shell script学习(一)
- 解决空指针异常的一些经验
- HDU 1024 (DP,有空在写一遍)
- java小爬虫智联保存到excel
- 前端富文本编辑器ckeditor的使用
- 列表及表单知识点
- css画圣诞树
- Android 数据存储方式(SQLite)
- 2017.7.08