echo:显示
题1:显示主机名
vim show_hostname.sh
执行:
方式一:bash方式
方式二: source方式
方式三:chmod修改权限
题2:编写一个shell脚本, 用于查找系统中大于5m小于10m的所有文件,并将文件路径信息保存到~/file_result文件中,并统计文件总共的数量
vim file_filter.sh
管道符化简代码:
执行:
循环
for var in list
do
命令
done
其中,list可以是{1..6}表示1到6,可以是通配符的表示方式,也可以是多个参数用空格分隔方式
var为变量,用于指代list部分中的每一个值内容
题3:查找/home下的所有文件,并对查到的文件查看文件类型
vim file_for.sh
执行
退出码:
exit 0退出0 exit 1 退出1
判断:
if 条件
then 命令
fi
if 条件
then
命令
else
命令
fi
if 条件
then
命令
elif 条件
then
命令
else
命令
fi
通常的一些条件写法
[[ ${?} == 0 ]] #如果上一个命令执行之后的返回码为0[[ ${?} != 0 ]] #如果上一个命令执行之后的返回码不为0[[ ${var} -gt 0 ]] #条件为var变量的数值大于0 [[ ${var} -lt 0 ]] #条件为var变量的数值小于0 #注意[ 与$有空格 , 符号 {} 与运算符直接有空格
题4:编写一个脚本, 自动启动httpd服务
exit code
执行
代码:
题1:
#!/bin/bash
#program:print hello ,hostname
#history:2022-5-21 first-version
#from rhcsa
echo Hello $(whoami)
题2:
#/bin/bash
#program:1.find all the files whose memoery size between 5m and 10m,then 2.storage the file path info to ~/file_result,and then3. count the total file numbe#r.
#history:first relese 2022-05-22 from rhcsa
find / -size +5M -size -10M > ~/file_result
wc -l ~/file_result
#/bin/bash
find / -size +5M -size -10M | tee ~/file_result1 | wc -l
题3:
#/bin/bash
#program:find all files under /home ,and then do file for each file
#history:2022-05-22 first-version from rhcsa
for filepath in $(find /home -name '.*' -user redhat -type f)
do
file ${filepath}
done
题4:
#!/bin/bash
#program:use exit and if statement to start httped service auto
#history:2022-05-22 first-version from rhcsa
systemctl is-active httpd.service 2> /dev/null
result=${?}
if [[ ${result} == 0 ]]
then
echo httped is running
elif [[ ${result} == 3 ]]
then
echo httped is not running
systemctl start httpd.service 2> /dev/null
result1=${?} #use result1 to storage exit code before
if [[ ${result1} == 5 ]]
then
echo httped start failed,you need to install httpd
elif [[ ${result1} == 0 ]]
then
echo httpd start success
else
echo other error
fi
fi