【Linux实践】权限管理 bash Shell 脚本编程实战

发布于 2020-11-16  359 次阅读


(一)创建文件 File1、File2、File3,依次设置 710、741、751 的数字权限;取消File1 的同组账户可执行权限、取消 File2 的同组账户可读权限、取消 File3 的其他账户可执行权限

(1)设置数字权限

查看改变权限前的文件权限情况:

file

赋予如下格式命名:chmod 710 File1,不同文件按照对应要求即可:
file

可以发现,710数字权限增加了File1的属主和同组的可执行权限;741数字权限增加了File2的属主与其他用户的可执行权限;751增加了File3属主、同组以及其他用户的可执行权限。

(2)设置账户组相关权限

采用命令chmod g-x File1取消File1 的同组账户可执行权限:

file

采用命令chmod g-r File2取消 File2 的同组账户可读权限:

file

采用命令chmod o-x File3取消 File3 的其他账户可执行权限:

file

效果与(1)的整体操作相反。

(二) 分别手工启动前台、后台进程;利用 at 命令调度启动一个作业

(1)前后台执行

ls为例说明,前台执行ls命令,即直接在终端输入并回车,此时在该进程进行时无法进行其他操作;执行后台进程,则在命令后添加&标识符,此时ls在执行的同时,还能同时执行其他命令如w

5faec03c93ba3.png

(2)at定时指令

创建可执行脚本如下:

#!/bin/bash
curl https://dgzc.ganahe.top >> ~/linuxLearn/chowd/atDemand.txt

采用curl指令获取网页内容并重定向输入到文件中。
通过指令at 01:32 today -f atAutobash.sh实现定时执行上述脚本:

5faec50ece875.png

通过指令atq或是at -l查看作业列表:

5faec5af97176.png

在指定时间后,输出的文件列表对比如下:

执行前 执行后
5faec2b8a29fd.png 5faec6a3037e6.png

指向的文件atDemand.txt已生成且内容如下:

5faec5de365ee.png

(三)编程提示输入两个单词,然后比较这两个单词。如果两个单词相同则显示“Match”,否则显示“Not match”,最后显示“End!”(if 控制结构)

主要思路:执行脚本后,由用户于输入两个单词,随后采用字符串的比较运算符实现比较。

为了输出信息更为全面与清晰,更能把握到条件判断与选择的细节,故在原题指定要求的输出上,做了一定的调整,编辑脚本如下:

#!/bin/bash

echo " welcome to use my shell's Script @by GanAHE"

echo "--------- the procedure running ------\n "

read -t 20 -p "please input two strings which splited by black:"  str1 str2
echo  - Do Words: {str1} ={str2} ?
if [ str1 =str2 ];then
        echo '- Match '
elif [ str1 \>str2 ];then
        echo "- Not match, the bigger one is:{str1}"

else echo "- Not match, the bigger one is:{str2}"
fi
echo ------ end! ------

采用read-t参数,设置输入时限,避免进程拥塞,同时-p参数可以在输入前给出提示,提升交互效果。
采用if进行条件选择判断,结果如下:

5fb28f4d909d1.png

file

(四)编程创建一个简单的菜单,屏幕显示如下:

a. Date:
b. User:
c. The working directory
Enter a,b or c:

根据用户输入选项做相应操作(case 控制结构)

(1)分析与设计

根据菜单设计的要求,当用户键入a时,输出系统数据情况如内存使用、进程占用等数据;键入b时,输出当前用户以及所有用户;键入c时,给出当前的工作路径。

分析如上需求,只需判断用户输入的内容去选择处理即可,同时增设不同的参数输入时的提示与说明等功能:

#!/bin/bash

echo "\n welcome to use this Script @copyright by GanAHE"
ind="no"
while [ ind != "quit" ]
do
  echo "\n---------- the system of monitor under Linux -----\n make a choise to get somthing:\na.Date:\nb.User:\nc.The working directory\nEnter a , b or c: "
  read op
  ind=op
  case "${op}" in a)
        free -m
        echo "-------------------------------"
        df -h
  ;;  

  b)  
        echo -n " - You are: "
        whoami
        echo  " - All user:"
        w | head
 ;;

 c)
       echo -n " - now directory is: "
       pwd
 ;;

 *)
      echo " * Input Error! Please input correct option! \n * You can input 'quit' to exit process"
 esac
done
echo "\n----------- End! -----------"

(2)效果

首先是执行普通选项(a、b、c):

file

file

file

当输入错误选择字符或是退出程序选项时:

file

动态展示效果如下:

(五) 输入分数 score,如果大于等于 60 分并且小于 80 分,输出“成绩良好”;如果大于等于 80 分,输出“成绩优秀”;如果小于 60 分,输出“成绩不及格”

主要涉及整数的条件判断与选择问题,设计的程序如下:

#!/bin/bash

echo "Welcome to use my Script @copyright @GanAHE"
echo
read -p  " - Please input your score:" sc
if [ {sc} -ge 80 ];then
  echo"成绩优秀"
elif [{sc}  -lt 80 ]&&[ ${sc} -ge 60 ];then
  echo " 成绩良好"
else echo " 成绩不及格"
fi
echo
echo "----------- end! -----------"

输出结果:

file

(六) 编程实现加法、乘法运算、奇偶数判断

(1)设计

题目要求实现基本的数据运算,需要用到exrp、let等脚本内部数据运算命令,随后还有数据类型判断等,将全部要求集成:

#!/bin/bash
echo "Thanks to use my Script @copyright by GanAHE"
echo
echo "choice the function:  a.calculator(or any char);  b. Judge parity"
read -p " - Please input your choice: "  cs  

#Judge parity
if [ cs = b ];then
  read -p"- Plese input an integer:"integer
  let op=integer%2
  if [op -eq 0 ];then
      echo "the value: integer is even number!"
  else echo "the value:integer is odd number!"
  fi  

# calculator
else
  read -p " - Please input the first number: " num1
  read -p " - Please input the second number: " num2

  read -p " - Please input the second operator(\"+\", \"-\", \"*\", \"/\"): " op
  #if的另一种格式,不仅仅局限于if [a ^ b ];then
  if [ -n "num1" -a -n "num2" -a -n "op" ]
  then
    # 1.check the value
    test1=(echo num1 | sed 's/[0-9]//g')
    test2=(echo num2 | sed 's/[0-9]//g')
    if [ -n "test1" -o -n "test2" ]
    then
        echo "Please input number."
        exit 1
    fi  

    # 2.check the sign of calculation
    caseop in
        "+")
            result=((num1 + num2))
            ;;
        "-")
            result=((num1 -num2))
            ;;
        "*")
            result=((num1 * num2))
            ;;
        "/")
            result=((num1 /num2))
            ;;
        *)

            echo "Please input correct operator, like \"+\", \"-\", \"*\", \"/\"."
            exit 2
            ;;
    esac

    # 3. print the result
    echo "num1op num2 =result"
    exit 0
  else
    echo "Number and oprator must not be empty"
    exit 3
  fi
fi

(2)效果

基本数据运算如下:

file

奇偶性判断效果:

file

(七)编程实现 n 的阶乘,n=10

(1) 设计

计算阶乘是一类比较的实用功能,可以设计为:把整个文件当作一个函数来用,直接在命令行输入,就像内置的各种命令一样:

#!/bin/bash

echo " -------- Thanks to use my Script @copyright by GanAHE ----------  "

num=1
#echo sfddg1 2

exprnum + 1 &>/dev/null
[ ? -ne 0 ] && echo "please input a number." && exit 2
[# -ne 1 ] && echo 'Usage:0 number' && exit 1
[num -le 0 ] && echo "please input a number bigger than 0" && exit 3
s=1
for i in `seq 1 num`
do   s=((s*i))
done
echo "- result: $s"

(2)效果

经过多次测试与调整,最终结果如下:

file

(八) 编写 shell 脚本,实现分段函数求值。分段函数如下:

file

要求利用多分支 if 条件语句和 for 循环语句实现,并分别输出 x=-5,-3,-1,1,2,3,5 时的值。

(1)主要设计

将分段函数设计成脚本函数形式,通过外部数据传入与调用可以实现函数多次使用,设计如下:

#!/bin/bash
echo "-------- Start ----------"
echo

# Function

func() {
  local t=1
  local y=0
  if [t -ne -3 ]&&[ t -lt 0 ];then
    let y=t*t+t-6

  elif [ t -ne 2 ]&&[t -ne 3 ]&&[ t -lt 5 ]&&[t -ge 0 ];then
    let y=t*t-t*5+6

  else let y=t*t-t+1
  fi  

  echo " ---------the result: y"
  echo
}

# Entrance

x=(5 -3 -1 1 2 3 5)
for i in{x[@]}
do
  echo " - when the value is: x={i}"
  funci
done
echo "--------- end! ---------"

(2)效果

输出结果如下:
file

总结

本次实践主要为Linux系统下权限与监视管理、Shell脚本编程的综合应用,一方面可以熟悉相关的原理与知识,另一方面可以用来初步探索脚本的现实应用。

脚本的可执行可以直接通过sh 文件名、bash 文件名、./文件名(需要提前赋予可执行权限)的方式执行,此实践联系了终端单行命令与脚本集成命令、权限与程序设计等,体验到了三种方式的区别与相同点,比如大多数命令均可执行但sh可能不支持echo的换行符等。

条件选择与循环是程序的基础构成之一,以上实践也比较好地运用到了这些语句,提高了程序的灵活性与可执行性。

当然,shell脚本的变量引用与传递、数据的处理与计算等远没有现今流行的Java、C/C++、Python等好用,引用指令不够自然,参数多样化且含有许多字符等,这也是一个不太方便和难以快速学习掌握的一点,也要求我们多加以思考、练习去真正掌握shell的相关知识与应用。