shell构建基本脚本

转载自我的太阳
大多数的Linux发行版的默认shell都是GNU bash shell。CentOS 7 系统默认的是bash shell,bash shell 提供了基本的命令来操作Linux系统。

1.1 shell 的启动

GNU bash shell提供对Linux系统的交互访问。以应用程序运行在Linux系统上,在用户登录到终端时自动启动。shell启动依赖于用户账户的配置。系统根据用户账户的配置来启动何种shell(一个Linux系统可能安装了多个shell)。
下面是leo这个账户的配置信息,配置信息存储在/etc/passwd文件中。

leo: x:1001:1001::/home/leo:/bin/bash //这个地方应该是没有空格的

这里有7个字段,用冒号隔开,分别对应
登录名:密码:UID:GID:账户文本描述:home目录的位置:用户的默认shell
这里可以看到用户的默认shell是bash,是在/bin/目录下的bash这个应用程序,当用户登录时系统就会启动执行/bin/bash而启动shell。还可以将shell定义为/usr/bin/bash,其实和/bin/bash是一样的,因为/bin/目录下的bash是一个软链接,也可以说是/usr/bin/bash在/bin/目录下的一个快捷方式。如果要改变用户的默认shell为cshell,可以使用命令

usermod -s /bin/cshell

1.2 常用的命令

cd #目录切换命令
ls #列出文件列表
pwd #显示当前所在目录路径
touch #创建文件
mkdir #创建文件夹
cp #复制文件或文件夹
mv #移动文件或文件夹
ln #链接文件
rm #删除文件或文件夹
rmdir #删除目录,只能删除空目录
tree #以树形展示目录结构
file #查看文件或目录的类型
cat #显示文本文件内容

2.1 实践操作

[leo@orig ~]$ pwd
/home/leo #当前所在目录是leo用户的home目录,用“~”表示
[leo@orig ~]$ ls -F #目录下有6个文件夹,加上-F区分文件夹和文件
Desktop/ Documents/ Downloads/ Music/ Pictures/ Videos/
[leo@orig ~]$ mkdir mydir #创建目录
[leo@orig ~]$ cd mydir/ #切换到目录 mydir
[leo@orig mydir]$ ls #列出目录内容为空
[leo@orig mydir]$ mkdir a #创建目录啊,a,b,c,和文件d
[leo@orig mydir]$ mkdir b
[leo@orig mydir]$ mkdir c
[leo@orig mydir]$ ls -F
a/ b/ c/ d
[leo@orig mydir]$ rmdir a/ #删除文件夹a
[leo@orig mydir]$ cd … #回到上一级目录
[leo@orig ~]$ rmdir mydir/ #删除目录mydir
rmdir: 删除 “mydir/” 失败: 目录非空
[leo@orig ~]$ rm -rf mydir/ #rm命令强制删除目录mydir,删除时没有提示(慎用)
[leo@orig ~]$ ls -F
Desktop/ Documents/ Downloads/ Music/ Pictures/ Videos
[leo@orig ~]$ touch hello #创建问价hello
[leo@orig ~]$ ls
Desktop Documents Downloads hello Music Pictures Videos
[leo@orig ~]$ mkdir dir #创建文件夹
[leo@orig ~]$ ls
Desktop dir Documents Downloads hello Music Pictures Videos
[leo@orig ~]$ mkdir mydir #创建文件夹mydir
[leo@orig ~]$ ls
Desktop dir Documents Downloads hello Music mydir Pictures Videos
[leo@orig ~]$ cp mydir/ dir/ #将mydir文件夹复制到dir文件夹中,失败!
cp: 略过目录"mydir/"
[leo@orig ~]$ ls
Desktop dir Documents Downloads hello Music mydir Pictures Videos
[leo@orig ~]$ cp hello dir/ #复制文件到dir文件夹
[leo@orig ~]$ cp -R mydir/ dir/ #将mydir文件夹复制到dir文件夹中,失败!
[leo@orig ~]$ ls dir/ #查看dir目录下已经有hello文件和mydir文件夹
hello mydir
[leo@orig ~]$ echo ‘hello world!’ >> dir/hello #向hello文件写入内容hello world!
[leo@orig ~]$ cat dir/hello #输出文件内容
hello world!
[leo@orig ~]$ file dir/hello #查看文件类型
dir/hello: ASCII text #ASCII编码的文本文件
[leo@orig ~]$ ln -s dir/hello sl_hello #创建dir/hello在当前目录下的软链接
[leo@orig ~]$ ls -l
总用量 32
drwxrwxr-x 2 leo leo 4096 3月 31 21:08 Desktop
drwxrwxr-x 3 leo leo 4096 7月 13 16:13 dir
drwxr-xr-x 4 leo leo 4096 7月 2 20:58 Documents
drwxr-xr-x 7 leo leo 4096 5月 24 10:47 Downloads
-rw-rw-r-- 1 leo leo 0 7月 13 16:11 hello
drwxrwxr-x 2 leo leo 4096 3月 31 21:07 Music
drwxrwxr-x 2 leo leo 4096 7月 13 16:11 mydir
drwxrwxr-x 2 leo leo 4096 3月 31 21:07 Pictures
lrwxrwxrwx 1 leo leo 9 7月 13 16:27 sl_hello -> dir/hello #软链接,指向dir/hello
drwxrwxr-x 2 leo leo 4096 3月 31 21:07 Videos
[leo@orig ~]$ ln -P dir/hello hl_hello #创建硬链接
[leo@orig ~]$ file hl_hello
hl_hello: ASCII text
[leo@orig ~]$ file sl_hello
sl_hello: symbolic link to `dir/hello’
######软链接的文件与源文件并不相同,从文件类型就可以看出,硬链接文件与源文件本质上是同一个文件,更改其中任何一个文件内容,另一个文件的内容都会同步改变

3.1 一次使用多个命令

前面讲了shell基本命令,但都是一次输入一条命令,下面我们来一次性使用多条命令。
下面的命令是先显示出当前所在目录的路径,再列出目录下的文件和文件夹(隐藏文件或文件夹不会列出来),最后列出目录下各文件或文件夹的详细信息。
[leo@orig ~]$ pwd;ls;ls -l
/home/leo
Desktop Documents hello Music Pictures Videos
dir Downloads hl_hello mydir sl_hello
总用量 36
drwxrwxr-x 2 leo leo 4096 3月 31 21:08 Desktop
drwxrwxr-x 3 leo leo 4096 7月 13 16:13 dir
drwxr-xr-x 4 leo leo 4096 7月 2 20:58 Documents
drwxr-xr-x 7 leo leo 4096 5月 24 10:47 Downloads
-rw-rw-r-- 1 leo leo 0 7月 13 16:11 hello
-rw-rw-r-- 2 leo leo 19 7月 13 16:32 hl_hello
drwxrwxr-x 2 leo leo 4096 3月 31 21:07 Music
drwxrwxr-x 2 leo leo 4096 7月 13 16:11 mydir
drwxrwxr-x 2 leo leo 4096 3月 31 21:07 Pictures
lrwxrwxrwx 1 leo leo 9 7月 13 16:27 sl_hello -> dir/hello
drwxrwxr-x 2 leo leo 4096 3月 31 21:07 Videos

一次性使用多条命令时,命令之间用分号隔开

3.2 创建第一个shell脚本文件

创建shell脚本文件时,必须在第一行指定要使用的shell,格式为:

#!/bin/bash #使用bash
#符号用作注释,shell不会处理注释的内容,第一行除外。
[leo@orig scripts]$ vim test01 #用vim编辑文件,若存在test01这个文件,则打开文件,否则创建该文件
[leo@orig scripts]$ cat test01 #下面是写好的脚本内容
#!/bin/bash
#输出当前时间和当前登录到系统的用户
echo -n 'The time and date are: ’
date #输出当前时间
echo “Let’s see who’s logged into the system”
who #查看当前登录到系统的用户
[leo@orig scripts]$ ls -l
总用量 4
-rw-rw-r-- 1 leo leo 154 7月 13 18:50 test01 #可以看出当前用户并没有脚本文件的执行权限
[leo@orig scripts]$ chmod +x test01 #赋予当前用户脚本文件的执行权限
[leo@orig scripts]$ ls -l
总用量 4
-rwxrwxr-x 1 leo leo 154 7月 13 18:50 test01 #已赋予权限,当前用户拥有对test01的读写和执行的权限
[leo@orig scripts]$ ./test01 #执行脚本
The time and date are: 2018年 07月 13日 星期五 18:58:16 CST
Let’s see who’s logged into the system
leo pts/0 2018-07-13 18:31 (220.175.138.28) #当前登录的用户
leo pts/1 2018-07-13 15:20 (223.104.170.211) #未正确退出登录的用户

3.3 shell脚本中使用变量

Linux系统的环境变量是在系统中的具有特定名称的对象,通常在系统启动的时候定义系统级的环境变量并赋值,在用户登录的时候定义用户级的环境变量。

3.3.1 使用系统环境变量

[leo@orig scripts]$ vim test02
[leo@orig scripts]$ cat test02
#!/bin/bash
#显示当前登录用户的信息
echo “User info for userid: $USER”
echo UID: $UID
echo HOME: H O M E [ l e o @ o r i g s c r i p t s ] HOME [leo@orig scripts] HOME[leo@origscripts] chmod +x test02
[leo@orig scripts]$ ./test02
User info for userid: leo
UID: 1001
HOME: /home/leo

3.3.2 使用局部变量

局部变量是由用户自定义的变量,在整个脚本中使用。由字母、数字或下划线组成,区分大小写。在使用的时候通过$符号引用。
如:定义变量并赋值 name=“Tom” ,使用变量 My name is $name #使用了name变量

[leo@orig scripts]$ vim test03
[leo@orig scripts]$ cat test03
#!/bin/bash
#使用局部环境变量
days=10 #定义变量days并赋值10
guest=“Tom” #定义变量guest并赋值Tom
echo "$guest checked in KaTeX parse error: Expected 'EOF', got '#' at position 16: days days ago" #̲使用days和guest变量 …guest checked in KaTeX parse error: Expected 'EOF', got '#' at position 18: …ys days ago" #̲使用重新赋值后的变量 [leo… chmod +x test03
[leo@orig scripts]$ ./test03
Tom checked in 10 days ago
Jee checked in 5 days ago

3.4 将shell命令的输出结果赋值给变量

[leo@orig scripts]$ cat test04
#!/bin/bash
date1=date #将date命令的输出结果赋值给date1变量
date2= ( d a t e ) e c h o " (date) echo " (date)echo"date1"
echo " d a t e 2 " [ l e o @ o r i g s c r i p t s ] date2" [leo@orig scripts] date2"[leo@origscripts] ./test04 #运行脚本,输出date1和date2的值
2018年 07月 15日 星期日 08:32:25 CST
2018年 07月 15日 星期日 08:32:25 CST

4.1 重定向输入和输出

4.1.1 输出重定向

基本的输出重定向将命令的输出结果保存到一个文件中,重定向采用符号“>”,格式为 cmd > outputfile

[leo@orig scripts]$ date > test06 #将date命令的输出结果重定向到test06文件
[leo@orig scripts]$ cat test06 #查看test06文件的内容
2018年 07月 15日 星期日 18:18:00 CST
[leo@orig scripts]$ echo “Hello world” > test06 #再将echo命令输出结果重定向到test06
[leo@orig scripts]$ cat test06 #查看结果,新的重定向输出已经将test06原来的内容覆盖了
Hello world

“>”符号重定向输出会覆盖掉原来的数据,使用“>>”符号进行重定向输出会在原来的数据后追加数据。
[leo@orig scripts]$ cat test06
Hello world
[leo@orig scripts]$ echo “shell” >> test06
[leo@orig scripts]$ cat test06
Hello world
shell

4.1.2 输入重定向

输入重定向将文件的内容重定向到命令。cmd < inputfile
wc命令可以对文本进行计数,默认输出三个值:行数,单词
[leo@orig scripts]$ wc < test06
2 3 18
另一种输入重定向方法称为内联输入重定向,符号是<<。除此之外,必须指明一个文本标记来划分输入数据的开始和结尾,任何字符串都可以作为文本标记,但必须一致。
[leo@orig scripts]$ grep sh << EOF

java
c++
python
c#
shell
php
EOF
shell
#####grep命令是一个文本检索命令,EOF是文本标记或者说是定界符,EOF之间的内容就是输入的内容,以上是使用grep命令在输入中检索出有sh字符串的行。

4.1.3 输出重定向和输入重定向结合使用

    有时需要将命令的输出结果作为另一个命令的输入,这是就可以使用输出重定向将命令输出结果重定向到一个中间文件,再使用输入重定向将中间文件的内容重定向到命令的输入。

[leo@orig scripts]$ rpm -qa > test07 #将命令rpm -qa 的输出结果重定向到文件test07
[leo@orig scripts]$ grep mysql < test07 #将文件test07的内容重定向到grep命令的输入
mysql-community-server-5.7.22-1.el7.x86_64
php72w-mysqlnd-7.2.4-1.w7.x86_64
mysql-community-common-5.7.22-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.22-1.el7.x86_64
mysql-community-libs-compat-5.7.22-1.el7.x86_64
mysql-community-libs-5.7.22-1.el7.x86_64
##以上命令的意思是:通过rpm命令列出服务器安装的rpm软件包,再从中检索出包含mysql的软件包,简单点就是查看服务器是否安装了mysql软件。

4.2 管道

重定向需要将结果输出到文件,在从文件重定向输入到命令,比较繁琐,中间有文件生产。我们用不着将命令输出重定向到文件中,可以将其直接重定向到另一个命令,这个过程叫做管道连接。
cmd1 | cmd2 | …
命令1与命令2同时执行,命令1输出会立即发送到命令2,数据传输不会用到任何中间文件或缓冲区。
[leo@orig scripts]$ rpm -qa | grep mysql
mysql-community-server-5.7.22-1.el7.x86_64
php72w-mysqlnd-7.2.4-1.w7.x86_64
mysql-community-common-5.7.22-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.22-1.el7.x86_64
mysql-community-libs-compat-5.7.22-1.el7.x86_64
mysql-community-libs-5.7.22-1.el7.x86_64

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值