20191209 Linux in respect of such a study (4)

4. Vim editor and Shell script command

Vim editor is provided three modes - command mode, line mode, and edit mode.

  • Command mode: control cursor movement, text can copy, paste, delete, and find other work.
  • Input modes: normal text entry.
  • Line mode: To save and exit the document, as well as set the editing environment.

image

Every time you run Vim editor, enter the default command mode, where you need to switch to the input mode and then for document preparation, and each time you have completed the document will need to return to command mode, and then enter the last line mode, save the document or quit the implementation of the operation. In Vim, can not be directly switched from the input mode to the last line mode.

image

image

4.1 Vim text editor

4.1.1 write simple documents

[hwj@localhost ~]$ vim practice.txt

If the document exists, then it is open. If not, it is to create a temporary input file. After opening practice.txt document, the default mode is the command to enter the Vim editor. May be used separately a, i, othree key switches from the command mode to input mode. Where, a key i and the key respectively and a current cursor position is switched to the input mode after the cursor, and then the o key is to create a blank line below cursor, and into the input mode of the editor. After you've written, you want to save and exit, you must first tap the keyboard Escto return to the command mode from input mode.

4.1.2 Configuring the host name

In the Linux system, most of the host name stored in the /etc/hostnamefile, modify the contents of the file to modify the host name.

hostname Command is used to view the current host name, but sometimes change the host name is not immediately synchronized to the system, so if you find that after editing also shows the original host name, you can restart the virtual machine to view it later.

[hwj@hwjsLinux ~]$ hostname
hwjsLinux
[hwj@hwjsLinux ~]$ cat /etc/hostname 
hwj's Linux

4.1.3 NIC configuration information

/etc/sysconfig/network-scripts Directory (to store the configuration file card).

systemctl restart network Command to restart the network card device

ping Command to test whether China Unicom

4.1.4 Configuring Yum repositories

/etc/yum.repos.d/Directory (because the directory to store the Yumsoftware repository configuration file)

4.2 Shell script writing

可以将 Shell 终端解释器当作人与计算机硬件之间的“翻译官”,它作为用户与 Linux 系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了诸如循环、分支等高级编程语言才有的控制结构特性。

Shell 脚本命令的工作方式有两种:交互式和批处理。

  • 交互式(Interactive):用户每输入一条命令就立即执行。
  • 批处理(Batch):由用户事先编写好一个完整的 Shell 脚本, Shell 会一次性执行脚本中诸多的命令。

查看 SHELL 变量可以发现当前系统已经默认使用 Bash 作为命令行终端解释器了:

[root@hwjsLinux yum.repos.d]# echo $SHELL
/bin/bash

4.2.1 编写简单的脚本

[root@linuxprobe ~]# vim example.sh
#!/bin/bash
#For Example BY linuxprobe.com
pwd
ls -al

Shell 脚本文件的名称可以任意,但为了避免被误以为是普通文件,建议将.sh 后缀加上,以表示是一个脚本文件。在上面的这个 example.sh 脚本中实际上出现了三种不同的元素:第一行的脚本声明(#!)用来告诉系统使用哪种 Shell 解释器来执行该脚本;第二行的注释信息(#)是对脚本功能和某些命令的介绍信息,使得自己或他人在日后看到这个脚本内容时,可以快速知道该脚本的作用或一些警告信息;第三、四行的可执行语句也就是我们平时执行的 Linux 命令了。

4.2.2 接收用户的参数

$0 对应的是当前 Shell 脚本程序的名称, $#对应的是总共有几个参数, $*对应的是所有位置的参数值, $?对应的是显示上一次命令的执行返回值,而$1$2$3...则分别对应着第 N 个位置的参数值

[hwj@hwjsLinux ~]$ cat example.sh 
#!/bin/bash
echo "name is $0"
echo "total $# params, they are $*"
echo "first param is $1, three is $3"

[hwj@hwjsLinux ~]$ bash example.sh one two three four
name is example.sh
total 4 params, they are one two three four
first param is one, three is three

4.2.3 判断用户的参数

Shell 脚本中的条件测试语法可以判断表达式是否成立,若条件成立则返回数字 0,否则便返回其他随机数值。条件测试语法的执行格式如图 所示。切记,条件表达式两边均应有一个空格。

image

按照测试对象来划分,条件测试语句可以分为 4 种:

  • 文件测试语句;
  • 逻辑测试语句;
  • 整数值比较语句;
  • 字符串比较语句。

文件测试即使用指定条件来判断文件是否存在或权限是否满足等情况的运算符

image

通过 Shell 解释器的内设$?变量显示上一条命令执行后的返回值

## 测试文件是否为目录类型
[hwj@hwjsLinux ~]$ [ -d /etc/fstab ]
[hwj@hwjsLinux ~]$ echo $?
1

## 测试文件是否为一般文件
[hwj@hwjsLinux ~]$ [ -f /etc/fstab ]
[hwj@hwjsLinux ~]$ echo $?
0

逻辑语句用于对测试结果进行逻辑分析,根据测试结果可实现不同的效果。

  • 逻辑“与”的运算符号是&&,它表示当前面的命令执行成功后才会执行它后面的命令
  • 逻辑“或”,它在 Linux 系统中的运算符号为||,表示当前面的命令执行失败后才会执行它后面的命令
  • 逻辑语句是“非”,在 Linux 系统中的运算符号是一个叹号!,它表示把条件测试中的判断结果取相反值。
[hwj@hwjsLinux ~]$ echo $USER
hwj
[hwj@hwjsLinux ~]$ [ $USER = hwj ] && echo 'true'
true
[hwj@hwjsLinux ~]$ [ $USER != hwj ] ||  echo 'true'
true
[hwj@hwjsLinux ~]$ [ ! $USER = hwj ] ||  echo 'true'
true
[hwj@hwjsLinux ~]$ [ ! $USER = root ] && echo "not root" || "root"
not root

整数比较运算符仅是对数字的操作,不能将数字与字符串、文件等内容一起操作,而且不能想当然地使用日常生活中的等号、大于号、小于号等来判断。因为等号与赋值命令符冲突,大于号和小于号分别与输出重定向命令符和输入重定向命令符冲突。因此一定要使用规范的整数比较运算符来进行操作。

image

[hwj@hwjsLinux ~]$ [ 10 -gt 10 ]
[hwj@hwjsLinux ~]$ echo $?
1
[hwj@hwjsLinux ~]$ [ 10 -eq 10 ]
[hwj@hwjsLinux ~]$ echo $?
0

image

字符串比较语句用于判断测试字符串是否为空值,或两个字符串是否相同。

[hwj@hwjsLinux ~]$ echo $String

[hwj@hwjsLinux ~]$ [ -z $String ]
[hwj@hwjsLinux ~]$ echo $?
0
[hwj@hwjsLinux ~]$ echo $LANG
en_US.UTF-8
[hwj@hwjsLinux ~]$ [ -z $LANG ]
[hwj@hwjsLinux ~]$ echo $?
1

4.3 流程控制语句

4.3.1 if 条件测试语句

if 语句分为单分支结构、双分支结构、多分支结构;

  • if 条件语句的单分支结构由 if、 then、 fi 关键词组成
  • if 条件语句的双分支结构由 if、 then、 else、 fi 关键词组成
  • if 条件语句的多分支结构由 ifthenelseeliffi 关键词组成
[hwj@hwjsLinux ~]$ cat test.sh
#!/bin/bash
read -p "Enter your score(0-100):" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then 
echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
echo "$GRADE is Pass"
else
echo "$GRADE is Fail"
fi

[hwj@hwjsLinux ~]$ bash test.sh 
Enter your score(0-100):90
90 is Excellent

read 是用来读取用户输入信息的命令,能够把接收到的用户输入信息赋值给后面的指定变量, -p 参数用于向用户显示一定的提示信息。

4.3.2 for 条件循环语句

for 循环语句允许脚本一次性读取多个信息,然后逐一对信息进行操作处理,当要处理的数据有范围时,使用 for 循环语句再适合不过了。

使用id命令查看用户的信息

/dev/null 是一个被称作 Linux 黑洞的文件,把输出信息重定向到这个文件等同于删除数据(类似于没有回收功能的垃圾箱)

在 Linux 系统中, /etc/passwd 是用来保存用户账户信息的文件。

脚本中出现的$(命令)是一种完全类似于转义字符中反引号【`命令`】的 Shell 操作符,效果同样是执行括号或双引号括起来的字符串中的命令。

[hwj@hwjsLinux ~]$ cat CheckHost.sh 
#!/bin/bash
HLIST=$(cat ~/ipadds.txt)
for IP in $HLIST
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ] ; then
echo "Host $IP is On-line."
else
echo "Host $IP is Off-line."
fi
done

[hwj@hwjsLinux ~]$ bash CheckHost.sh 
Host 192.168.10.10 is Off-line.
Host http://www.baidu.com is Off-line.
Host 127.0.0.1 is On-line.

4.3.3 while 条件循环语句

while 条件循环语句是一种让脚本根据某些条件来重复执行命令的语句,它的循环结构往往在执行前并不确定最终执行的次数,完全不同于 for 循环语句中有目标、有范围的使用场景。 while 循环语句通过判断条件测试的真假来决定是否继续执行命令,若条件为真就继续执行,为假就结束循环。

[hwj@hwjsLinux ~]$ cat Guess.sh 
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "Price is between 0-999, Guess it"
while true
do
read -p "Pleas Enter Num you Guess:" INT
let TIMES++
if [ $INT -eq $PRICE ] ; then
echo "TRUE, Price is $PRICE"
echo "Guess $TIMES times"
exit 0
elif [ $INT -gt $PRICE ]; then
echo "HIGHER!!!"
else
echo "LOWER!!!"
fi
done

4.3.4 case 条件测试语句

case 条件测试语句和 switch 语句的功能非常相似! case 语句是在多个范围内匹配数据,若匹配成功则执行相关命令并结束整个条件测试;而如果数据不在所列出的范围内,则会去执行星号(*)中所定义的默认命令。

[hwj@hwjsLinux ~]$ cat CheckKeys.sh 
#!/bin/bash
read -p "Please Enter a character:" KEY
case "$KEY" in
[a-z]|[A-Z])
echo "Alpha"
;;

[0-9])
echo "Number"
;;

*)
echo "Other"
esac

4.4 计划任务服务程序

计划任务分为一次性计划任务与长期性计划任务,大家可以按照如下方式理解。

  • 一次性计划任务:今晚 11 点 30 分开启网站服务。
  • 长期性计划任务:每周一的凌晨 3 点 25 分把/home/wwwroot 目录打包备份为backup.tar.gz

如果想要查看已设置好但还未执行的一次性计划任务,可以使用“at -l”命令;要想将其删除,可以用“atrm 任务序号”。在使用 at 命令来设置一次性计划任务时,默认采用的是交互式方法。

[hwj@hwjsLinux ~]$ at 23:00
at> systemctl restart httpd
at> <EOT>
job 4 at Thu Dec  5 23:00:00 2019
You have new mail in /var/spool/mail/hwj
[hwj@hwjsLinux ~]$ at -l
2   Thu Dec  5 23:30:00 2019 a hwj
4   Thu Dec  5 23:00:00 2019 a hwj
[hwj@hwjsLinux ~]$ atrm 2
[hwj@hwjsLinux ~]$ at -l
4   Thu Dec  5 23:00:00 2019 a hwj
## 以非交互式方式创建一次性任务
[hwj@hwjsLinux ~]$ echo "systemctl restart httpd" | at 23:05
job 5 at Thu Dec  5 23:05:00 2019
[hwj@hwjsLinux ~]$ at -l
4   Thu Dec  5 23:00:00 2019 a hwj
5   Thu Dec  5 23:05:00 2019 a hwj

crondServices created periodically, regularly perform certain specific tasks. Create, edit scheduled tasks command to crontab -eview the current scheduled tasks command crontab -l, delete a scheduled task command crontab -r. In addition, if you are logged in as the administrator system, you can also crontabadd commands -uto edit someone else's plan task parameters.

image

Except that a comma (,) respectively represent the plurality of time periods, such as "8,9,12" denotes August, September and December. May also be minus (-) to indicate a period of successive time periods (e.g., the value field, "date" is "12-15", it means that 12 to 15 per month). And the time interval represented by the tasks division sign (/) (e.g., "* / 2" represents a task performed every 2 minutes)

In the crondscheduled task parameters for the service, all the way commands must use absolute paths to write, if you do not know the absolute path, use the whereisquery command, rmthe command path for the output information in the following bold part.

Note the use of the service plan at work.

  • In the crondconfiguration parameters for the service, you can order as like Shell scripts #beginning number written on the annotation information, so you can quickly look back at a later date for important information about its features, requirements, and writers such as when this command code.
  • Scheduled Tasks "minute" field must have value, must not be empty or *number "day" and "week" field can not be used, otherwise the conflict will occur.

Guess you like

Origin www.cnblogs.com/huangwenjie/p/12013014.html