学习Shell

Shell简析和基本操作介绍

Posted by Haiming on October 11, 2019

已经想不出该在这里说什么了,开工……

参考:http://www.runoob.com/linux/linux-shell.html

1. Shell 简介和 Shell 环境

Shell 既是一种命令语言,又是一种程序设计语言。

Shell 提供了一个应用程序,其提供了一个界面,用户通过这个界面进行访问操作系统内核 的操作。

1.1 Shell 环境

本教程之中所说的 Shell 指的是 Bash, Bash 是大多数系统的默认 Shell, 也是大部分 Linux 的默认shell。

#!

就是告诉系统,其之后的路径所指定的程序就是解释这个脚本文件的 Shell 程序。

2. 第一个 Shell 程序

反正 Shell 是随便写的, 在 MacBook 之中内置了(或许……? 因为我没有下载 Shell),因此就没有安装步骤。

所有程序都是从 Hello World 开始,当然我们的shell也不例外。

新建一个文件,叫啥无所谓。我们这个地方就把它叫做tesh.sh

#! /bin/bash 
echo "Hello World"

其输出就是 “Hello World”。

如果作为解释器参数,那么第一行不需要指定解释器信息。指定了也没用。

/bin/sh test.sh
/bin/php test.php

3. Shell 变量

Shell 之中,在定义变量的时候,不需要在变量名字之中加入$

有几点要注意的:

  1. 变量名和等号之间不能有空格!是的。不可以有。不然会直接提示 not found
  2. 变量之中不可以有空格,但是可以有下划线。下划线可以放在第一个字符位置
  3. 变量之中不可以加入标点符号
  4. 变量不可以使用 Bash 之中的标点符号(废话

下面是某些有效的Shell 变量名。

RUNOOB
LD_LIBRARY_PATH
_var
var2

无效的变量命名:

?var=123
user*name=runoob

除了显式的直接赋值,还可以使用语句来给变量赋值,比如:

for file in `ls /etc`

这样的命令可以将整个 /etc 下面的文件名循环出来。

3.1 使用变量

使用一个定义过的变量,只要在变量前面加美元符号即可。比如:

#! /bin/bash 
echo "Hello World"
your_name="runoob.com"
echo ${your_name}

输出为:

Hello World
runoob.com

变量外面的花括号是可选的,加不加都可以。加花括号是为了识别变量的边界,比如:

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

遇到这种情况,不加花括号的话,就会将skillScript整个当作是一个变量。

推荐是将所有变量都加上花括号。

之所以叫做变量,其值肯定可以改变。

变量在赋值的时候直接赋值就好,在取值的时候才需要加入 $ 符号。

3.2 只读变量

使用 readonly 可以将变量定义为只读变量。其值如果被改变,会直接报错。

#! /bin/bash 
echo "Hello World"
your_name="runoob.com"
echo $your_name
your_name="ChangeName"
echo $your_name
onlyread="readonly"
readonly onlyread
onlyread="ChangeName"

结果:

Hello World
runoob.com
ChangeName
test.sh: line 9: onlyread: readonly variable

3.3 删除变量

使用 unset 删除变量。

unset variable_name

但是注意,只读变量不可以修改,也不可以被删除。如果被删除,会直接报下面的错误:

#! /bin/bash 
echo "Hello World"
your_name="runoob.com"
echo $your_name
your_name="ChangeName"
echo $your_name
onlyread="readonly"
readonly onlyread
unset onlyread

输出:

Hello World
runoob.com
ChangeName
test.sh: line 9: unset: onlyread: cannot unset: readonly variable

3.4 变量类型

运行 shell 的时候,同时存在三种变量。

  • 局部变量: 在脚本或者命令之中定义,仅在当前shell 实例之中有效,其他shell 启动的程序不能访问局部变量。我们之前定义的全都是局部变量。
  • 环境变量: 所有的程序,包括 shell 启动的程序,都可以访问环境变量。 有些程序需要环境变量来保证其正常运行。必要的时候,Shell 脚本也可以定义环境变量。但是大部分情况不必要。
  • Shell 变量: shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行

3.5 一些操作符

  • [@]: Get all data from one array
  • # :Get number of elements in one array
  • [n] : Get element with the index in array

4. Shell pass parameters

So we can pass parameters to the shell script when we use it.

The format for passing parameters into script is $n, n represent one number, which is the sequence of the script.

Please notice that $0 is always the name of file. So that if you want to start from the parameter you type in, please use the $1 and $2 and so on.

4.1 Some special symbols

  • $# : Get the total number of parameters
  • $* : Get the parameters as one string and can output.

The difference between $* and $@ is:

  • If you pass 3 parameters into one script,$* equals to “1 2 3”, but $@ equals to “1” “2” “3”,it passes 3 parameters.

5. Printf Operations

Printf, in bash, can use to do operation on the things of format for string.

Here is the table of special characters for Printf

序列 说明
\a 警告字符,通常为ASCII的BEL字符
\b 后退
\c 抑制(不显示)输出结果中任何结尾的换行字符(只在%b格式指示符控制下的参数字符串中有效),而且,任何留在参数里的字符、任何接下来的参数以及任何留在格式字符串中的字符,都被忽略
\f 换页(formfeed)
\n 换行
\r 回车(Carriage return)
\t 水平制表符
\v 垂直制表符
\ 一个字面上的反斜杠字符
\ddd 表示1到3位数八进制值的字符。仅在格式字符串中有效
\0ddd 表示1到3位的八进制值字符

6. Shell test command

In the test command for shell, it can be used to check if some condition is true. It can do value, string and file, 3 aspects of testing.

参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真
num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi