Linux Shell学习
来源:互联网 发布:傻瓜进销存软件破解版 编辑:程序博客网 时间:2024/06/06 19:46
1.Shell
shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的。Shell既是一种命令语言,又是一种程序设计语言。作为命令语言,它交互式地解释和执行用户输入的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。
2.Shell有两种执行命令的方式:
- 交互式(Interactive):解释执行用户的命令,用户输入一条命令,Shell就解释执行一条。
- 批处理(Batch):用户事先写一个Shell脚本(Script),其中有很多条命令,让Shell一次把这些命令执行完,而不必一条一条地敲命令。
3.bash完全兼容sh,也就是说,用sh写的脚本可以不加修改的在bash中执行。
4.Shell支持自定义变量。
定义变量时,变量名不加美元符号($),如:
- variableName="value"
- 中间不能有空格,可以使用下划线(_)。
- 不能使用标点符号。
- 不能使用bash里的关键字(可用help命令查看保留关键字)。
5.使用一个定义过的变量,只要在变量名前面加美元符号($)即可,如:
- your_name="mozhiyan"
- echo $your_name
- echo ${your_name}}
- for skill in Ada Coffe Action Java
- do
- echo "I am good at ${skill}Script"
- done
推荐给所有变量加上花括号,这是个好的编程习惯。
6.只读变量
使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。下面的例子尝试更改只读变量,结果报错:
- #!/bin/bash
- myUrl="http://see.xidian.edu.cn/cpp/shell/"
- readonly myUrl
- myUrl="http://see.xidian.edu.cn/cpp/danpianji/"
/bin/sh: NAME: This variable is read only.
7.删除变量
使用 unset 命令可以删除变量。语法:
- unset variable_name
8.算术运算符
- if [ $a == $b ]
- then
- echo "a is equal to b"
- fi
注意:条件表达式要放在方括号之间,并且要有空格,例如 [$a==$b] 是错误的,必须写成 [ $a == $b ]。
乘号(*)前边必须加反斜杠(\)才能实现乘法运算
9.关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字。10.布尔运算符
11.字符串运算符
12.文件测试运算符
13.注释
以“#”开头的行就是注释,会被解释器忽略。
sh里没有多行注释,只能每一行加一个#号。只能像这样:如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢?每一行加个#符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。
sh里没有多行注释,只能每一行加一个#号。只能像这样:
- #--------------------------------------------
- # 这是一个自动打ipa的脚本,基于webfrogs的ipa-build书写:
- # https://github.com/webfrogs/xcode_shell/blob/master/ipa-build
- # 功能:自动为etao ios app打包,产出物为14个渠道的ipa包
- # 特色:全自动打包,不需要输入任何参数
- #--------------------------------------------
14.获取字符串长度
- 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`#输出3(i是第3个字符)
15.定义数组
在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。定义数组的一般形式为:array_name=(value1 ... valuen)
例如:
- 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]}
- ${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]}
16.printf 命令用于格式化输出
如同 echo 命令,printf 命令也可以输出简单的字符串:
- $printf "Hello, Shell\n"
- Hello, Shell
- $
printf 命令的语法:
printf format-string [arguments...]
format-string 为格式控制字符串,arguments 为参数列表。与C语言printf()函数的不同:注意,根据POSIX标准,浮点格式%e、%E、%f、%g与%G是“不需要被支持”。这是因为awk支持浮点预算,且有它自己的printf语句。这样Shell程序中需要将浮点数值进行格式化的打印时,可使用小型的awk程序实现。然而,内建于bash、ksh93和zsh中的printf命令都支持浮点格式。
- printf 命令不用加括号
- format-string 可以没有引号,但最好加上,单引号双引号均可。
- 参数多于格式控制符(%)时,format-string 可以重用,可以将所有参数都转换。
- arguments 使用空格分隔,不用逗号。
- # format-string为双引号
- $ printf "%d %s\n" 1 "abc"
- 1 abc
- # 单引号与双引号效果一样
- $ printf '%d %s\n' 1 "abc"
- 1 abc
- # 没有引号也可以输出
- $ printf %s abcdef
- abcdef
- # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
- $ printf %s abc def
- abcdef
- $ printf "%s\n" abc def
- abc
- def
- $ printf "%s %s %s\n" a b c d e f g h i j
- a b c
- d e f
- g h i
- j
- # 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
- $ printf "%s and %d \n"
- and 0
- # 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
- $ printf "The first program always prints'%s,%d\n'" Hello Shell
- -bash: printf: Shell: invalid number
- The first program always prints 'Hello,0'
- $
17.for循环
for循环一般格式为:
in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。
例如,顺序输出当前列表中的数字:运行结果:
顺序输出字符串中的字符:运行结果:
for 变量 in 列表do command1 command2 ... commandNdone
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。
例如,顺序输出当前列表中的数字:
- for loop in 1 2 3 4 5
- do
- echo "The value is: $loop"
- done
The value is: 1The value is: 2The value is: 3The value is: 4The value is: 5
顺序输出字符串中的字符:
- for str in 'This is a string'
- do
- echo $str
- done
This is a string
如果不加引号则输出:This
is
a
string
显示主目录下以 .bash 开头的文件:运行结果:
显示主目录下以 .bash 开头的文件:
- #!/bin/bash
- for FILE in $HOME/.bash*
- do
- echo $FILE
- done
/root/.bash_history/root/.bash_logout/root/.bashrc
18.函数
函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。像其他编程语言一样,Shell 也支持函数。Shell 函数必须先定义后使用。
Shell 函数的定义格式如下:
Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。
如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。
调用函数只需要给出函数名,不需要加括号。
Shell 函数的定义格式如下:
function_name () { list of commands [ return value ]}
如果你愿意,也可以在函数名前加上关键字 function:function function_name () { list of commands [ return value ]}
函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。
如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。
调用函数只需要给出函数名,不需要加括号。
看一个带有return语句的函数:运行结果:
如果你希望直接从终端调用函数,可以将函数定义在主目录下的 .profile 文件,这样每次登录后,在命令提示符后面输入函数名字就可以立即调用。
- #!/bin/bash
- funWithReturn(){
- echo "The function is to get the sum of two numbers..."
- echo -n "Input first number: "
- read aNum
- echo -n "Input another number: "
- read anotherNum
- echo "The two numbers are $aNum and $anotherNum !"
- return $(($aNum+$anotherNum))
- }
- funWithReturn
- # Capture value returnd by last command
- ret=$?
- echo "The sum of two numbers is $ret !"
The function is to get the sum of two numbers...Input first number: 25Input another number: 50The two numbers are 25 and 50 !The sum of two numbers is 75 !
函数返回值在调用该函数后通过 $? 来获得。如果你希望直接从终端调用函数,可以将函数定义在主目录下的 .profile 文件,这样每次登录后,在命令提示符后面输入函数名字就可以立即调用。
19.函数调用
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...
带参数的函数示例:运行脚本:
带参数的函数示例:
- #!/bin/bash
- funWithParam(){
- echo "The value of the first parameter is $1 !"
- echo "The value of the second parameter is $2 !"
- echo "The value of the tenth parameter is $10 !" # $1的值 和 0
- echo "The value of the tenth parameter is ${10} !"
- echo "The value of the eleventh parameter is ${11} !"
- echo "The amount of the parameters is $# !" # 参数个数
- echo "The string of the parameters is $* !" # 传递给函数的所有参数
- }
- funWithParam 1 2 3 4 5 6 7 8 9 34 73
#!/bin/bashfunWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" # 参数个数 echo "The string of the parameters is $* !" # 传递给函数的所有参数}funWithParam 1 2 3 4 5 6 7 8 9 34 73
The value of the first parameter is 1 !The value of the second parameter is 2 !The value of the tenth parameter is 10 ! The value of the tenth parameter is 34 !The value of the eleventh parameter is 73 !The amount of the parameters is 11 !The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。 0 0
- linux学习shell篇
- Linux Shell编程学习
- linux shell学习
- linux shell脚本学习
- Linux Shell编程学习
- Linux Shell学习笔记
- Linux Shell脚本学习
- Linux shell 学习
- linux shell 学习1
- Linux Shell编程学习
- Linux之shell学习
- Linux Shell学习
- linux shell学习笔记
- Linux shell脚本学习
- LINUX SHELL 学习摘要
- linux shell学习中
- linux shell 学习
- Linux Shell编程学习
- RevitAPI外部服务介绍(ExternalService)
- Opencv step by step - 阈值化
- linux系统启动
- 黑马程序员_javaIO编程和字符集
- 《C primer plus》qsort(...)-快速排序法
- Linux Shell学习
- log4j
- 黑马程序员_java的一些新特性
- 杭电1002
- Javascript:谈谈JS的全局变量跟局部变量
- 老笔记本硬盘扩容记
- Vs2012在Linux开发中的应用(3):添加新平台hi3516
- glDrawPixels使用注意事项
- SQL Server 的通用分页显示存储过程