第一题
截止到2020.6,最快的超级计算机:
名称:Supercomputer Fugaku - Supercomputer Fugaku, A64FX 48C 2.2GHz, Tofu interconnect D, Fujitsu RIKEN Center for Computational Science
所在位置:日本
使用的CPU型号与规格:富士通ARM处理器 A64FX 48C 2.2GHz
总共使用CPU数量:7,299,072
第二题:
CPU:AMD Athlon II X2 240,外频:200Mhz,倍频:14X,主频为2.8GHz。
内存:4GB 1600MHz DDR3 Non-ECC CL11 DIMM 1Rx8
第三题:
CPU:Inter Pentium G620 2.6GHz
内存:4GBytes;DDR3
显卡:PCI Express 3.0;2GBytes
主板:ASUSTEK COMPUTER INC;Intel H61(南桥)。
总结
:如果是Windows操作系统可以使用鲁大师软件或者CPUID CPU-Z软件查询电脑硬件配置。如果是Linux操作系统可以使用cat /proc/cpuinfo或者lspci等命令查看各项组件型号。
第四题:
(1)DMI2 5GT/S;(2)三级缓存 8MB;(3)16条PCIe 通道。
第五题:
(1)SATA3(6Gbps,即6Gbit/s);(2)读:280MB/S(SATA2),550MB/S(SATA3);写:260MB/S(SATA2),520MB/S(SATA3);(3)IOPS的值为:6187(读),17740(写)。
总结
:要查询目前linux内核目前的最新版本可以直接登陆网站(https://www.kernel.org/)进行查询即可。如果要直接在Linux系统中查询内核版本,可以通过命令(uname -r)来查询。总结
:详细的Android与Linux内核的对应关系参考:https://blog.csdn.net/ly890700/java/article/details/75040704总结
:查询硬件资料的时候可以直接在百度上面查,也可以直接在对应硬件的官网查,前提是知道硬件的型号。总结
:如果是Windows操作系统可以使用鲁大师软件或者CPUID CPU-Z软件查询电脑硬件配置。如果是Linux操作系统可以使用cat /proc/cpuinfo或者lspci等命令查看各项组件型号。总结
:要查询目前linux内核目前的最新版本可以直接登陆网站(https://www.kernel.org/)进行查询即可。如果要直接在Linux系统中查询内核版本,可以通过命令(uname -r)来查询。总结
:详细的Android与Linux内核的对应关系参考:https://blog.csdn.net/ly890700/java/article/details/75040704总结
:总结
:所有的LC_*的值会和LANG保持一致,如果LANG没有值那么所有的LC_*会采用默认值,如果LC_ALL有值的话,所有的LC_*会合LC_ALL一样,但是改变LC_ALL并不会改变LANG的值。另外还有一点需要注意,纯命令行模式(ctrl + alt + F2~6可以切换)下,如果采用中文字符,命令输出中文信息的时候会出现乱码,因此需要改成英文字符集。还有一点要注意,命令更改字符集之后,如果重启之后会回到系统一开始的默认值。如果要让系统默认的字符集是XXX的话,可以修改系统配置文件/etc/locale.conf的内容(可以使用nano编辑器来进行修改)。1、第一题:
2、第二题:
3、第三题:
4、第四题:
5、第五题:
6、第六题:
7、第七题:
8、第八题:
9、第九题:
10、第十题:
11、第十一题:
12、第十二题:
13、第十三题:
14、第十四题:
15、第十五题:
16、第十六题:
17、第十七题:
18、第十八题:
19、第十九题:
20、第二十题:
21、第二十一题:
1、关于如何创建新的用户以及将用户添加到某个用户组的操作
1、第一题:
2、第二题:
3、第三题:
4、第四题:
5、第五题:
6、第六题:
1、如何取出shell中的变量的值
1、关于软硬链接的说明
1、第一题:
2、第二题:
3、第三题:
4、第四题:
5、第五题:
6、第六题:
7、第七题:
8、第八题:
9、第九题:
10、第十题:
11、第十一题:
1、关于为什么目录【/】、【/boot】和【/home】inode相同问题的说明。
1、如何设置启动挂载?
这样子设置之后系统就会根据启动配置文件/etc/fstab中的信息将挂载信息写入到真正记录文件系统挂载的/etc/mtab和/etc/mounts这两个文件中。这样就算是重起也不会消失了。
1、第一题
2、第二题
1、第一题
2、第二题
3、第三题
4、第四题
5、第五题
6、第六题
7、第七题
8、第八题
9、第九题
10、第十题
11、第十一题
12、第十二题
13、第十三题
14、第十四题
15、第十五题
16、第十六题
1、关于环境变量和普通变量的区别
1、关于环境变量和自定义变量的补充说明
1、数据重定向中关于‘1>&2’和’2>&1’的说明
1、情景模拟题,关于图形界面注销用户无法执行.bash_logout文件中命令的说明
2、第一题
3、第二题
4、第三题
5、第四题
6、第五题
7、第六题
8、第七题
9、第八题
10、第九题
11、第十题
12、第十一题
13、第十二题
14、第十三题
15、第十四题
16、第十五题
17、第十六题
18、第十七题
19、第十八题
20、第十九题
21、第二十题
22、第二十一题
1、第一题
2、第二题
1、交互式脚本,变量内容由用户决定
我的写法。
#!/bin/bash
# Program:
# User inputs his first name and last name. Program shows his full name.
# History:
# 2020/08/08 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p 'Please input your first name: ' firstname
read -p 'Please input your last name: ' lastname
echo -e "\nYour full name is ${firstname} ${lastname}"
exit 0
[948cshell@localhost bin]$ sh showname.sh
Please input your first name: Tao
Please input your last name: Xincan
Your full name is Tao Xincan
2、随日期变化,利用date创建文件
#!/bin/bash
# Program:
# Program create three files, which named by user's input and date command.
# History:
# 2020/08/08 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p 'What is your filename: ' filename
touch "${filename}_$(date +%F | sed 's/-//g')"
touch "${filename}_$(date -d "-1 day" +%F | sed 's/-//g')"
touch "${filename}_$(date -d "-2 day" +%F | sed 's/-//g')"
exit 0
[948cshell@localhost bin]$ sh my_create_3_filename.sh
What is your filename: testfile
[948cshell@localhost bin]$ ll
总用量 16
-rw-rw-r--. 1 948cshell 948cshell 1121 8月 9 16:04 create_3_filename.sh
-rwxrw-r--. 1 948cshell 948cshell 208 8月 9 15:58 hello.sh
-rw-rw-r--. 1 948cshell 948cshell 461 8月 9 16:04 my_create_3_filename.sh
-rwxrw-r--. 1 948cshell 948cshell 405 8月 9 16:02 showname.sh
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:04 testfile_20200807
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:04 testfile_20200808
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:04 testfile_20200809
#!/bin/bash
# Program:
# Program create three files, which named by user's input and date command.
# History:
# 2020/08/09 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1、让使用者输入文件名称,并取得fileuser这个变量
echo -e "I will use 'touch' command to create 3 files." # 纯粹的显示信息
read -p "Please input your filename: " fileuser # 提示使用者输入
# 2、为了避免使用者随意按Enter,利用变量功能分析文件名是否有设置?
# 开始判断是否有配置文件名,该语句的意思是如果fileuser为空白就将
# filename这个字符串赋值给filename变量。
filename=${
fileuser:-"filename"}
# 3、开始利用date命令来取得所需要的文件名
date1=$(date --date='2 days ago' +%Y%m%d) # 前两天的日期
date2=$(date --date='1 days ago' +%Y%m%d) # 前一天的日期
date3=$(date +%Y%m%d) # 今天的日期
file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3}
#3、将文件名建立起来
touch "${file1}"
touch "${file2}"
touch "${file3}"
当输入空白文件名的情况如下。
[948cshell@localhost bin]$ sh create_3_filename.sh
I will use 'touch' command to create 3 files.
Please input your filename:
[948cshell@localhost bin]$ ll
总用量 16
-rw-rw-r--. 1 948cshell 948cshell 1121 8月 9 16:04 create_3_filename.sh
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:07 filename20200807
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:07 filename20200808
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:07 filename20200809
-rwxrw-r--. 1 948cshell 948cshell 208 8月 9 15:58 hello.sh
-rw-rw-r--. 1 948cshell 948cshell 461 8月 9 16:04 my_create_3_filename.sh
-rwxrw-r--. 1 948cshell 948cshell 405 8月 9 16:02 showname.sh
当输入非空白文件名的情况如下。
[948cshell@localhost bin]$ sh create_3_filename.sh
I will use 'touch' command to create 3 files.
Please input your filename: testfile
[948cshell@localhost bin]$ ll
总用量 16
-rw-rw-r--. 1 948cshell 948cshell 1121 8月 9 16:08 create_3_filename.sh
-rwxrw-r--. 1 948cshell 948cshell 208 8月 9 15:58 hello.sh
-rw-rw-r--. 1 948cshell 948cshell 461 8月 9 16:04 my_create_3_filename.sh
-rwxrw-r--. 1 948cshell 948cshell 405 8月 9 16:02 showname.sh
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:09 testfile20200807
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:09 testfile20200808
-rw-rw-r--. 1 948cshell 948cshell 0 8月 9 16:09 testfile20200809
3、数值运算,简单的加减乘除
!/bin/bash
# Program:
# User inputs 2 integer numbers ; program will cross these two numbers.
# History:
# 2020/08/08 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "You should input 2 numbers, I will multiplying them! \n"
declare -i firstnum
declare -i secondnum
read -p "Please input the first number: " firstnum
read -p "Please input the second number: " secondnum
declare -i sum=${firstnum}*${secondnum}
echo $sum
exit 0
[948cshell@localhost bin]$ sh my_multiplying.sh
You should input 2 numbers, I will multiplying them!
Please input the first number: 2
Please input the second number: 3
6
#!/bin/bash
# Program:
# User inputs 2 integer numbers ; program will cross these two numbers.
# History:
# 202/0/08/09 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "You SHOULD input 2 numbers, I will multiply them! \n"
read -p "first number: " firstnu
read -p "second number: " secnu
# 可以通过【result=$((计算式))】计算整数运算的值,并把结果赋值给result变量。
# 以下代码也可以写成【declare -i total=${firstnu}*${secnu}】,只是这种写法中
# 表达式之间不能使用空格,第一种表达式之间是可以使用空格的。
total=$((${firstnu}*${secnu}))
echo -e "\n The result of ${firstnu} x ${secnu} is ==> ${total}"
exit 0
[948cshell@localhost bin]$ sh multiplying.sh
You SHOULD input 2 numbers, I will multiply them!
first number: 2
second number: 3
The result of 2 x 3 is ==> 6
4、数值运算,通过bc计算Pi
#!/bin/bash
# Program:
# User input a scale number to calculate pi number
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "This program will calculate pi value. \n"
echo -e "You should input a float number to calculate pi value. \n"
read -p "The scale number (10~10000) ? " checking
# 如果checking输入的是空白,就将10赋值给num变量
num=${
checking:-"10"}
echo -e "Starting calculate pi value. Be patient."
# time命令后面跟命令,用来计算后面所跟的命令执行所花的时间。
# echo命令经常和bc命令通过管道来进行配合,bc命令会将echo
# 命令的引号中的语句按照分号进行分割,依次执行。bc命令中的-l
# 选项是调用标准函数库的意思,4*a(1)是其中的一个函数,会返回
# pi的值。
time echo "scale=${num} ; 4*a(1)" | bc -lq
exit 0
[948cshell@localhost bin]$ sh cal_pi.sh
This program will calculate pi value.
You should input a float number to calculate pi value.
The scale number (10~10000) ?
Starting calculate pi value. Be patient.
3.1415926532
real 0m0.005s
user 0m0.003s
sys 0m0.003s
1、关于使用source命令运行shell脚本的时候,脚本最后【exit 0】语句的说明。
1、关于test命令的脚本范例
#!/bin/bash
# Program:
# User input a filename, program will check the following:
# 1、exist?
# 2、file / directory
# 3、file permission
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input a filename: " filename
# 下面的语句本来是想判断文件存在则继续执行下面的语句,不存在则退出,可是由于
# 没有学习脚本中的if语句,所以不太会写。于是就写了下面这个错误的版本。
test -e "${filename}" && echo "exist" || echo "Not exist" && exit 0
test -f "${filename}" && echo "This is a file" || echo "This is a directory"
test -r "${filename}" && perm="${perm} readable"
test -w "${filename}" && perm="${perm} writable"
test -x "${filename}" && perm="${perm} executable"
echo "User's permission are $perm"
#!/bin/bash
# Program:
# User input a filename, program will check the following:
# 1、exist?
# 2、file / directory
# 3、file permission
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1、让输入者输入文件名,并且判断使用者是否真的有输入字符
echo -e "Please input a filename, I will check the filename's type and permission. \n\n"
read -p "Input a filename: " filename
test -z ${filename} && echo "You MUST input a filename." && exit 0
# 2、判断文件是否存在?若不存在则显示信息并结束脚本
test ! -e ${filename} && echo "The filename '${filename}' DO NOT exist" && exit 0
# 3、开始判断文件类型与属性
test -f ${filename} && filetype="regular file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -f ${filename} && perm="${perm} writable"
test -f ${filename} && filetype="${perm} executable"
# 4、开始输出信息
echo "The filename: ${filename} is a ${filetype}"
echo "And the permissions for you are : ${perm}"
1、关于中括号作为判断符号的范例。
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input your choice : " choice
[ "${choice}" == "Y" -o "${choice}" == "y" ] && echo "OK, continue"
[ "${choice}" == "N" -o "${choice}" == "n" ] && echo "Oh, interrupt"
[ "${choice}" != "Y" -a "${choice}" != "y" -a "${choice}" != "N" -a "${choice}" != "n" ] && echo "I don't know what your choice is"
[948cshell@localhost bin]$ sh ans_yn.sh
Please input your choice : y
OK, continue
[948cshell@localhost bin]$ sh ans_yn.sh
Please input your choice : Y
OK, continue
[948cshell@localhost bin]$ sh ans_yn.sh
Please input your choice : n
Oh, interrupt
[948cshell@localhost bin]$ sh ans_yn.sh
Please input your choice : N
Oh, interrupt
[948cshell@localhost bin]$ sh ans_yn.sh
Please input your choice : d
I don't know what your choice is
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N) :" yn
[ "${yn}" == "Y" -o "${yn}" == "y" ] && echo "OK, continue" && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo "Oh, interrupt" && exit 0
echo "I don't know what your choice is" && exit 0
[948cshell@localhost bin]$ sh ans_yn.sh
Please input (Y/N) :y
OK, continue
[948cshell@localhost bin]$ sh ans_yn.sh
Please input (Y/N) :Y
OK, continue
[948cshell@localhost bin]$ sh ans_yn.sh
Please input (Y/N) :n
Oh, interrupt
[948cshell@localhost bin]$ sh ans_yn.sh
Please input (Y/N) :N
Oh, interrupt
[948cshell@localhost bin]$ sh ans_yn.sh
Please input (Y/N) :d
I don't know what your choice is
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N) :" yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK, continue"
exit 0
fi
if [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh, interrupt"
exit 0
fi
echo "I don't know what your choice is" && exit 0
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N) :" yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK, continue"
exit 0
elif [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh, interrupt"
exit 0
else
echo "I don't know what your choice is" && exit 0
fi
1、关于脚本中的默认变量的使用范例。
#!/bin/bash
# Program:
# Program shows the script name, parameters...
# History:
# 2020/08/11 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "程序的文件名是:${0}"
echo "程序中共有几个参数:${#}"
[ "${#}" -lt "2" ] && echo "The number of parameter is less than 2. Stop here. " && exit 0
echo "传入的参数有:${*}"
echo "第一个参数是:${1}"
echo "第二个参数是:${2}"
[948cshell@localhost ~]$ sh my_how_paras.sh a b c
程序的文件名是:my_how_paras.sh
程序中共有几个参数:3
传入的参数有:a b c
第一个参数是:a
第二个参数是:b
[948cshell@localhost ~]$ sh my_how_paras.sh
程序的文件名是:my_how_paras.sh
程序中共有几个参数:0
The number of parameter is less than 2. Stop here.
#!/bin/bash
# Program:
# Program shows the script name, parameters...
# History:
# 2020/08/11 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script name is ==> ${0}"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> ${1}"
echo "The 2nd parameter ==> ${2}"
[948cshell@localhost bin]$ sh how_paras.sh a b c
The script name is ==> how_paras.sh
Total parameter number is ==> 3
Your whole parameter is ==> 'a b c'
The 1st parameter ==> a
The 2nd parameter ==> b
[948cshell@localhost bin]$ sh how_paras.sh a
The script name is ==> how_paras.sh
Total parameter number is ==> 1
The number of parameter is less than 2. Stop here.
2、关于shift向右偏移变量号码的范例说明
#!/bin/bash
# Program:
# Program shows the effect of shift function.
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
# 进行一次参数的向左偏移
shift
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
# 进行三次参数的向左偏移
shift 3
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
[948cshell@localhost bin]$ sh shift_paras.sh one two three four five six
Total parameter number is ==> 6
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==> 2
Your whole parameter is ==> 'five six'
1、多重、复杂条件判断式的范例。
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
if [ "${1}" == "hello" ]; then
echo "Hello, how are you?"
elif [ -z "${1}" ]; then
echo "必须输入参数"
else
echo "请输入正确的参数hello"
fi
[948cshell@localhost bin]$ sh my_hello-2.sh
必须输入参数
[948cshell@localhost bin]$ sh my_hello-2.sh l
请输入正确的参数hello
[948cshell@localhost bin]$ sh my_hello-2.sh hello
Hello, how are you?
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
if [ "${1}" == "hello" ]; then
echo "Hello, how are you ?"
elif [ "${1}" == "" ]; then
echo "You MUST input parameters, ex> {
${0} someword }"
else
echo "The only parameters is 'hello', ex> {
${0} hello}"
fi
[948cshell@localhost bin]$ sh hello-2.sh
You MUST input parameters, ex> {
hello-2.sh someword }
[948cshell@localhost bin]$ sh hello-2.sh d
The only parameters is 'hello', ex> {
hello-2.sh hello}
[948cshell@localhost bin]$ sh hello-2.sh hello
Hello, how are you ?
#!/bin/bash
# Program:
# This program shows the user's choice
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
case ${1} in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> {
${0} someword}"
;;
*) # 其实相当于通配符,0~无穷多个任意字符之意。
echo "Usage ${0} {hello}"
;;
esac
[948cshell@localhost bin]$ sh case_hello-2.sh
You MUST input parameters, ex> {
case_hello-2.sh someword}
[948cshell@localhost bin]$ sh case_hello-2.sh sdf
Usage case_hello-2.sh {
hello}
[948cshell@localhost bin]$ sh case_hello-2.sh hello
Hello, how are you ?
2、多重、复杂条件判断式的较复杂的范例。
#!/bin/bash
# Program:
# You input your demobilization date, I calculate how many days before you demobilize.
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "请输入您的退伍日期(YYYYMMDD):" retired
# 目前的时间,用秒数来表示。
current_date="$(date +%s)"
# 退役的时间,用秒钟表示。
retired_date="$(date --date="${retired}" +%s)"
# 判断是否退役。
if [ "${current_date}" -ge "${retired_date}" ]; then
echo "您已经可以退役了。"
else
interval=$((${retired_date}-${current_date}))
echo "距离您退役还有$((${interval}/86400))天。"
fi
[948cshell@localhost bin]$ sh my_cal_retired.sh
请输入您的退伍日期(YYYYMMDD):20200101
您已经可以退役了。
[948cshell@localhost bin]$ sh my_cal_retired.sh
请输入您的退伍日期(YYYYMMDD):20201212
距离您退役还有122天。
#!/bin/bash
# Program:
# You input your demobilization date, I calculate how many days before you demobilize.
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1、告知使用者这个程序的用途,并且告知应该如何输入日期格式
echo "This program will try to calculate: "
echo "How many days before your demobilization date ..."
read -p "Please input your demobilization date (YYYYMMDD ex>20200812): " date2
#2、测试一下,这个输入的内容是否正确?利用正则表达式
date_d=$(echo ${
date2} | grep '[0-9]\{8\}')
if [ "${date_d}" == "" ]; then
echo "You input the wrong date format..."
exit 1
fi
#3、开始计算日期
# 退伍日期秒书
declare -i date_dem=$(date --date="${date2}" +%s)
# 现在日期秒数
declare -i date_now=$(date +%s)
# 剩余秒数统计
declare -i date_total_s=$((${date_dem}-${date_now}))
# 转为日数
declare -i date_d=$((${date_total_s}/60/60/24))
# 判断是否退伍
if [ "${date_d}" -lt "0" ]; then
echo "You had been demobilization before : $((-1*${date_d})) days ago."
else
declare -i date_h=$(($((${date_total_s}-${date_d}*60*60*24))/60/60))
echo "You will demobilize after ${date_d} days and ${date_h} hours."
fi
[948cshell@localhost bin]$ sh cal_retired.sh
This program will try to calculate:
How many days before your demobilization date ...
Please input your demobilization date (YYYYMMDD ex>20200812): 20201212
You will demobilize after 121 days and 12 hours.
[948cshell@localhost bin]$ sh cal_retired.sh
This program will try to calculate:
How many days before your demobilization date ...
Please input your demobilization date (YYYYMMDD ex>20200812): 2020-12-12
You input the wrong date format...
[948cshell@localhost bin]$ sh cal_retired.sh
This program will try to calculate:
How many days before your demobilization date ...
Please input your demobilization date (YYYYMMDD ex>20200812): 20200101
You had been demobilization before : 224 days ago.
以上代码中的正则表达式做输入数据的容错需要学习。
1、case…esac语句的范例。
#!/bin/bash
# Program:
# This script only accepts the flowing parameter: one, two or three.
# History:
# 2020/08/08 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will print your selection !"
# read -p "Input your choice: " choice $ 暂时取消,可以替换
# case ${choice} in # 暂时取消,可以替换
case ${1} in
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[948cshell@localhost bin]$ sh show123.sh
This program will print your selection !
Usage show123.sh {
one|two|three}
[948cshell@localhost bin]$ sh show123.sh one
This program will print your selection !
Your choice is ONE
[948cshell@localhost bin]$ sh show123.sh two
This program will print your selection !
Your choice is TWO
[948cshell@localhost bin]$ sh show123.sh three
This program will print your selection !
Your choice is THREE
[948cshell@localhost bin]$ sh show123.sh four
This program will print your selection !
Usage show123.sh {
one|two|three}
#!/bin/bash
# Program:
# This script only accepts the flowing parameter: one, two or three.
# History:
# 2020/08/08 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will print your selection !"
read -p "Input your choice: " choice
case ${choice} in
# case ${1} in # 暂时取消
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[948cshell@localhost bin]$ sh show123.sh
This program will print your selection !
Input your choice: one
Your choice is ONE
[948cshell@localhost bin]$ sh show123.sh
This program will print your selection !
Input your choice:
Usage show123.sh {
one|two|three}
[948cshell@localhost bin]$ sh show123.sh
This program will print your selection !
Input your choice: two
Your choice is TWO
[948cshell@localhost bin]$ sh show123.sh
This program will print your selection !
Input your choice: three
Your choice is THREE
1、关于脚本中函数的范例
#!/bin/bash
# Program:
# Use function to repeat information.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit() {
# echo命令加上-n选项可以不换行,让接下来的命令的输出在同一行进行显示
echo -n "Your choice is "
}
echo "This program will print your selection !"
case ${1} in
"one")
printit ; echo ${1} | tr [a-z] [A-Z]
;;
"two")
printit; echo ${1} | tr [a-z] [A-Z]
;;
"three")
printit; echo ${1} | tr [a-z] [A-Z]
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[948cshell@localhost bin]$ sh show123-2.sh
This program will print your selection !
Usage show123-2.sh {
one|two|three}
[948cshell@localhost bin]$ sh show123-2.sh ONE
This program will print your selection !
Usage show123-2.sh {
one|two|three}
[948cshell@localhost bin]$ sh show123-2.sh one
This program will print your selection !
Your choice is ONE
[948cshell@localhost bin]$ sh show123-2.sh two
This program will print your selection !
Your choice is TWO
2、关于函数中的$0、$1…变量的使用范例。
#!/bin/bash
# Program:
# Use function to repeat information.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit() {
echo "Your choice is ${1}"
}
echo "This program will print your selection !"
case ${1} in
"one")
printit 1
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[948cshell@localhost bin]$ sh show123-3.sh
This program will print your selection !
Usage show123-3.sh {
one|two|three}
[948cshell@localhost bin]$ sh show123-3.sh one
This program will print your selection !
Your choice is 1
[948cshell@localhost bin]$ sh show123-3.sh two
This program will print your selection !
Your choice is 2
[948cshell@localhost bin]$ sh show123-3.sh three
This program will print your selection !
Your choice is 3
上面代码中函数printit中的${1}和主函数中调用printit函数后面跟的第一个参数有关,${2}和主函数中调用printit函数后面跟的第二个参数有关,以此类推。函数中的${0}和函数名有关。
1、while不定循环与until不定循环的使用范例。
#!/bin/bash
# Program:
# Repeat question until user input corrent answer.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input yes/YES to stop this program: " flag
while [ "${flag}" != "yes" -a "${flag}" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " flag
done
[948cshell@localhost bin]$ sh my_yes_to_stop.sh
Please input yes/YES to stop this program: Yes
Please input yes/YES to stop this program: yes
[948cshell@localhost bin]$ sh my_yes_to_stop.sh
Please input yes/YES to stop this program: YEs
Please input yes/YES to stop this program: YES
#!/bin/bash
# Program:
# Repeat question until user input corrent answer.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
while [ "${yn}" != "yes" -a "${yn}" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[948cshell@localhost bin]$ sh yes_to_stop.sh
Please input yes/YES to stop this program: NO
Please input yes/YES to stop this program: no
Please input yes/YES to stop this program: Yes
Please input yes/YES to stop this program: YES
OK! you input the correct answer.
[948cshell@localhost bin]$ sh yes_to_stop.sh
Please input yes/YES to stop this program: yes
OK! you input the correct answer.
#!/bin/bash
# Program:
# Repeat question until user input corrent answer.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
[948cshell@localhost bin]$ sh my_yes_to_stop-2.sh
Please input yes/YES to stop this program: no
Please input yes/YES to stop this program: yes
[948cshell@localhost bin]$ sh my_yes_to_stop-2.sh
Please input yes/YES to stop this program: NO
Please input yes/YES to stop this program: YES
#!/bin/bash
# Program:
# Repeat question until user input corrent answer.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[948cshell@localhost bin]$ sh yes_to_stop-2.sh
Please input yes/YES to stop this program: NO
Please input yes/YES to stop this program: YES
OK! you input the correct answer.
[948cshell@localhost bin]$ sh yes_to_stop-2.sh
Please input yes/YES to stop this program: no
Please input yes/YES to stop this program: yes
OK! you input the correct answer.
1、for…do…done循环的使用范例。
#!/bin/bash
# Program:
# Using for ... loop to print 3 animals
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# for循环中的in后面跟的条件可以用空格进行分割,变量animal会依次取各个条件的值。
for animal in dog cat elephant
do
echo "There are ${animal}s..."
done
[948cshell@localhost bin]$ sh show_animal.sh
There are dogs...
There are cats...
There are elephants...
#!/bin/bash
# Program:
# Usr id, finger command to check system account's information.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
users=$(cut -d ':' -f1 /etc/passwd)
# for循环中的in后面跟的条件可以放到一个用换行分割的变量中,username变量会依次等于每一行的值
for username in ${users}
do
id ${username}
done
[948cshell@localhost bin]$ sh userid.sh
uid=0(root) gid=0(root) 组=0(root)
uid=1(bin) gid=1(bin) 组=1(bin)
uid=2(daemon) gid=2(daemon) 组=2(daemon)
uid=3(adm) gid=4(adm) 组=4(adm)
uid=4(lp) gid=7(lp) 组=7(lp)
uid=5(sync) gid=0(root) 组=0(root)
uid=6(shutdown) gid=0(root) 组=0(root)
uid=7(halt) gid=0(root) 组=0(root)
uid=8(mail) gid=12(mail) 组=12(mail)
uid=11(operator) gid=0(root) 组=0(root)
uid=12(games) gid=100(users) 组=100(users)
uid=14(ftp) gid=50(ftp) 组=50(ftp)
uid=99(nobody) gid=99(nobody) 组=99(nobody)
uid=192(systemd-network) gid=192(systemd-network) 组=192(systemd-network)
uid=81(dbus) gid=81(dbus) 组=81(dbus)
uid=999(polkitd) gid=998(polkitd) 组=998(polkitd)
uid=998(libstoragemgmt) gid=996(libstoragemgmt) 组=996(libstoragemgmt)
uid=997(colord) gid=995(colord) 组=995(colord)
uid=32(rpc) gid=32(rpc) 组=32(rpc)
uid=996(saslauth) gid=76(saslauth) 组=76(saslauth)
uid=173(abrt) gid=173(abrt) 组=173(abrt)
uid=172(rtkit) gid=172(rtkit) 组=172(rtkit)
uid=75(radvd) gid=75(radvd) 组=75(radvd)
uid=995(chrony) gid=992(chrony) 组=992(chrony)
uid=994(gluster) gid=991(gluster) 组=991(gluster)
uid=107(qemu) gid=107(qemu) 组=107(qemu),36(kvm)
uid=993(unbound) gid=990(unbound) 组=990(unbound)
uid=59(tss) gid=59(tss) 组=59(tss)
uid=113(usbmuxd) gid=113(usbmuxd) 组=113(usbmuxd)
uid=992(geoclue) gid=988(geoclue) 组=988(geoclue)
uid=991(setroubleshoot) gid=987(setroubleshoot) 组=987(setroubleshoot)
uid=171(pulse) gid=171(pulse) 组=171(pulse)
uid=990(saned) gid=984(saned) 组=984(saned)
uid=42(gdm) gid=42(gdm) 组=42(gdm)
uid=29(rpcuser) gid=29(rpcuser) 组=29(rpcuser)
uid=65534(nfsnobody) gid=65534(nfsnobody) 组=65534(nfsnobody)
uid=989(gnome-initial-setup) gid=983(gnome-initial-setup) 组=983(gnome-initial-setup)
uid=74(sshd) gid=74(sshd) 组=74(sshd)
uid=70(avahi) gid=70(avahi) 组=70(avahi)
uid=89(postfix) gid=89(postfix) 组=89(postfix),12(mail)
uid=38(ntp) gid=38(ntp) 组=38(ntp)
uid=72(tcpdump) gid=72(tcpdump) 组=72(tcpdump)
uid=1000(948cshell) gid=1000(948cshell) 组=1000(948cshell),10(wheel)
uid=1001(test1) gid=1001(test1) 组=1001(test1),1000(948cshell)
#!/bin/bash
# Program:
# User input dir name , I find the permission of files
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1、先看看这个目录是否存在
read -p "Please input a directory: " dir
if [ "${dir}" == "" -o ! -d "${dir}" ]; then
echo "The ${dir} is NOT exist in your system."
exit 1
fi
# 2、开始测试文件
filelist=$(ls ${
dir})
for filename in ${filelist}
do
perm=""
test -r "${dir}/${filename}" && perm="${perm} readable"
test -w "${dir}/${filename}" && perm="${perm} writable"
test -x "${dir}/${filename}" && perm="${perm} executable"
echo "The file ${dir}/${filename}'s permission is ${perm}"
done
[948cshell@localhost bin]$ sh dir_perm.sh
Please input a directory: /home/948cshell/bin
The file /home/948cshell/bin/ans_yn.sh's permission is readable writable
The file /home/948cshell/bin/cal_1_100.sh's permission is readable writable
The file /home/948cshell/bin/cal_pi.sh's permission is readable writable
The file /home/948cshell/bin/cal_retired.sh's permission is readable writable
The file /home/948cshell/bin/case_hello-2.sh's permission is readable writable
The file /home/948cshell/bin/create_3_filename.sh's permission is readable writable
The file /home/948cshell/bin/dir_perm.sh's permission is readable writable
The file /home/948cshell/bin/file_perm.sh's permission is readable writable
The file /home/948cshell/bin/hello-2.sh's permission is readable writable
The file /home/948cshell/bin/hello.sh's permission is readable writable executable
The file /home/948cshell/bin/how_paras.sh's permission is readable writable
The file /home/948cshell/bin/ifelif_ans_yn.sh's permission is readable writable
The file /home/948cshell/bin/ifthen_ans_yn.sh's permission is readable writable
The file /home/948cshell/bin/multiplying.sh's permission is readable writable
The file /home/948cshell/bin/my_ans_yn.sh's permission is readable writable
The file /home/948cshell/bin/my_cal_retired.sh's permission is readable writable
The file /home/948cshell/bin/my_create_3_filename.sh's permission is readable writable
The file /home/948cshell/bin/my_file_perm.sh's permission is readable writable
The file /home/948cshell/bin/my_hello-2.sh's permission is readable writable
The file /home/948cshell/bin/my_how_paras.sh's permission is readable writable
The file /home/948cshell/bin/my_multiplying.sh's permission is readable writable
The file /home/948cshell/bin/my_yes_to_stop-2.sh's permission is readable writable
The file /home/948cshell/bin/my_yes_to_stop.sh's permission is readable writable
The file /home/948cshell/bin/shift_paras.sh's permission is readable writable
The file /home/948cshell/bin/show123-2.sh's permission is readable writable
The file /home/948cshell/bin/show123-3.sh's permission is readable writable
The file /home/948cshell/bin/show123.sh's permission is readable writable
The file /home/948cshell/bin/show_animal.sh's permission is readable writable
The file /home/948cshell/bin/showname.sh's permission is readable writable executable
The file /home/948cshell/bin/userid.sh's permission is readable writable
The file /home/948cshell/bin/yes_to_stop-2.sh's permission is readable writable
The file /home/948cshell/bin/yes_to_stop.sh's permission is readable writable
1、for…do…done的数值处理的使用范例
#!/bin/bash
# Program:
# Try do calculate 1+2+...+${your_input}
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input a number, I will count for 1+2+...+your input: " nu
s=0;
for ((i=1; i<=${nu}; i++))
do
s=$((${s}+${i}))
done
echo "The result of '1+2...+${nu}' is ==> ${s}"
[948cshell@localhost bin]$ sh cal_1_100-2.sh
Please input a number, I will count for 1+2+...+your input: 9
The result of '1+2...+9' is ==> 45
[948cshell@localhost bin]$ sh cal_1_100-2.sh
Please input a number, I will count for 1+2+...+your input: 100
The result of '1+2...+100' is ==> 5050
1、随机数与数组在脚本中的使用范例。
#!/bin/bash
# Program:
# Try do tell you what you may eat.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 写下所有的店家
eat[1]="卖当当汉堡"
eat[2]="肯爷爷炸鸡"
eat[3]="彩虹日式便当"
eat[4]="越油越好吃大雅"
eat[5]="想不出吃啥学餐"
eat[6]="太师父便当"
eat[7]="池上便当"
eat[8]="怀念火车便当"
eat[9]="一起吃泡面"
# 保存有几个可用的便当
eatnum=9
check=$(( ${RANDOM} * ${eatnum} /32767 + 1 ))
echo "your may eat ${eat[${check}]}"
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 一起吃泡面
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 肯爷爷炸鸡
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 一起吃泡面
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 怀念火车便当
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 彩虹日式便当
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 池上便当
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 太师父便当
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 彩虹日式便当
[948cshell@localhost bin]$ sh what_to_eat.sh
your may eat 一起吃泡面
#!/bin/bash
# Program:
# Try do tell you what you may eat.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 写下所有的店家
eat[1]="卖当当汉堡"
eat[2]="肯爷爷炸鸡"
eat[3]="彩虹日式便当"
eat[4]="越油越好吃大雅"
eat[5]="想不出吃啥学餐"
eat[6]="太师父便当"
eat[7]="池上便当"
eat[8]="怀念火车便当"
eat[9]="一起吃泡面"
eatnum=9
eated=0
# 初始化,一开始所有的便当都没有被选。
for (( i=1; i<=${eatnum}; i++ ))
do
haspick[${i}]=0
done
while [ "${eated}" -lt "3" ]
do
check=$(( ${RANDOM} * ${eatnum} /32767 + 1 ))
# 检查是否便当已经被选了,如果被选了,mycheck赋值为1。
mycheck=0
for (( i=1; i<=${eatnum}; i++ ))
do
if [ "${haspick[${check}]}" == "1" ]; then
mycheck=1
fi
done
# 判断是否有已经被选择的便当,如果没有,就是一次成功的选择。
if [ "${mycheck}" != "1" ]; then
haspick[${check}]=1
eated=$(( ${eated} + 1 ))
echo "your may eat ${eat[${check}]}"
fi
done
[948cshell@localhost bin]$ sh what_to_eat-2.sh
your may eat 怀念火车便当
your may eat 卖当当汉堡
your may eat 太师父便当
[948cshell@localhost bin]$ sh what_to_eat-2.sh
your may eat 池上便当
your may eat 肯爷爷炸鸡
your may eat 越油越好吃大雅
[948cshell@localhost bin]$ sh what_to_eat-2.sh
your may eat 池上便当
your may eat 卖当当汉堡
your may eat 彩虹日式便当
1、第一题
#!/bin/bash
# Program:
# Print username who are using CentOS and current directory.
# History:
# 2020/08/13 948cshell First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "您目前的身份是:$(whoami)"
echo "您目前所在的目录是:$(pwd)"
[948cshell@localhost bin]$ sh print_whoami_pwd.sh
您目前的身份是:948cshell
您目前所在的目录是:/home/948cshell/bin
2、第二题
#!/bin/bash
# Program:
# Print the remained time to my next birthday.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
current_year="$(date +%Y)"
next_year="$(( ${current_year} + 1 ))"
current_time="$(date +%s)"
current_year_birthday="$(date --date="${current_year}0320" +%s)"
next_year_birthday="$(date --date="${next_year}0320" +%s)"
if [ "${current_time}" -gt "${current_year_birthday}" ]; then
remained=$(( ${next_year_birthday} - ${current_time} ))
else
remained=$(( ${current_year_birthday} - ${current_time} ))
fi
echo "距离生日还有$(( ${remained} /60/60/24 ))天"
[948cshell@localhost bin]$ sh how_long_can_I_have_my_next_birthday.sh
距离生日还有218天
3、第三题
#!/bin/bash
# Program:
# user input a number n, sh will calculate the result of 1+2...+n.
# History:
# 2020/08/10 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input a number n : " n
declare -i sum=0
for ((i=1; i<=${n}; i++ ))
do
sum=$(( ${sum} + ${i} ))
done
echo "The result of 1+2...+${n} is : ${sum}."
[948cshell@localhost bin]$ sh cal_1_to_n.sh
Please input a number n : 100
The result of 1+2...+100 is : 5050.
[948cshell@localhost bin]$ sh cal_1_to_n.sh
Please input a number n : 10
The result of 1+2...+10 is : 55.
4、第四题
#!/bin/bash
# Program:
# deal with directory /root/test/logical
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
tested_directory='/root/test'
tested_file='logical'
if [ -e "${tested_directory}/${tested_file}" ]; then
if [ -f "${tested_directory}/${tested_file}" ]; then
rm -i "${tested_directory}/${tested_file}"
mkdir -p "${tested_directory}/${tested_file}"
else
rm -ir "${tested_directory}/${tested_file}"
fi
else
if [ ! -e "${tested_directory}" ]; then
mkdir -p "${tested_directory}"
fi
cd "${tested_directory}"
touch "${tested_file}"
fi
5、第五题
#!/bin/bash
# Program:
# Print account in this system.
# History:
# 2020/08/12 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
row_num=1
for account in $(cut -d ':' -f1 /etc/passwd)
do
echo "This ${row_num} account is \"${account}\""
row_num=$(( ${row_num} + 1 ))
done
[948cshell@localhost bin]$ sh print_account.sh
This 1 account is "root"
This 2 account is "bin"
This 3 account is "daemon"
This 4 account is "adm"
This 5 account is "lp"
This 6 account is "sync"
This 7 account is "shutdown"
This 8 account is "halt"
This 9 account is "mail"
This 10 account is "operator"
This 11 account is "games"
This 12 account is "ftp"
This 13 account is "nobody"
This 14 account is "systemd-network"
This 15 account is "dbus"
This 16 account is "polkitd"
This 17 account is "libstoragemgmt"
This 18 account is "colord"
This 19 account is "rpc"
This 20 account is "saslauth"
This 21 account is "abrt"
This 22 account is "rtkit"
This 23 account is "radvd"
This 24 account is "chrony"
This 25 account is "gluster"
This 26 account is "qemu"
This 27 account is "unbound"
This 28 account is "tss"
This 29 account is "usbmuxd"
This 30 account is "geoclue"
This 31 account is "setroubleshoot"
This 32 account is "pulse"
This 33 account is "saned"
This 34 account is "gdm"
This 35 account is "rpcuser"
This 36 account is "nfsnobody"
This 37 account is "gnome-initial-setup"
This 38 account is "sshd"
This 39 account is "avahi"
This 40 account is "postfix"
This 41 account is "ntp"
This 42 account is "tcpdump"
This 43 account is "948cshell"
This 44 account is "test1"
1、关于/etc/login.defs文件的内容。
MAIL_DIR /var/spool/mail
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
UID_MIN 1000
UID_MAX 60000
SYS_UID_MIN 201
SYS_UID_MAX 999
GID_MIN 1000
GID_MAX 60000
SYS_GID_MIN 201
SYS_GID_MAX 999
CREATE_HOME yes
UMASK 077
USERGROUPS_ENAB yes
ENCRYPT_METHOD SHA512
1、关于当用户的sudo权限重复设置时的问题以及对用户能够使用sudo执行的命令限制的设置问题的说明。
1、什么是ssh?
1、批量增加用户的脚本模板
#!/bin/bash
# Program:
# This shell script will creates amount of Linux login accounts for you.
# 1、check the "accountadd.txt" file exist? You must create that file manually.
# one account name one line in the "accountadd.txt" file.
# 2、user openssl to create users password.
# 3、user must change his password in his first login.
# History:
# 2020/08/16 948cshell First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH
# 0、userinput
# if your account need secondary group, add here.
usergroup=""
# "openssl" or "account" is needed.
pwmech="openssl"
# if "yes" then I will modify home dir permission to 711
homeperm="no"
# 1、check the accountadd.txt file
# "create" is useradd and "delete" is userdel.
action="${1}"
if [ ! -f "accountadd.txt" ]; then
echo "There is no accountadd.txt file, stop here."
exit 1
fi
# 如果有额外的用户组需要加入,创建一个GID小于500的系统工作组
[ "${usergroup}" != "" ] && groupadd -r ${usergroup}
usernames=$(cat accountadd.txt)
for username in ${usernames}
do
case ${action} in
"create")
[ "${usergroup}" ] && usegrp=" -G ${usergroup}" || usegrp=""
# 新建帐号
useradd ${usegrp} ${username}
# 随机生成一个6位的密码或者将用户名当作密码
[ "${pwmech}" == "openssl" ] && usepw=$(openssl rand -base64 6) || usepw=${username}
# 给每个用户设置密码
echo "${usepw}" | passwd --stdin ${username}
# 设置用户一登陆就要修改密码
chage -d 0 ${username}
[ "${homeperm}" == "yes" ] && chmod 711 /home/${username}
echo "username=${username}, password=${usepw}" >> outputpw.txt
;;
"delete")
echo "deleting ${username}"
userdel -r ${username}
;;
esac
done
1、第一题
2、第二题
3、第三题
4、第四题
5、第五题
6、第六题
7、第七题
8、第八题
9、第九题
1、关于磁盘阵列等级RAID 5的说明。
1、创建一个RAID 5级别的软件磁盘阵列并格式化与挂载使用案例。
案例的RAID 5级别的软件磁盘阵列的环境为:利用4个分区组成RAID 5。每个分区大小是1G,需确定每个分区一样大较佳。将1个分区设置为热备份磁盘。chunk设置为256KB这么大即可。这个热备份磁盘的大小与其他RAID所需分区一样大。将RAID 5设备挂载到目录【/srv/raid】下。
还可以通过查看内存中的文件【/proc/mdstat】文件来显示正在运行的软件磁盘阵列信息。如下图。
上图中的第二行分区后面有【(S)】的表示这是用来备份的。第三行的【[4/4]】表示此磁盘阵列需要4个设备,其中有4个设备正常运行,后面的【[UUUU]】中的U表示正常运行的意思,如果是不正常运行则会是【_】。
第三步,格式化软件磁盘阵列并使用。如下图。
关于上图中的命令【mkfs.xfs】的使用参考【导学a240】。到此完成了软件磁盘阵列的挂载使用。
上图中的热备份已经变为了活跃状态。
第二步:将出错的磁盘删除并加入新磁盘。
先从软件磁盘阵列中删除坏掉的磁盘或分区。如下图。
由于操作目标是分区,所以不需要关机更换磁盘。如果是磁盘坏掉了,在移除出磁盘阵列之后,需要更换磁盘。
将新的磁盘加入到磁盘阵列中,设备名一般还是和之前移出的设备名相同。如下图所示。
将新的完好的分区或磁盘作为备份,如上图最后一行所示。
1、如何关闭RAID?(承接14.2.3的磁盘阵列)
第一步:先卸载,并且将文件【/etc/fstab】中的对应的信息删除。如下图所示。
第二步:覆盖掉RAID的metadata以及XFS的超级块,之后在关闭【/dev/md0】,这里覆盖使用命令【dd】。如下图。
通过查看文件【/proc/mdstat】可以发现,已经将磁盘阵列删除了。如下图。
第四部:将文件【/etc/mdadm.conf】中的对应信息删除即可。
1、如何创建一个LV并挂载使用?
通过LVM创建LV并格式化挂载使用:例子,建立一个名为vbirdvg的卷组,在这个卷组中划出一个名为vbirdlv的分区,并将其挂载到目录【/srv/lvm】。
第一步:disk阶段,即实际的磁盘。
使用命令gdisk随便划分5个分区,将他们的分区编码设置为8e00,即Linux LVM。如下图,分区14到17所示。
第二步:PV阶段,将分区变成物理卷。
使用到的命令有如下:
pvcreate:将物理分区建立为物理卷(PV)。
pvscan:查找目前系统里面任何具有物理卷(PV)的磁盘。
pvdisplay:显示出目前系统上面的PV状态。
pvremove:将PV属性移除,让该分区不具有PV属性。
具体操作如下。
先扫描看系统中有没有PV。如下图。
从上图中可以发现有一个PV存在,这是系统安装时候创建的。
将第一步划分的物理分区(分区14到17)建立成为PV。如下图。
上图中利用大括号,一口气创建了4个PV。再进行一次PV的扫描,如下图。
上图中检索到了刚刚创建的4个分区。
可以查看更加详细的PV的状态信息。下面以分区【/dev/sdb14】举例。
Allocatable字段表示是否被分配,NO表示没有被分配,PE Size表示一个PE的大小,Total PE表示PV共分了多少个PE出来,Free PE表示未被LV用掉的PE数量,Allocated PE表示尚可分配出去的PE数量。这些有关PE的字段的信息会在将该PV分配给VG时进行更新。
第三步:VG阶段,创建VG大磁盘
使用到的命令如下:
vgcreate:主要建立VG的命令。
vgscan:查找系统上面是否有VG存在。
vgdisplay:显示目前系统上面的VG状态。
vgextend:在VG内增加额外的PV。
vgreduce:在VG内删除PV。
vgchange:设置VG是否启动。
vgremove:删除一个VG。
具体操作如下。
创建一个VG。如下图。
上图中命令【vgcreate】的选项【-s】后面跟一个数字用来指定PE的大小(单位是m,g,t),倒数第二个参数是自定义的VG名称(vbirdvg),最后一个参数是用到的PV名称。
查看系统中的有多少VG。
从上图中查看到了刚刚创建的vbirdvg。之后在扫描以下系统中所有的PV,如下图。
可以发现上图中的PV14到PV16都属于名称为vbirdvg的卷组(VG),这是在创建卷组vbirdvg时指定的。
查看vbirdvg卷组的详细信息。
从上图中可以看出,字段PE Size的大小时16M,这是创建时指定的,Total PE表示总的PE数目,Free PE表示商可配置给LV的PE数量,由于目前还没有建立LV,所以PE可自由使用。PE是在建立卷组后自动创建的。
可以查看以下分配给卷组的物理卷的详细信息,如下图。
刚创建PV的时候字段Total PE等都是0,由于没有分配给卷组,当分配给卷组后就会有PE的数目了。
可以对一个创建好的卷组进行容量的扩充。如下图。
上图中的命令vgextend后面一个参数是卷组,最后面一个参数是被加入的PV。
第三步:LV阶段,对大磁盘VG进行分区(LV)
使用的命令如下:
lvcreate:建立LV。
lvscan:查询系统上面的LV。
lvdisplay:显示系统上面的LV状态。
lvextend:在LV里面增加容量。
lvreduce:在LV里面减少容量。
lvremove:删除一个LV。
lvresize:对LV进行容量大小的调整。
具体操作如下。
在一个已经创建好的卷组中创键一个LV分区。如下图。
上图中命令lvcreate的选项-L后面跟数字用来指定划分的LV分区的大小,该倒数第二个选项是要创建的LV分区的名称,最后一个参数是被划分的已经创建的卷组。
查看系统中所有的LV分区。如下图。
可以看到刚刚创建的LV分区vbirdlv处于活跃状态。
查看LV分区vbirdlv的详细信息。如下图。
上图中可以看出这个LV分区的大小时2G。
第四步:文件系统阶段,将LV分区格式化并挂载。
进行xfs文件系统的格式化。如下图。
将其进行挂载。如下图。
测试是否可以使用。如下图。
从上图可以看出,的确可以使用。
1、放大LV容量的范例
放大文件系统容量:比如给上个案例的目录【/srv/lvm】增加500MB的容量。
第一步:查看卷组【vbirdvg】有多少容量。如下图。
从上图可以看出卷组的容量足够500M,可以直接放大LV分区的大小。
第二步:放大LV,利用命令lvresize来增加。如下图。
从上图可以看出原来vbirdlv分区是2G,放大后变成了2.5G了。虽然LV分区放大到了2.5G,可是可以发现文件系统并没有增加容量。如下图,依然还是2G大小。
第三步:增加文件系统的容量。
先查看以下文件系统的信息。如下图。
增加文件系统的大小,使用命令【xfs_growfs】命令。如下图。
发现容量变成了2.5G了。
1、磁盘容量存储池的范例
建立LV快照区给上面例子中的逻辑卷vbirdlv作备份。
第一步:查看卷组vbirdvg还剩下多少容量。如下图。
从上图中可以看出只剩下25PE了。
第二步:利用lvcreate建立逻辑卷vbirdlv的快照区,命名为vbirdsnap1,且给予25个PE。如下图。
上图中的lvcreate命令的-s选项表示创建的是一个快照文件的意思,-l选项后面跟一个数字,表示使用多少个PE的意思,-n选项用来指定创建的快照的名称,最后一个参数是创建快照的目标文件。查看一下快照的详细信息,如下图。
上图中的LV Size表示原始文件的大小,在这里的例子中指的是vbirdlv的大小,COW-table size是快照可以记录的最大容量,Allocated to snapshot是目前已经被用掉的容量。将快照文件进行挂载,查看文件系统的信息。如下图。
从上图中的可以看出被快照的文件与快照文件是一样的,这里有个细节就是在挂载快照文件的时候由于快照文件与被快照的文件拥有相同的uuid所以在使用挂载命令mount的时侯要忽略相同uuid的影响。
1、创建LV快照并进行数据恢复范例
使用快照功能恢复系统的例子,拿上面案例的逻辑卷vbirdlv和它的快照vbirdsnap1举例。
第一步:将逻辑卷vbirdlv做一点修改。注意修改的容量不要超过快照文件的大小。
经过修改之后可以发现这两个文件已经是不一样的了。检测一下快照文件,如下图所示。
从上图中可以发现快照已经使用了37%左右的容量。
第二步:利用快照将原来的文件系统进行备份,使用命令xfsdump。如下图。
从上图中可以看出成功根据快照创建了vbirdlv的备份文件【/home/lvm.dump】文件。
第三步:删除快照vbirdsnap1,因为备份文件已经建立了,之后恢复逻辑卷vbirdlv对应的文件。
删除快照。如下图。
之后将逻辑卷vbirdlv重新格式化。如下图。
将vbirdlv设备重新挂载,并进行数据的恢复。如下图。
可以看到,已经恢复成功了。
补充一点:在进行数据的测试时,可以将快照区作为测试用的文件,原系统作为备份数据,这样子就算搞砸了,直接将快照删了,在根据原系统建一个新的快照就行,非常的方便。
1、删除LVM的范例
删除系统中的某LVM:以删除上面建立的LV、VG和PV为例。
删除LVM的流程如下:
先卸载系统上面的LVM文件系统(包括快照和所有的LV)。
使用lvremove删除LV。
使用vgchange -a n VGname让该卷组不具有Active标志。
使用vgremove删除VG。
使用pvremove删除PV。
最后使用gdisk或者fdisk将分区的System ID改回来。
具体操作如下图。
删除存储池,存储上的设备,逻辑卷的顺序是:存储池、存储上的设备、逻辑卷(快照之前已经删了,如果还有快照也要删除)。
接下来将分区的System ID改回来就行了。如下图。
1、第一题
2、第二题
3、第三题:
4、第四题:
1、第一题
2、第二题
3、第三题
4、第四题
5、第五题
6、第六题
1、第一题
2、第二题
3、第三题
4、第四题
5、第五题
6、第六题