简单的shell脚本

1.用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下。


参考程序:

C代码  

  1. #!/bin/sh
  2. FILENAME=
  3. echo “Input file name:”
  4. read FILENAME
  5. if [ -c "$FILENAME" ]
  6. then
  7. cp $FILENAME /dev
  8. fi

2.设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。


参考答案:

C代码  

  1. #!/bin/sh
  2. i=1
  3. groupadd class1
  4. while [ $i -le 30 ]
  5. do
  6. if [ $i -le 9 ] ;then
  7. USERNAME=stu0${i}
  8. else
  9. USERNAME=stu${i}
  10. fi
  11. useradd $USERNAME
  12. mkdir /home/$USERNAME
  13. chown -R $USERNAME /home/$USERNAME
  14. chgrp -R class1 /home/$USERNAME
  15. i=$(($i+1))
  16. done


3.编写shell程序,实现自动删除50个账号的功能。账号名为stud1至stud50。

参考程序:

C代码  

  1. #!/bin/sh
  2. i=1
  3. while [ $i -le 50 ]
  4. do
  5. userdel -r stud${i}
  6. i=$(($i+1 ))
  7. done


4.某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:
(1)在下午4 :50删除/abc目录下的全部子目录和全部文件;
(2)从早8:00~下午6:00每小时读取/xyz目录下x1文件中每行第一个域的全部数据加入到/backup目录下的bak01.txt文件内;
(3)每逢星期一下午5:50将/data目录下的所有目录和文件归档并压缩为文件:backup.tar.gz;
(4)在下午5:55将IDE接口的CD-ROM卸载(假设:CD-ROM的设备名为hdc);
(5)在早晨8:00前开机后启动。

参考答案:
解决方案:
(1)用vi创建编辑一个名为prgx的crontab文件;
(2)prgx文件的内容:

C代码  

  1. 50 16 * * * rm -r /abc/*
  2. 0 8-18/1 * * * cut -f1 /xyz/x1 >;>; /backup/bak01.txt
  3. 50 17 * * * tar zcvf backup.tar.gz /data
  4. 55 17 * * * umount /dev/hdc

(3)由超级用户登录,用crontab执行 prgx文件中的内容:
[email protected]:#crontab prgx;在每日早晨8:00之前开机后即可自动启动crontab。


5.设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下。

参考答案:
(1)编写shell程序fileback:

C代码  

  1. #!/bin/sh
  2. DIRNAME=`ls /root | grep bak`
  3. if [ -z "$DIRNAME" ] ; then
  4. mkdir /root/bak
  5. cd /root/bak
  6. fi
  7. YY=`date +%y`
  8. MM=`date +%m`
  9. DD=`date +%d`
  10. BACKETC=$YY$MM$DD_etc.tar.gz
  11. tar zcvf $BACKETC /etc
  12. echo “fileback finished!”

(2)编写任务定时器:

C代码  

  1. echo “0 0 1 * * /bin/sh /usr/bin/fileback” >; /root/etcbakcron
  2. crontab /root/etcbakcron
  3. 或使用crontab -e 命令添加定时任务:
  4. 0 1 * * * /bin/sh /usr/bin/fileback


6.有一普通用户想在每周日凌晨零点零分定期备份/user/backup到/tmp目录下,该用户应如何做?

参考答案:

(1)第一种方法:

C代码  

  1. 用户应使用crontab –e 命令创建crontab文件。格式如下:
  2. 0 0 * * sun cp –r /user/backup /tmp

(2)第二种方法:
用户先在自己目录下新建文件file,文件内容如下:

C代码  

  1. 0 * * sun cp –r /user/backup /tmp

然后执行 crontab file 使生效。


7.设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。

参考答案: 建立程序 Pro16如下:

C代码  

  1. #!/bin/sh
  2. i=1
  3. while [ i -le 50 ]
  4. do
  5. if [ -d /userdata ];then
  6. mkdir -p /userdata/user$i
  7. chmod 754 /userdata/user$i
  8. echo “user$i”
  9. let “i = i + 1″ (或i=$(($i+1))
  10. else
  11. mkdir /userdata
  12. mkdir -p /userdata/user$i
  13. chmod 754 /userdata/user$i
  14. echo “user$i”
  15. let “i = i + 1″ (或i=$(($i+1))
  16. fi
  17. done

8、mysql备份实例,自动备份mysql,并删除30天前的备份文件

C代码  

  1. #!/bin/sh
  2. #auto backup mysql
  3. #wugk  2012-07-14
  4. #PATH DEFINE
  5. BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`
  6. MYSQLDB=www
  7. MYSQLPW=backup
  8. MYSQLUSR=backup
  9. if[ $UID -ne 0 ];then
  10. echo This script must use administrator or root user ,please exit!
  11. sleep 2
  12. exit 0
  13. fi
  14. if[ ! -d $BAKDIR ];then
  15. mkdir -p $BAKDIR
  16. else
  17. echo This is $BAKDIR exists ,please exit ….
  18. sleep 2
  19. exit
  20. fi
  21. ###mysqldump backup mysql
  22. /usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB >/data/backup/mysql/`date +%Y-%m-%d`/www_db.sql
  23. cd $BAKDIR ; tar -czf  www_mysql_db.tar.gz *.sql
  24. cd $BAKDIR ;find  . -name “*.sql” |xargs rm -rf[ $? -eq 0 ]&&echo “This `date +%Y-%m-%d` RESIN BACKUP is SUCCESS”
  25. cd /data/backup/mysql/ ;find . -mtime +30 |xargs rm -rf

9、自动安装Nginx脚本,采用case方式,选择方式,也可以根据实际需求改成自己想要的脚本

C代码  

  1. #!/bin/sh
  2. ###nginx install shell
  3. ###wugk 2012-07-14
  4. ###PATH DEFINE
  5. SOFT_PATH=/data/soft/
  6. NGINX_FILE=nginx-1.2.0.tar.gz
  7. DOWN_PATH=http://nginx.org/download/
  8. if[ $UID -ne 0 ];then
  9. echo This script must use administrator or root user ,please exit!
  10. sleep 2
  11. exit 0
  12. fi
  13. if[ ! -d $SOFT_PATH ];then
  14. mkdir -p $SOFT_PATH
  15. fi
  16. download ()
  17. {
  18. cd $SOFT_PATH ;wget $DOWN_PATH/$NGINX_FILE
  19. }
  20. install ()
  21. {
  22. yum install pcre-devel -y
  23. 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
  24. [ $? -eq 0 ]&&make &&make install
  25. }
  26. start ()
  27. {
  28. lsof -i :80[ $? -ne 0 ]&&/usr/local/nginx/sbin/nginx
  29. }
  30. stop ()
  31. {
  32. ps -ef |grep nginx |grep -v grep |awk ‘{print $2}’|xargs kill -9
  33. }
  34. exit ()
  35. {
  36. echo $? ;exit
  37. }
  38. ###case menu #####
  39. case $1 in
  40. download )
  41. download
  42. ;;
  43. install )
  44. install
  45. ;;
  46. start )
  47. start
  48. ;;
  49. stop )
  50. stop
  51. ;;
  52. * )
  53. echo “USAGE:$0 {download or install or start or stop}”
  54. exit
  55. esac

10、批量解压tar脚本,批量解压zip并且建立当前目录。

C代码  

  1. #!/bin/sh
  2. PATH1=/tmp/images
  3. PATH2=/usr/www/images
  4. for i in `ls ${PATH1}/*`
  5. do
  6. tar xvf  $i  -C $PATH2
  7. done

这个脚本是针对所有tar文件在一个目录,但是实际情况中,有可能在下级或者更深的目录,我们可以使用find查找

C代码  

  1. #!/bin/sh
  2. PATH1=/tmp/images
  3. PATH2=/usr/www/images
  4. for i in `find  $PATH1  -name  ”*.tar” `
  5. do
  6. tar xvf  $i  -C $PATH2
  7. done

如何是zip文件,例如123189.zip 132342.zip 等等批量文件,默认unzip直接解压不带自身目录,意思是解压123189.zip完当前目录就是图片,不能创建123189目录下并解压,可以用shell脚本实现

C代码  

  1. #!/bin/sh
  2. PATH1=/tmp/images
  3. PATH2=/usr/www/images
  4. cd $PATH1
  5. for i in `find  . -name  ”*.zip”|awk  -F.  {print $2} `
  6. do
  7. mkdir -p   PATH2$i
  8. unzip -o  .$i.zip  -d   PATH2$i
  9. done

原创文章转载请注明: Linux常用Shell脚本珍藏 | 专注Unix/Linux领域

ssh 批量上传文件

上传文件大多数用的是ftp,但是用ftp有一点不好,就是本地和远程的目录要对应,这样就要在多个目录下去切换,这样挺麻烦的,如果不注意的话,很有可能传错。所以想了个办法利用scp来批量上传文件或者目录。

一,scp上传不要输入密码

如果要用scp来上传文件,第一步就要去掉scp上传时要输入密码。要不然就没办法批量上传了。具体请参考:ssh 不用输入密码

二,ssh批量上传脚本

1,要上传的文件列表放到一个test文件中

C代码  

  1. [email protected]:/home/zhangy# cat test
  2. /home/zhangy/test/aaa
  3. /home/zhangy/test/nginx.conf
  4. /home/zhangy/test/test.sql
  5. /home/zhangy/test/pa.txt
  6. /home/zhangy/test/password

上面就要上传的文件。

2,批量上传的脚本

vim file_upload.sh

C代码  

  1. #!/bin/sh
  2. DATE=`date +%Y_%m_%d_%H`
  3. if [ $1 ]
  4. then
  5. for file in $(sed ‘/^$/d‘ $1)     //去掉空行
  6. do
  7. if [ -f $file ]                 //普通文件
  8. then
  9. res=`scp $file $2:$file`      //上传文件
  10. if [ -z $res ]                //上传成功
  11. then
  12. echo $file >> ${DATE}_upload.log   //上传成功的日志
  13. fi
  14. elif [ -d $file ]              //目录
  15. then
  16. res=`scp -r $file $2:$file`
  17. if [ -z $res ]
  18. then
  19. echo $file >> ${DATE}_upload.log
  20. fi
  21. fi
  22. done
  23. else
  24. echo "no file" >> ${DATE}_error.log
  25. fi

上传成功后,返回的是一个空行,上传不成功,什么都不返回

3,上传的格式

C代码  

  1. ./file_upload.sh test 192.168.1.13

test是上传列表文件,192.168.1.13文件要传到的地方。

0

转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/linux/1356.html

1. 转换文件大小写:

A script to convert the specified filenames to lower case.

C代码  

  1. #!/bin/sh
  2. # lowerit
  3. # convert all file names in the current directory to lower case
  4. # only operates on plain files--does not change the name of directories
  5. # will ask for verification before overwriting an existing file
  6. for x in `ls`
  7. do
  8. if [ ! -f $x ]; then
  9. continue
  10. fi
  11. lc=`echo $x  | tr ‘[A-Z]‘ ‘[a-z]‘`
  12. if [ $lc != $x ]; then
  13. mv -i $x $lc
  14. fi
  15. done

or

C代码  

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <files>" 1>&2
  4. exit 1
  5. fi
  6. for filename in "[email protected]"
  7. do
  8. new_filename=`echo "$filename" | tr A-Z a-z`
  9. test "$filename" = "$new_filename" && continue
  10. if test -r "$new_filename"
  11. then
  12. echo "$0: $new_filename exists" 1>&2
  13. elif test -e "$filename"
  14. then
  15. mv "$filename" "$new_filename"
  16. else
  17. echo "$0: $filename not found" 1>&2
  18. fi
  19. done

2. 看网站 Watch a Website

A script to repeated download a webpage until it matches a regex then notify an e-mail address.

For example to get e-mail when Kesha tickets (not for yourself of course) go on sale you might run:

C代码  

  1. % watch_website.sh http://ticketek.com.au/ ‘Ke[sS$]+ha‘ [email protected]
  2. repeat_seconds=300  #check every 5 minutes
  3. if test $# = 3
  4. then
  5. url=$1
  6. regexp=$2
  7. email_address=$3
  8. else
  9. echo "Usage: $0 <url> <regex>" 1>&2
  10. exit 1
  11. fi
  12. while true
  13. do
  14. if wget -O- -q "$url"|egrep "$regexp" >/dev/null
  15. then
  16. echo "Generated by $0" | mail -s "$url now matches $regexp" $email_address
  17. exit 0
  18. fi
  19. sleep $repeat_seconds
  20. done

3. 转GIF到PNG convert GIF files to PNG

This scripts converts GIF files to PNG files via the intermediate PPM format.

C代码  

  1. if [ $# -eq 0 ]
  2. then
  3. echo "Usage: $0 files..." 1>&2
  4. exit 1
  5. fi
  6. if ! type giftopnm 2>/dev/null
  7. then
  8. echo "$0: conversion tool giftopnm not found " 1>&2
  9. exit 1
  10. fi
  11. # missing "in ..." defaults to in "[email protected]"
  12. for f
  13. do
  14. case "$f" in
  15. *.gif)
  16. # OK, do nothing
  17. ;;
  18. *)
  19. echo "gif2png: skipping $f, not GIF"
  20. continue
  21. ;;
  22. esac
  23. dir=`dirname "$f"`
  24. base=`basename "$f" .gif`
  25. result="$dir/$base.png"
  26. giftopnm "$f" | pnmtopng > $result && echo "wrote $result"
  27. done

4.  计数 Counting

A utility script to print the sub-range of integers specified by its arguments.

Useful to use on the command line or from other scripts

C代码  

  1. if test $# = 1
  2. then
  3. start=1
  4. finish=$1
  5. elif test $# = 2
  6. then
  7. start=$1
  8. finish=$2
  9. else
  10. echo "Usage: $0 <start> <finish>" 1>&2
  11. exit 1
  12. fi
  13. for argument in "[email protected]"
  14. do
  15. if echo "$argument"|egrep -v ‘^-?[0-9]+$‘ >/dev/null
  16. then
  17. echo "$0: argument ‘$argument‘ is not an integer" 1>&2
  18. exit 1
  19. fi
  20. done
  21. number=$start
  22. while test $number -le $finish
  23. do
  24. echo $number
  25. number=`expr $number + 1`    # or number=$(($number + 1))
  26. done

5. 字频率 Word Frequency

Count the number of time each different word occurs in the files given as arguments.

C代码  

  1. sed ‘s/ /\n/g‘ "[email protected]"|      # convert to one word per line
  2. tr A-Z a-z|               # map uppercase to lower case
  3. sed "s/[^a-z‘]//g"|       # remove all characters except a-z and ‘
  4. egrep -v ‘^$‘|             # remove empty lines
  5. sort|                     # place words in alphabetical order
  6. uniq -c|                  # use uniq to count how many times each word occurs
  7. sort -n                   # order words in frequency of occurrance

For example

C代码  

  1. % cd /home/cs2041/public_html/lec/shell/examples
  2. % ./word_frequency.sh dracula.txt|tail
  3. 2124 it
  4. 2440 that
  5. 2486 in
  6. 2549 he
  7. 2911 a
  8. 3600 of
  9. 4448 to
  10. 4740 i
  11. 5833 and
  12. 7843 the

6. Finding

Search $PATH for the specified programs

C代码  

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <program>" 1>&2
  4. exit 1
  5. fi
  6. for program in "[email protected]"
  7. do
  8. program_found=‘‘
  9. for directory in `echo "$PATH" | tr ‘:‘ ‘ ‘`
  10. do
  11. f="$directory/$program"
  12. if test -x "$f"
  13. then
  14. ls -ld "$f"
  15. program_found=1
  16. fi
  17. done
  18. if test -z $program_found
  19. then
  20. echo "$program not found"
  21. fi
  22. done

Alternative implementation using while, and a cute use of grep and ||

C代码  

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <program>" 1>&2
  4. exit 1
  5. fi
  6. for program in "[email protected]"
  7. do
  8. echo "$PATH"|
  9. tr ‘:‘ ‘\n‘|
  10. while read directory
  11. do
  12. f="$directory/$program"
  13. if test -x "$f"
  14. then
  15. ls -ld "$f"
  16. fi
  17. done|
  18. egrep ‘.‘ || echo "$program not found"
  19. done

And another implementation using while, and a cute use of grep and ||

C代码  

    1. if test $# = 0
    2. then
    3. echo "Usage $0: <program>" 1>&2
    4. exit 1
    5. fi
    6. for program in "[email protected]"
    7. do
    8. n_path_components=`echo $PATH|tr -d -c :|wc -c`
    9. index=1
    10. while test $index -le $n_path_components
    11. do
    12. directory=`echo "$PATH"|cut -d: -f$index`
    13. f="$directory/$program"
    14. if test -x "$f"
    15. then
    16. ls -ld "$f"
    17. program_found=1
    18. fi
    19. index=`expr $index + 1`
    20. done
    21. test -n $program_found || echo "$program not found"
    22. done
时间: 2024-08-03 17:16:43

简单的shell脚本的相关文章

一个简单的shell脚本

一个简单的shell脚本 编写 假设我想知道目前系统上有多少人登录,使用who命令可以告诉你现在系统有谁登录: 1.[[email protected] ~]$ who2.KANO tty1 2016-02-15 01:47 (:0)3.KANO pts/0 2016-02-15 01:48 (kelvin) 当然在个人电脑上,所列出来的列表可能只有上面这么短.但是在一些大型.多用户的系统上,所列出的列表就可能很长.这个时候我们可以使用自动计算用户总数.wc是一个字数计算程序,它可以计算出行数(

一些简单的shell脚本例子

         对于刚开始学shell脚本的人来说,建立编程思维很重要,需要能够把自己需要做的事情,用编程的方式表达出来,下面是我学习和搜集的一些例子,对于刚刚开始接触的人,或许有一定的帮助.      求任意数的和或乘积,先定义函数,任意数的和.乘积,然后通过case结构再定义变量,调用函数. hei(){ x=1 while [ $# -gt 0 ] do x=`expr $x \* $1` shift done echo $x } he(){ x=0 while [ $# -gt 0 ]

编写简单的shell脚本 - for循环 - 解决报错 Syntax error: Bad for loop variable

为了编写批量导入数据的程序,故而学习编写shell脚本!现学现用! ============================================ 1.第一个简单的for循环 #!/bin/bashfor i in 1 2 3 4;do echo $i;done 2.测试for的自增长的循环: #!/bin/bashfor ((i=1; i<=5; i++))do echo $i;done 如果会报错,没有则跳过: Syntax error: Bad for loop variable

编写简单的Shell脚本

在一些复杂的Linux维护工作中,大量重复性的输入和操作不但费时费力,而且容易出错,而编写一个恰到好处的shell脚本程序,可以批量处理.自动化的完成一系列维护任务,大大减轻了管理员的负担 shell脚本基础 1. 编写第一个shell脚本 Linux中的shell脚本是一个特殊的应用程序,介于操作系统内核和用户之间,充当了一个命令解释器的角色,负责接收用户的操作指令并进行解释,将需要执行的操作传递给内核执行,并输出执行结果 常见的shell解释器 可通过/etc/shells文件了解当前系统所

小鸟初学Shell编程(二)编写简单的Shell脚本

Shell脚本 编写Python.PHP脚本通常需要掌握语言的函数,那么Shell脚本则不需要,只需要掌握Linux命令就可以编写Shell脚本,因为Shell脚本就是由多个Linux命令组成,通过将多个Linux命令组合保存成一个脚本文件,可直接给其他人使用. 组合命令 进入一个目录,查看目录的文件,这个过程分别需要执行两条命令,分别是cd 和ls. 分开执行两个命令的形式如下: [[email protected] usr]# cd /usr/ [[email protected] usr]

两个简单的shell脚本(关于启动和服务)

实验要求: 1.为root用户编写登陆欢迎脚本,报告当前主机中的进程数,已登陆用户数,根文件系统的磁盘使用率 2.编写三个脚本程序:start,stop和status,用来启动,停止各种系统服务 实验步骤: [[email protected] ~]# vi welcome.sh #!/bin/bash # this is test.by shengjie p=`ps aux | wc -l` //设置变量p echo "Running Processes:$p" u=`who | w

一个简单的shell脚本-----实现虚拟机实验环境的简单配置

亲自写过shell脚本后才发现,这玩意真是太方便了,当你想把一些琐碎的.细节性的小指令一次性来完成时,脚本无疑是最好的选择,方便.快捷,关键是真是懒人必备啊. 由于安装的centos6.5是最小化安装,且是实验环境,即安装在vmware workstations上面,在首次安装完成后,配置完IP地址的相关信息后,就做了一个初始的快照,以便下次实验完后,可以通过快照快速还原系统. 但是由于实验环境的要求,例如防火墙.SELinux.光盘挂载.yum源配置等,都是最常需要修改的选项,并且由于最小化安

一些简单的shell脚本实例

1.词频统计 sed -e s/'[[:punct:]]'/' '/g -e s/'[[:digit:]]'/' '/g $filename| tr [A-Z] [a-z] | tr '\n' ' '| tr ' ' '\n' | sed '/^$/d' >>newfilename #干掉文本中的标点和数字 #替换大小写 #替换换行符为空格后再替换空格为换行符(可能不必要) #删除空行重定向到新文件 sort newfilename| uniq -c | sort -n #排序后删除重复项并统

我的一些简单的shell脚本实例

1.模拟linnux登录shell #/bin/bashecho -n "login:" read nameecho -n "password:"read passwdif [ $name = "cht" -a $passwd = "abc" ];thenecho "the host and password is right!"else echo "input is error!"fi