zoukankan      html  css  js  c++  java
  • shell命令编程小例

    程序目的:设计一个shell程序,分别实现1)锁定终端屏幕,2)选择文件编辑器编辑文件,3)启动您所想要启动的工具,4)使用C文件输出当地时间   四个功能

    程序分析:锁定终端屏幕过程需要忽略SIGHUP,SIGINT,SIGQUIT,SIGTERM,SIGTSTP信号,调用trap命令,然后设置一个密码,锁定终端屏幕,利用while循环检验解锁密码是否正确,不正确则一直循环,从而达到锁定终端屏幕的目的;选择文件编辑器编辑文件就是各种编辑器命令的调用;启动工具,例如firefox,可以使用which获得firefox可执行程序位置,再调用之即可;C文件输出当地时间,编译已经写好的代码,用shell调用可执行程序

    /*--------------------main-----------------------*/
    #!/bin/bash

    while true
    do

    echo  "1)锁定终端屏幕"
    echo  "2)选择文件编辑器编辑文件"
    echo  "3)启动您所想要启动的工具"
    echo  "4)使用C文件输出当地时间"
    echo  "5)退出"

    echo  -n "请输入您想要的操作编号:"
    read  num

    case $num in
    1) bash terminallock;;
    2) bash edit;;
    3) bash toolset;;
    4) ./checktime;;
    5) exit;;

    esac

    done

    /*--------------------terminallock-----------------------------*/

    #!/bin/bash
    #终端锁定工具

    secret()
    {
    #设置密码
    echo -n "请输入你的密码:"
    read firstpasswd
    echo

    #确认密码
    echo -n "请再次输入你的密码:"
    read secpasswd
    echo
    }


    #忽略SIGHUP,SIGINT,SIGQUIT,SIGTERM,SIGTSTP信号
    trap '' 1 2 3 15 20

    clear
    stty -echo

    secret

    while [ $firstpasswd != $secpasswd ]
    do
       clear
       echo "您输入的密码不一致,请重新设定"
       secret
    done

    clear
    echo -n "请输入您的密码:"
    read input
    echo

    #当用户输入密码与设定密码不一致时,则一直在while中循环
    while [ $firstpasswd != $input ]
    do
       clear
       echo -n "您输入的密码错误,请再次输入:"
       read input
    done
         
    clear
    echo "back again"
    stty echo
    exit 0

    /*----------------------------tooleset-----------------------*/

    #!/bin/bash

    echo -n "请输入您想要启动的工具:"
    read name
    str=which $name
    bash $str
    exit

    /*----------------------------checktime.c---------------------------*/

    #include <stdio.h>
    #include <time.h>
    int main()
    {
      time_t t;
      t=time(0);
      printf("%s\n",asctime(localtime(&t)));

    }

    /*----------------------------edit--------------------------------*/

    #!/bin/bash

    while true
    do

    echo "1)-----vi-------"
    echo "2)-----vim------"
    echo "3)-----pico-----"
    echo "4)-----gedit----"
    echo "5)-----emacs----"
    echo "6)-----quit-----"


    echo -n "请输入您想选择的编辑器编号:"
    read num

    case $num in
     1) vi;;
     2) vim;;
     3) pico;;
     4) gedit;;
     5) emacs;;
     6) exit;;
    esac


    done

    运行结果:

    king@ubuntu:~$ bash main
    1)锁定终端屏幕
    2)选择文件编辑器编辑文件
    3)启动您所想要启动的工具
    4)使用C文件输出当地时间
    5)退出
    请输入您想要的操作编号:1

    请输入你的密码:
    请再次输入你的密码:

    请输入您的密码:

    back again
    1)锁定终端屏幕
    2)选择文件编辑器编辑文件
    3)启动您所想要启动的工具
    4)使用C文件输出当地时间
    5)退出

    请输入您想要的操作编号:4
    Sat Sep 11 13:44:10 2010

    1)锁定终端屏幕
    2)选择文件编辑器编辑文件
    3)启动您所想要启动的工具
    4)使用C文件输出当地时间
    5)退出

    请输入您想要的操作编号:3
    请输入您想要启动的工具:firefox

  • 相关阅读:
    Android Training精要(二)開啟ActionBar的Overlay模式
    Android Training精要(一)ActionBar上级菜单导航图标
    SimpleDateFormat的线程安全问题
    svn删除目录后提交显示Item 'XXXX' is out of date解决方法
    使用 python 开发 Web Service
    Bootstrap 中文文档教程
    起步 简介整个项目、组件、和如何使用一个简单的模版入门
    mysql笔记——索引
    20个数据库设计的最佳实践
    ASP.NET MVC Bootstrap极速开发框架
  • 原文地址:https://www.cnblogs.com/chuxiking/p/1823924.html
Copyright © 2011-2022 走看看