17、创建一个脚本
在server0上创建一个名为/root/foo.sh的脚本,让其提供下列特性:
当运行/root/foo.sh redhat,输出fedora
当运行/root/foo.sh fedora,输出redhat
当没有任何参数或者参数不是redhat或者fedora时,其错误输出产生以下的信息:/root/foo.sh redhat|fedora
[root@server0 ~]# vim /root/foo.sh
#!/bin/bash
case $1 in
redhat)
echo "fedora" ;;
fedora)
echo "redhat" ;;
*)
echo "/root/foo.sh redhat|fedora" ;;
esac
[root@server0 ~]# chmod 775 foo.sh
[root@server0 ~]# chmod +x foo.sh
测试
[root@server0 ~]# ./foo.sh redhat
[root@server0 ~]# ./foo.sh fedora
[root@server0 ~]# ./foo.sh ddd
整数变量表达式
if [ int1 -eq int2 ] Equal 等于
if [ int1 -ne int2 ] not equal 不等于
if [ int1 -ge int2 ] greater equal >=
if [ int1 -gt int2 ] greater than >
if [ int1 -le int2 ] less equal<=
if [ int1 -lt int2 ] less than <
变量说明:
$$ Shell本身的PID(ProcessID)
$! Shell最后运行的后台Process的PID
$? 最后运行的命令的结束代码(返回值)
$- 使用Set命令设定的Flag一览
$* 所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@ 所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$# 添加到Shell的参数个数
$0 Shell本身的文件名
$1~$n 添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
文件表达式
if [ -f file ] 如果文件存在
if [ -d ... ] 如果目录存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可读
if [ -w file ] 如果文件存在且可写
if [ -x file ] 如果文件存在且可执行
[root@server0 ~]# cat /etc/shells 查看shell 解释器
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@server0 ~]# echo $PATH 查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server0 ~]# which bash
/usr/bin/bash
[root@server0 ~]# ll -i /usr/bin/bash
8399757 -rwxr-xr-x. 1 root root 960368 Jan 29 2014 /usr/bin/bash
[root@server0 ~]# ll -i /bin/bash
8399757 -rwxr-xr-x. 1 root root 960368 Jan 29 2014 /bin/bash
innode :索引编号 相同,是同一个文件
软连接
硬链接
[root@server0 ~]# bash foo.sh
标准输入,标准输入,错误输出,错误输入 ???
[root@server0 ~]# echo 22 > a
[root@server0 ~]# cat a
22
[root@server0 ~]# cat a d > /dev/null
cat: d: No such file or directory
[root@server0 ~]# cat a d &> /dev/null
[root@server0 ~]# cat a d 2&> e
[root@server0 ~]# cat a d 2> e
18、创建一个添加用户的脚本
在server0上创建一个名为/root/batchusers,此脚本能够实现为系统system1创建本地用户,并且这些用户的用户名来自一个包含用户名列表的文件,同时满足下列要求:
此脚本要求提供一个参数,此参数就是包含用户名列表的文件
如果没有提供参数,此脚本应该给出下面的提示信息Usage: /root/batusers userfile ,并且退出返回相应的值
如果提供一个不存在的文件名,此脚本应该给出下面的提示信息 Input file not found然后退出并返回相应的值
创建的用户登录shell为/bin/false
此脚本不需要为用户设置密码 (注意:有得时候需要设置统一密码为redhat)
您可以从下面的URL获取用户列表作为测试用
http://classroom.example.com/materials/userlist
[root@server0 ~]# vim /root/batchusers #不是sh脚本没有颜色提示
#!/bin/bash
if [ $# -eq 0 ];then #$# 参数个数 等于 0
echo "Usage: /root/batusers userfile"
exit 1
fi
if [ ! -f $1 ];then #-f 文件存在 $1第一参数
echo "Input file not found"
exit 2
fi
while read username #读文件一行
do
useradd -s /bin/false $username &> /dev/null
done < $1 #当没有读到东西时结束
[root@server0 ~]# chmod +x batchusers
[root@server0 ~]# wget http://classroom.example.com/materials/userlist
[root@server0 ~]# cat userlist
[root@server0 ~]# id roy
测试
[root@server0 ~]# ./batchusers
Usage: /root/batchusers userfile
[root@server0 ~]# echo $? 最后运行的命令的结束代码(返回值)
1
[root@server0 ~]# ./batchusers eee
Input file not found
[root@server0 ~]# echo $?
2
[root@server0 ~]# ./batchusers userlist
[root@server0 ~]# echo $?
0
[root@server0 ~]# id roy
[root@server0 ~]# ./batchusers userlist
[root@server0 ~]# echo $?
9 #返回9 说明已经执行过了
21、部署MariaDB数据库
在server0上部署MariaDB。要求如下:
仅允许从server0系统上使用登陆到数据库。
登陆数据库所用的账号为root,密码为root_password。
从http://content.example.com/courses/rhce/rhel7.0/materials/mariadb/mariadb.dump上下载文件,并将其恢复为legacy库。并设置数据库访问:
用户名 密码 权限
Mary xxx 对legacy库的所有数据有选择操作权限
Legacy xxx 对legacy库的所有数据有选择、插入、更新、删除操作权限
Report xxx 对legacy库的所有数据有选择操作权限
[root@server0 ~]# yum groupinstall mariadb mariadb-server.x86_64 -y
[root@server0 ~]# systemctl restart mariadb
[root@server0 ~]# systemctl enable mariadb.service
[root@server0 ~]# mysql_secure_installation 安装数据库
Set root password? [Y/n] Y ### 设置 root 密码为 redhat
New password: root
Disallow root login remotely? [Y/n] Y ### 禁止远程访问
[root@server0 ~]# wget http://content.example.com/courses/rhce/rhel7.0/materials/mariadb/mariadb.dump
[root@server0 ~]# mysql -uroot -proot
[root@server0 ~]# mysql -u root -p
Enter password:
MariaDB [(none)]> create database legacy;
MariaDB [(none)]> show databases;
MariaDB [(none)]> use legacy;
MariaDB [legacy]> show tables;
MariaDB [legacy]> source /root/mariadb.dump 逻辑备份还原,导入式
MariaDB [legacy]> show tables;
MariaDB [legacy]> grant select on legacy.* to mary@localhost identified by 'marry_password';
选择权限 对legacy下的所有表 mary本地用户 密码认证为root
MariaDB [legacy]> grant select on legacy.* to report@localhost identified by 'report_password';
MariaDB [legacy]> grant select,insert,update,delete on legacy.* to legacy@localhost identified by 'legacy_password';
MariaDB [legacy]> exit
MariaDB [mysql]> create database legacy;
MariaDB [legacy]> create table stu (name varchar(20),age int(10),salary int(10),job varchar(10) );
MariaDB [legacy]> insert into stu values('alex',33,10000,'teacher'); 增
MariaDB [legacy]> select name from stu where age=33; 查
MariaDB [legacy]> update stu set name='snow' where job="it"; 改
MariaDB [legacy]> delete from stu where age=33; 删
MariaDB [legacy]> drop table stu;
22、数据查询填空
在server0上登陆数据库,查看XXX库进行查询,并将结果填入相应的框格中。
Q1在product表中,查询RT-AC68U的产品id()
Q2查询类别为Servers的产品的数量()
模拟考环境请在http://classroom.example.com/cgi-bin/mariadb提交
[root@server0 ~]# mysql -u root -p
MariaDB [(none)]> show databases;
MariaDB [(none)]> use legacy
MariaDB [legacy]> show tables;
MariaDB [legacy]> select * from product;
MariaDB [legacy]> select * from category;
MariaDB [legacy]> select * from manufacturer;
MariaDB [legacy]> desc product;
MariaDB [legacy]> select id from product where name='RT-AC68U';
+----+
| id |
+----+
| 3 |
+----+
MariaDB [legacy]> select * from category,product where category.name='Servers' and category.id=product.id_category;
+----+---------+----+-------------------+---------+-------+-------------+-----------------+
| id | name | id | name | price | stock | id_category | id_manufacturer |
+----+---------+----+-------------------+---------+-------+-------------+-----------------+
| 2 | Servers | 1 | ThinkServer TS140 | 539.88 | 20 | 2 | 4 |
| 2 | Servers | 2 | ThinkServer RD630 | 2379.14 | 20 | 2 | 4 |
+----+---------+----+-------------------+---------+-------+-------------+-----------------+
MariaDB [legacy]> select product.id from category,product where category.name='Servers' and category.id=product.id_category;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
MariaDB [legacy]> select count(product.id) from category,product where category.name='Servers' and category.id=product.id_category;
MariaDB [legacy]> exit
[root@server0 ~]# firefox http://classroom.example.com/cgi-bin/mariadb