linux shell教程(二)
来源:互联网 发布:去黑眼圈眼袋知乎 编辑:程序博客网 时间:2024/05/17 06:55
Shell教程目录
1.Shell简介
2.几种常见的Shell
3.Shell与编译型语言的差异
4.什么时候使用Shell
5.第一个Shell脚本
6.Shell变量
7.Shell特殊变量
8.Shell替换
9.Shell运算符
10.Shell注释
11.Shell字符串
12.Shell数组
13.Shell echo命令
14.shell printf命令
15.Shell if else语句
16.Shell case esac语句
17.Shell for循环
18.Shell while循环
19.Shell until循环
20.Shell跳出循环
21.Shell函数
22.Shell函数参数
23.Shell输入输出重定向
24.Shell文件包含
9.Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
Bash 支持很多运算符,包括算数运算符、关系运算符、布尔运算符、字符串运算符和文件测试运算符。
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
例如,两个数相加:
- #!/bin/bash
- val=`expr 2 + 2`
- echo "Total value : $val"
Total value : 4两点注意:
- 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
- 完整的表达式要被 ` ` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。
算术运算符
先来看一个使用算术运算符的例子:
- #!/bin/sh
- a=10
- b=20
- val=`expr $a + $b`
- echo "a + b : $val"
- val=`expr $a - $b`
- echo "a - b : $val"
- val=`expr $a \* $b`
- echo "a * b : $val"
- val=`expr $b / $a`
- echo "b / a : $val"
- val=`expr $b % $a`
- echo "b % a : $val"
- if [ $a == $b ]
- then
- echo "a is equal to b"
- fi
- if [ $a != $b ]
- then
- echo "a is not equal to b"
- fi
a + b : 30a - b : -10a * b : 200b / a : 2b % a : 0a is not equal to b注意:
- 乘号(*)前边必须加反斜杠(\)才能实现乘法运算;
- if...then...fi 是条件语句,后续将会讲解。
注意:条件表达式要放在方括号之间,并且要有空格,例如 [$a==$b] 是错误的,必须写成 [ $a == $b ]。
关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字。先来看一个关系运算符的例子:
- #!/bin/sh
- a=10
- b=20
- if [ $a -eq $b ]
- then
- echo "$a -eq $b : a is equal to b"
- else
- echo "$a -eq $b: a is not equal to b"
- fi
- if [ $a -ne $b ]
- then
- echo "$a -ne $b: a is not equal to b"
- else
- echo "$a -ne $b : a is equal to b"
- fi
- if [ $a -gt $b ]
- then
- echo "$a -gt $b: a is greater than b"
- else
- echo "$a -gt $b: a is not greater than b"
- fi
- if [ $a -lt $b ]
- then
- echo "$a -lt $b: a is less than b"
- else
- echo "$a -lt $b: a is not less than b"
- fi
- if [ $a -ge $b ]
- then
- echo "$a -ge $b: a is greater or equal to b"
- else
- echo "$a -ge $b: a is not greater or equal to b"
- fi
- if [ $a -le $b ]
- then
- echo "$a -le $b: a is less or equal to b"
- else
- echo "$a -le $b: a is not less or equal to b"
- fi
10 -eq 20: a is not equal to b10 -ne 20: a is not equal to b10 -gt 20: a is not greater than b10 -lt 20: a is less than b10 -ge 20: a is not greater or equal to b10 -le 20: a is less or equal to b
布尔运算符
先来看一个布尔运算符的例子:
- #!/bin/sh
- a=10
- b=20
- if [ $a != $b ]
- then
- echo "$a != $b : a is not equal to b"
- else
- echo "$a != $b: a is equal to b"
- fi
- if [ $a -lt 100 -a $b -gt 15 ]
- then
- echo "$a -lt 100 -a $b -gt 15 : returns true"
- else
- echo "$a -lt 100 -a $b -gt 15 : returns false"
- fi
- if [ $a -lt 100 -o $b -gt 100 ]
- then
- echo "$a -lt 100 -o $b -gt 100 : returns true"
- else
- echo "$a -lt 100 -o $b -gt 100 : returns false"
- fi
- if [ $a -lt 5 -o $b -gt 100 ]
- then
- echo "$a -lt 100 -o $b -gt 100 : returns true"
- else
- echo "$a -lt 100 -o $b -gt 100 : returns false"
- fi
10 != 20 : a is not equal to b10 -lt 100 -a 20 -gt 15 : returns true10 -lt 100 -o 20 -gt 100 : returns true10 -lt 5 -o 20 -gt 100 : returns false
字符串运算符
先来看一个例子:
- #!/bin/sh
- a="abc"
- b="efg"
- if [ $a = $b ]
- then
- echo "$a = $b : a is equal to b"
- else
- echo "$a = $b: a is not equal to b"
- fi
- if [ $a != $b ]
- then
- echo "$a != $b : a is not equal to b"
- else
- echo "$a != $b: a is equal to b"
- fi
- if [ -z $a ]
- then
- echo "-z $a : string length is zero"
- else
- echo "-z $a : string length is not zero"
- fi
- if [ -n $a ]
- then
- echo "-n $a : string length is not zero"
- else
- echo "-n $a : string length is zero"
- fi
- if [ $a ]
- then
- echo "$a : string is not empty"
- else
- echo "$a : string is empty"
- fi
abc = efg: a is not equal to babc != efg : a is not equal to b-z abc : string length is not zero-n abc : string length is not zeroabc : string is not empty
文件测试运算符
文件测试运算符用于检测 Unix 文件的各种属性。例如,变量 file 表示文件“/var/www/tutorialspoint/unix/test.sh”,它的大小为100字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性:
- #!/bin/sh
- file="/var/www/tutorialspoint/unix/test.sh"
- if [ -r $file ]
- then
- echo "File has read access"
- else
- echo "File does not have read access"
- fi
- if [ -w $file ]
- then
- echo "File has write permission"
- else
- echo "File does not have write permission"
- fi
- if [ -x $file ]
- then
- echo "File has execute permission"
- else
- echo "File does not have execute permission"
- fi
- if [ -f $file ]
- then
- echo "File is an ordinary file"
- else
- echo "This is sepcial file"
- fi
- if [ -d $file ]
- then
- echo "File is a directory"
- else
- echo "This is not a directory"
- fi
- if [ -s $file ]
- then
- echo "File size is zero"
- else
- echo "File size is not zero"
- fi
- if [ -e $file ]
- then
- echo "File exists"
- else
- echo "File does not exist"
- fi
File has read accessFile has write permissionFile has execute permissionFile is an ordinary fileThis is not a directoryFile size is zeroFile exists
10.Shell注释
以“#”开头的行就是注释,会被解释器忽略。sh里没有多行注释,只能每一行加一个#号。只能像这样:
- #--------------------------------------------
- # 这是一个自动打ipa的脚本,基于webfrogs的ipa-build书写:
- # https://github.com/webfrogs/xcode_shell/blob/master/ipa-build
- # 功能:自动为etao ios app打包,产出物为14个渠道的ipa包
- # 特色:全自动打包,不需要输入任何参数
- #--------------------------------------------
- ##### 用户配置区 开始 #####
- #
- #
- # 项目根目录,推荐将此脚本放在项目的根目录,这里就不用改了
- # 应用名,确保和Xcode里Product下的target_name.app名字一致
- #
- ##### 用户配置区 结束 #####
11.Shell字符串
字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号。单双引号的区别跟PHP类似。单引号
- str='this is a string'
- 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
- 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。
双引号
- your_name='qinjx'
- str="Hello, I know your are \"$your_name\"! \n"
- 双引号里可以有变量
- 双引号里可以出现转义字符
拼接字符串
- your_name="qinjx"
- greeting="hello, "$your_name" !"
- greeting_1="hello, ${your_name} !"
- echo $greeting $greeting_1
获取字符串长度
- string="abcd"
- echo ${#string} #输出 4
提取子字符串
- string="alibaba is a great company"
- echo ${string:1:4} #输出liba
查找子字符串
- string="alibaba is a great company"
- echo `expr index "$string" is`
12.Shell数组:shell数组的定义、数组长度
Shell在编程方面比Windows批处理强大很多,无论是在循环、运算。bash支持一维数组(不支持多维数组),并且没有限定数组的大小。类似与C语言,数组元素的下标由0开始编号。获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0。
定义数组
在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。定义数组的一般形式为:array_name=(value1 ... valuen)
例如:
- array_name=(value0 value1 value2 value3)
- array_name=(
- value0
- value1
- value2
- value3
- )
还可以单独定义数组的各个分量:
- array_name[0]=value0
- array_name[1]=value1
- array_name[2]=value2
读取数组
读取数组元素值的一般格式是:${array_name[index]}
例如:
- valuen=${array_name[2]}
- #!/bin/sh
- NAME[0]="Zara"
- NAME[1]="Qadir"
- NAME[2]="Mahnaz"
- NAME[3]="Ayan"
- NAME[4]="Daisy"
- echo "First Index: ${NAME[0]}"
- echo "Second Index: ${NAME[1]}"
$./test.shFirst Index: ZaraSecond Index: Qadir使用@ 或 * 可以获取数组中的所有元素,例如:
- ${array_name[*]}
- ${array_name[@]}
- #!/bin/sh
- NAME[0]="Zara"
- NAME[1]="Qadir"
- NAME[2]="Mahnaz"
- NAME[3]="Ayan"
- NAME[4]="Daisy"
- echo "First Method: ${NAME[*]}"
- echo "Second Method: ${NAME[@]}"
$./test.shFirst Method: Zara Qadir Mahnaz Ayan DaisySecond Method: Zara Qadir Mahnaz Ayan Daisy
获取数组的长度
获取数组长度的方法与获取字符串长度的方法相同,例如:
- # 取得数组元素的个数
- length=${#array_name[@]}
- # 或者
- length=${#array_name[*]}
- # 取得数组单个元素的长度
- lengthn=${#array_name[n]}
接下页
0 0
- linux shell教程(二)
- Linux shell 简明教程(二)
- shell教程二: shell中的$
- shell 教程二:数组
- 《unix/linux编程实践教程》之Shell编程二
- linux shell教程
- linux shell编程教程
- linux shell编程教程
- linux shell脚本教程
- linux shell教程(一)
- linux shell教程(三)
- linux shell教程(四)
- Linux shell 教程
- Linux--shell详细教程
- Linux Shell系列教程之(二)第一个Shell脚本
- Shell-Cron基本教程二
- Linux Shell 入门(二)
- Linux Shell编程 二
- linux 驱动机制之设备,驱动,总线
- Unity3D 调用外部程序
- jsp界面获取action中的参数值
- ASP.NET(C#)不提示直接关闭当前页面
- wind7 IE桌面图标
- linux shell教程(二)
- BAT解密:互联网技术发展之路(3)- 牛逼公司的技术架构都是这个范
- Solr or Elasticsearch–That Is the Question
- 九、运用栈的知识对后缀表达式的运算方式进行表达
- MVC C#在后台接收一个气象台Json,在前台可以弹出json中所有的数据,但是现在想获取气象Json中每一个对象
- 苹果开发 笔记(30)城市列表
- PageRank算法简介及Map-Reduce实现
- 最新版SDWebImage的使用
- tcpip面试复习