Shell入门(一)

目录

Shell

Shell 脚本

Shell 环境

运行 Shell 脚本有两种方法:

使用变量

只读变量 不可以被更改,也不可以被删除(unset)

删除变量

变量类型

Shell 字符串

单引号

双引号


Shell

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell

Shell 脚本

Shell 脚本(shell script),是一种为 shell 编写的脚本程序。

业界所说的 shell 通常都是指 shell 脚本,注意:shell 和 shell script 是两个不同的概念。

由于习惯的原因, "shell编程" 都是指 shell 脚本编程,不是指开发 shell 自身

Shell 环境

Shell 编程跟 java、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。

Linux 的 Shell 种类众多,常见的有:

  • Bourne Shell(/usr/bin/sh或/bin/sh)
  • Bourne Again Shell(/bin/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • Shell for Root(/sbin/sh)
  • ……
  • Bash,也就是 Bourne Again Shell,由于易用和免费,Bash 在日常工作中被广泛使用。同时,Bash 也是大多数Linux 系统默认的 Shell。

#! 的作用:告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序。

先新建一个test.sh文件

#!/bin/bash
vi test.sh

进入了编辑器

#!/bin/bash
echo "i am a test !"
echo "we are the same thing"
echo "thank you"

然后执行shell脚本

运行 Shell 脚本有两种方法:

1、作为可执行程序

chmod +x ./test.sh  #使脚本具有执行权限
./test.sh  #执行脚本./test.sh
//结果如下
i am a test !
we are the same thing
thank you

chmod +x的意思就是给执行权限

2、作为解释器参数

这种运行方式是,直接运行解释器,其参数就是 shell 脚本的文件名,如:

/bin/sh test.sh

这种方式运行的脚本,不需要在第一行指定解释器信息,写了也没用。

使用变量

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

#!/bin/bash
your_name="twenty-seven"
echo $your_name
echo ${your_name}

#输出结果为:
  twenty-seven
  twenty-seven
变量名外面的花括号是可选的,加不加都行,加花括号是为了帮助解释器识别变量的边界,比如下面这种情况:如果不加${变量},就会输出4个"i love color"
#!/bin/bash
for color in red black white yellow;
do
echo "i love ${color}"
done

# 结果如下
# i love red
# i love black
# i love white
# i love yellow

只读变量 不可以被更改,也不可以被删除(unset)

使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。

下面的例子尝试更改只读变量,结果报错:

#!/bin/bash
name="twenty-seven"
readonly name
name="twenty-eight"
error:bash: name: 只读变量
 

删除变量

使用 unset 命令可以删除变量。语法:

unset 变量名

eg:
#!/bin/bash
name="twenty-seven"
echo ${name}
#输出   twenty-seven
unset name
echo ${name}
#输出  无输出

变量被删除后不能再次使用。unset 命令不能删除只读变量。

实例

#!/bin/bash
stu_id="twenty"
echo ${stu_id}
    twenty
readonly stu_id
unset stu_id
error:bash: unset: stu_id: 无法反设定: 只读 variable

以上实例执行将没有任何输出。

变量类型

运行shell时,会同时存在三种变量:

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

Shell 字符串

字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号。

单引号

#!/bin/bash
name='twenty-seven'
str='my name is ${name}'
echo $str  #单引号
#输出  my name is ${name}
str2="my name is ${name}"
echo $str2 #双引号
#输出  my name is twenty-seven

单引号字符串的限制:

  • 单引号里的任何字符都会原样输出单引号字符串中的变量是无效的
  • 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。

双引号

name='twenty-seven'
str="Hello, I know you are \"$name\"! \n"
echo $str
#!/bin/bash
name="twenty-seven"
str="hello,the name is "${name}"! \n"
echo $str

#输出结果: hello,the name is twenty-seven! \n
 

双引号的优点:

  • 双引号里可以有变量
  • 双引号里可以出现转义字符

猜你喜欢

转载自blog.csdn.net/qq_40959340/article/details/83993526