shell脚本之练习题
设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。
i=1
groupadd class1
while [ $i -le 30 ]
do
if [ $i -le 9 ] ;then
USERNAME=stu0${i}
else
USERNAME=stu${i}
fi
useradd $USERNAME
chgrp -R class1 /home/$USERNAME
i=$(($i+1))
done
编写shell程序,实现自动删除50个账号的功能。账号名为stud1至stud50。
#!/bin/sh
i=1
while [ $i -le 50 ]
do
userdel -r stud${i}
i=$(($i+1 ))
done
mysql备份实例,自动备份mysql,并删除30天前的备份文件
#!/bin/sh #auto backup mysql
#wugk 2012-07-14
#PATH DEFINE BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`
MYSQLDB=www
MYSQLPW=backup
MYSQLUSR=backup if[ $UID -ne 0 ];then
echo This script must use administrator or root user ,please exit!
sleep 2
exit 0
fi if[ ! -d $BAKDIR ];then
mkdir -p $BAKDIR
else
echo This is $BAKDIR exists ,please exit ….
sleep 2
exit
fi ###mysqldump backup mysql /usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB >/data/backup/mysql/`date +%Y-%m-%d`/www_db.sql cd $BAKDIR ; tar -czf www_mysql_db.tar.gz *.sql cd $BAKDIR ;find . -name “*.sql” |xargs rm -rf[ $? -eq 0 ]&&echo “This `date +%Y-%m-%d` RESIN BACKUP is SUCCESS” cd /data/backup/mysql/ ;find . -mtime +30 |xargs rm -rf
自动安装Nginx脚本,采用case方式,选择方式,也可以根据实际需求改成自己想要的脚本
#!/bin/sh ###nginx install shell
###wugk 2012-07-14
###PATH DEFINE SOFT_PATH=/data/soft/
NGINX_FILE=nginx-1.2.0.tar.gz
DOWN_PATH=http://nginx.org/download/ if[ $UID -ne 0 ];then
echo This script must use administrator or root user ,please exit!
sleep 2
exit 0
fi if[ ! -d $SOFT_PATH ];then
mkdir -p $SOFT_PATH
fi download ()
{
cd $SOFT_PATH ;wget $DOWN_PATH/$NGINX_FILE
} install ()
{
yum install pcre-devel -y
cd $SOFT_PATH ;tar xzf $NGINX_FILE ;cd nginx-1.2.0/ &&./configure –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-http_ssl_module
[ $? -eq 0 ]&&make &&make install
} start ()
{
lsof -i :80[ $? -ne 0 ]&&/usr/local/nginx/sbin/nginx
} stop ()
{
ps -ef |grep nginx |grep -v grep |awk ‘{print $2}’|xargs kill -9
} exit ()
{
echo $? ;exit
} ###case menu ##### case $1 in
download )
download
;; install )
install
;; start )
start
;;
stop )
stop
;; * ) echo “USAGE:$0 {download or install or start or stop}”
exit
esac
文件上传,列出部分代码
#!/bin/sh DATE=`date +%Y_%m_%d_%H` if [ $1 ]
then
for file in $(sed '/^$/d' $1) //去掉空行
do
if [ -f $file ] //普通文件
then
res=`scp $file $2:$file` //上传文件
if [ -z $res ] //上传成功
then
echo $file >> ${DATE}_upload.log //上传成功的日志
fi
elif [ -d $file ] //目录
then
res=`scp -r $file $2:$file`
if [ -z $res ]
then
echo $file >> ${DATE}_upload.log
fi
fi
done
else
echo "no file" >> ${DATE}_error.log
fi
shell脚本之练习题的更多相关文章
- linux 的基本操作(编写shell 脚本)
终于到shell 脚本这章了,在以前笔者卖了好多关子说shell脚本怎么怎么重要,确实shell脚本在linux系统管理员的运维工作中非常非常重要.下面笔者就带你正式进入shell脚本的世界吧. 到现 ...
- shell脚本练习题(更新中...)
练习题(这里贴的是自己写的代码, 网上给的题目代码我会附加在最下面) 1. 编写shell脚本,计算1-100的和: #!/bin/bash #caculate the to `; do sum=$[ ...
- shell 脚本之 shell 练习题汇总
整理了一些 shell 相关的练习题,记录到这里. 1. 请按照这样的日期格式 xxxx-xx-xx 每日生成一个文件,例如:今天生成的文件为 2013-09-23.log, 并且把磁盘的使用情况写到 ...
- shell脚本练习题
下面列出了几个linux学习中的shell脚本题目 1.九九乘法表 #!/bin/bash echo "九九乘法表" # 注意((之间不能有空格.加减乘除的格式.还有转义字符\*. ...
- shell脚本结构
echo $? 代表上一次命令的状态返回值,‘0’则代表为真<执行成功>,‘非零’则代表为假<执行失败>. shell脚本: <判断老男孩的年纪> [root@bo ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- 第三部分shell编程3(shell脚本2)
7. if 判断一些特殊用法 if [ -z $a ] 这个表示当变量a的值为空时会怎么样if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样if ...
- 第一个shell脚本
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...
- 使用C#给Linux写Shell脚本
在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...
随机推荐
- python基础-匿名函数、内置函数、正则表达式、模块
1. 匿名函数 1.1 有名函数 有名函数:定义了一个函数名,函数名指向内存地址:通过函数名进行访问.函数名加括号就可以运行有名函数,例如:func() def func(x, y, z = 1): ...
- Java高级架构师(一)第14节:新增和列表页面和分页tag
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- iOS开发——使用基于Autolayout的ScrollView
问题描述: 在使用Autolayout布局方式对ScrollView进行布局时,如果你想做一个可以垂直方向滚动的九宫格类似这样: 拿一行来说,一定不要想当然的尝试去给一行图标进行均匀排列的操作(指 ...
- Java杂谈5——关键字final与volatile
Final关键字 在Java语言中,随着语境的不同final关键字所代表的语义会有一些细微的差异.总的来说,final关键字表达的含义是“禁止修改”,这层有点类似于C++中的const关键字.之所以要 ...
- Redis Exception: Exceeded timeout of 00:00:03
Redis Exception: Exceeded timeout of 00:00:03 居然是 重启了网管, 把网络禁用重启就好了. 服 最终更新: 原来是架构湿 设置为每分钟只能读取6 ...
- Word2010插入页码分节符
不是分页符,分节符主要是为了插入前后两种不同类型的页码,比如前面查罗马数字,从目录开始插入阿拉伯数字.
- 下载谷歌浏览器(Chrome)扩展离线安装包crx文件最简单的方法
转:http://alyzq.com/?p=627 如果不会使用,请看下面的操作步骤 引言(可以不看): 下面介绍一下,下载谷歌浏览器(Google Chrome)扩展的离线安装包crx文件最简单的方 ...
- CRC代码实现
CRC代码实现1: #include <stdio.h> #include <string.h> unsigned int cfgCrc32(const unsigned ch ...
- WebLogic MBean Monitor
weblogic server提供了一个dashboard让我们对mbean进行图形化的展现和分析,地址是 http://localhost:7001/console/dashboard 但有时候总是 ...
- vue - 认识ora
主要用来实现node.js命令行环境的loading效果,和显示各种状态的图标等... const ora = require('ora'); const spinner = ora('Loading ...