Shell scripts under Mac using study notes (a)

references Shell Tutorials
Common MAC command line terminal
Shell scripts under Mac

1. Use the terminal to create test.sh:

(1) enter the specified folder path (command Example: cd Desktop/面向对象程序设计);
(2) create test.sh file (command Example: touch test.sh);

(3) create the first Shell script:

#!/bin/bash
echo "Hello World!"

Note: #!is a mark of the agreement, which tells the system what the script requires an interpreter to perform that which Shell use.
echoCommand is used to output text window.
(4) running through the terminal [Appendix Run Shell scripts, there are two methods , the first paper,]:

NOTE:

  • Be sure to enter ./test.shinstead,tech.sh ;
  • The first error occurs Permission denied, you must first enter chmod u+x ./test.sh //使脚本具有执行权限and then execute the script.

2.Shelld basic syntax:

(1) variable:

① When you define a variable, the variable name without the dollar sign ($, PHP language variables needed), such as:

your_name="Kenn"

Variable naming rules the same as C language, but one thing to note is that the place can not have spaces between the variable name and the equal sign .
② When using variables, as long as the dollar plus sign in front of variable name, such as:

your_name="Kenn"
echo $your_name //或者 echo ${your_name}

Variable name outside the curly braces are optional, plus without anything, add braces to help identify the boundary interpreter variables.
Such as:

for skill in Ada Coffe Action Java; do
    echo "I am good at ${skill}Script"
done   

If you do not add braces to skill variables written echo "I am good at $skillScript", the interpreter will put $skillScriptas a variable (its value is empty), code execution is not the result we expect the same again.
Recommended for all variables braces, this is a good programming practice.
③ defined variables, can be re-defined as:

your_name="Tom"
echo $your_name
your_name="Kenn"
echo $your_name

这样写是合法的,但注意,第二次赋值的时候不能写$your_name="alibaba"使用变量的时候才加美元符($)。
④只读变量(readonly+变量名,只读变量的值不能被改变)、删除变量(变量被删除后不能再次使用,unset 命令不能删除只读变量)。
⑤变量类型(局部变量、环境变量、shell变量)。

(2)字符串:

字符串可以用单引号,也可以用双引号,也可以不用引号。(优缺点可见参考链接)
①拼接字符串(echo $greeting $greeting_1);
②获取字符串长度(echo ${#printf_name});
③提取子字符串(第一个字符的索引值为 0,与C语言规范相同,echo ${printf_name:1:5} # 输出ello,从字符串第2个字符开始截取五个字符);
④查找子字符串(注意:以下脚本中是反引号,而不是单引号):
例:查找字符 i 或 o 的位置(哪个字母先出现就计算哪个)
shell string="runoob is a great site" echo `expr index "$string" io` # 输出 4

(3)数组:

bash支持一维数组(不支持多维数组),并且没有限定数组的大小。类似与C语言,数组元素的下标由0开始编号。获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0。
①定义数组数组名=(值1 值2 ... 值n):在 Shell 中,用括号来表示数组,数组元素用"空格"符号分割开。还可以单独定义数组的各个分量,可以不使用连续的下标,而且下标的范围没有限制。
②读取数组:${数组名[下标]},使用@符号可以获取数组中的所有元素,例如:echo ${array_name[@]}
③获取数组的长度:获取数组长度的方法与获取字符串长度的方法相同,例如:

# 取得数组元素的个数
length=${#array_name[@]}
# 或者
length=${#array_name[*]}
# 取得数组单个元素的长度
lengthn=${#array_name[n]}

(4)Shell 注释:

#开头的行就是注释,会被解释器忽略。通过每一行加一个#号设置多行注释。
多行注释还可以使用以下格式:

:<<EOF
注释内容...
注释内容...
注释内容...
EOF

EOF也可以使用其他符号:

:<<'
注释内容...
注释内容...
注释内容...
'

:<<!
注释内容...
注释内容...
注释内容...
!

(5)Shell 传递参数:

我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n。n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推。(其中 $0 为执行的文件名,传递的参数中如果包含空格,应该使用单引号或者双引号将该参数括起来,以便于脚本将这个参数作为整体来接收。)具体,可见参考链接。

$*$@区别:
相同点:都是引用所有参数。
不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)。

(6)Shell 基本运算符:

Shell 和其他编程语言一样,支持多种运算符,包括:

  • 算数运算符
  • 关系运算符(关系运算符只支持数字,不支持字符串,除非字符串的值是数字)
  • 布尔运算符
  • 字符串运算符
  • 文件测试运算符
    注意:
  • 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
  • 完整的表达式要被```包含.
  • 条件表达式要放在方括号之间,并且要有空格,例如: [\(a==\)b] 是错误的,必须写成 [ $a == $b ]。Shell 中的中括号用法总结
  • 乘号(*)前边必须加反斜杠()才能实现乘法运算;
  • 在 MAC 中 shell 的 expr 语法是:$((表达式)),此处表达式中的 "*" 不需要转义符号 "" 。

Guess you like

Origin www.cnblogs.com/lvhang/p/12243001.html