Linux--Shell脚本中的基础知识

Shell

Shell 是操作系统中的一个软件

它包在 Linux 内核外面,为用户和内核之间的交互提供了一个接口

系统中的命令用 Shell 去解释

Shell 接收系统回应的输出并显示其到屏幕中

bash = GNU Bourne-Again Shell
在这里插入图片描述

Shell 脚本

脚本是一种解释性的语言

Shell 脚本保存执行动作

用脚本判定命令的执行条件

用脚本来实现动作的批量执行

创建 Shell 脚本

vim script.sh

使用 vim 编写脚本

#!/bin/bash

脚本使用的解释器,通常用幻数 “#!” 指定

AUTHOR

脚本作者

DATE

脚本创作时间

MAIL

脚本作者联系方式

VERSION

脚本的版本

第一个 Shell 脚本

vim test.sh

编辑脚本

cat test.sh

#!/bin/bash

#! — 幻数:指定脚本所使用的解释器
/bin/bash
使用 bash 解释器
echo Hello World !!!

在这里插入图片描述

sh test.sh

执行脚本

在这里插入图片描述

Shell 脚本的四种执行方式

vim watch.sh

cat watch.sh

watch -n 1 date

1秒监控一次 date 命令

在这里插入图片描述

sh watch.sh

在这里插入图片描述

source watch.sh

在这里插入图片描述

. watch.sh

在这里插入图片描述

chmod +x watch.sh

./watch.sh

在这里插入图片描述

制作 Shell 脚本简介

vim /etc/vimrc

编辑 /etc/vimrc 配置文件

map <F9> ms:call TEST()<cr>'s

设置 F9 快捷键执行 TEST 函数

function TEST()

call append(0,"########################")

第 0 行添加 ################## 符

注:

计算机是从 0 开始计算

在这里插入图片描述

新建文本

使用 F9 快捷键后效果

在这里插入图片描述

call append(1,"#Name: #")

第 2 行添加 #Name: #

在这里插入图片描述

F9

在这里插入图片描述

call append(1,"#Name:Leon #")

第 2 行添加 #Name:Leon #

call append(2,"#Age:20 #")

第 3 行添加 #Age:20 #

call append(3,"#Create_Date:".strftime("%Y-%m-%d %H:%M). "#")

第 4 行添加 #Create_Date:".strftime("%Y-%m-%d %H:%M). "#

同步建立 Shell 脚本时间

call append(4,"##########")

第 5 行添加 ##########

在这里插入图片描述

F9

在这里插入图片描述

map <F9> ms:call TEST()<cr>'s

设置 F9 快捷键执行 TEST 函数

function TEST()

call append(0,"########################")

第 0 行添加 ################## 符

call append(1,"#Name:Leon #")

第 2 行添加 #Name:Leon #

call append(2,"#Age:20 #")

第 3 行添加 #Age:20 #

call append(3,"#Create_Date:".strftime("%Y-%m-%d %H:%M). "#")

第 4 行添加 #Create_Date:".strftime("%Y-%m-%d %H:%M). "#

同步建立 Shell 脚本时间

call append(4,"##########")

第 5 行添加 ##########

call append(5,"")

第 6 行添加 空行

call append(6,"#!/bin/bash")

第 7 行添加 幻数

endfunction

结束

在这里插入图片描述

F9

模板

在这里插入图片描述

" map <F9> ms:call TEST()<cr>'s

注销快捷键

autocmd BufNewFile *.sh exec ":call TEST()"

添加:在新建立的 Shell 脚本里,自动执行函数生成 Shell 脚本简介

在这里插入图片描述

vim file1.sh

新建立 file1.sh

cat file1.sh

自动执行函数生成 Shell 脚本简介

在这里插入图片描述

touch file2.sh

建立 file2.sh

vim file2.sh

再编辑 file2.sh

cat file2.sh

无法自动执行函数生成 Shell 脚本简介

在这里插入图片描述

Shell 脚本调试

-x

适用于所有 Shell 脚本

vim test.sh

echo ""

echo Hello World !!!

echo ""

cal

cat

在这里插入图片描述

sh test.sh

命令行卡住,有未执行的错误命令

ctrl + c

结束

在这里插入图片描述

sh -x test.sh

调试脚本,查看问题

在这里插入图片描述

ctrl + c

cat -n test.sh

补全命令

sh -x test.sh
再次执行脚本
在这里插入图片描述

Shell 脚本实验

实验一

执行 ip_show.sh

显示当前主机的 ip 地址

vim ip_show.sh

#!/bin/bash

echo "host ipaddress:

ifconfig | awk '/inet\>/&&!/127.0.0.1/{print $2}'"

sh ip_show.sh

在这里插入图片描述

实验二

执行user_show.sh

显示当前主机中能登陆系统的用户

vim user_show.sh

#!/bin/bash

echo "Login user list: "

echo “”

echo “awk -F : '/bash$/||/sh/{print $1}' /etc/passwd

sh user_show.sh
在这里插入图片描述

实验三

执行host_message.sh

显示当前主机的名称、ip 以及能够登陆系统的用户

vim user_show.sh

#!/bin/bash

显示当前主机的名称

echo "hostname: hostname"

echo “”

显示当前主机的 ip

echo "host ipaddress:

ifconfig | awk '/inet\>/&&!/127.0.0.1/{print $2}'"

echo “”

能够登陆系统的用户

echo "Login user list: "

echo “”

echo “awk -F : '/bash$/||/sh/{print $1}' /etc/passwd

sh host_message.sh

在这里插入图片描述

实验四

clear_log.sh

执行命令清空日志

vim clear_log.sh

#!/bin/bash

[ “$USER” = “root” ] && {

> /var/log/messages

echo -e “\033[32m clean /var/log/messages successful!!\033[0m”

} || {

echo -e “\033[31m This script must run with root !!\033[0m”

}

sh clear_log.sh

在这里插入图片描述

在这里插入图片描述

在这个实验中,我们用到了 改变字符颜色功能
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章