shell命令编程小例
2010-09-11 13:46
225 查看
程序目的:设计一个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
程序分析:锁定终端屏幕过程需要忽略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
相关文章推荐
- Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
- Linux命令shell脚本编程学习笔记-18章初识sed和gawk
- Linux C/C++编程Shell命令大杂烩(1)
- 深入浅出Shell 编程:Unix/Linux 命令
- (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
- 【shell编程基础3】shell编程的组合应用之二:管道及其命令
- shell编程基础(3.常用概念命令与操作
- shell脚本编程:sed命令编辑文本
- shell脚本的编程命令
- linux shell编程学习笔记(5)shell运算符号和运算命令
- PHP安全编程:shell命令注入
- Shell脚本编程之结构化命令
- shell脚本编程基础(二)----使用结构化命令
- Linux命令与shell脚本编程学习笔记-16创建函数
- Shell 编程与命令 无关
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- Linux 命令及shell编程备忘
- Shell脚本编程基础 三 使用结构化命令
- Linux 基础命令(六)—— SHELL脚本编程基础
- shell脚本编程教程之一 --- 基本命令+环境变量+正则表达式