常用shell脚本总结

一、计算100以内奇偶数的和的几种方法

1)、方法一 {1..100..2}

[root@station78 scirtp]# cat 6.sh#!/bin/shevensum=0for i  in {1..100..2}do        evensum=$[$evensum+$i]doneecho "1+3+5+...=$evensum"oddsum=0for I  in {2..100..2}do        oddsum=$[$oddsum+$I]doneecho "2+4+6+..=:$oddsum."[root@station78 scirtp]# sh 6.sh1+3+5+...=25002+4+6+..=:2550.

2)、方法二 `seq 1 2 100`

[root@station78 scirtp]# cat 6.sh#!/bin/shevensum=0for i  in `seq 1 2 100`do        evensum=$[$evensum+$i]doneecho "1+3+5+...=$evensum"oddsum=0for I  in {2..100..2}do        oddsum=$[$oddsum+$I]doneecho "2+4+6+..=:$oddsum."[root@station78 scirtp]# sh 6.sh1+3+5+...=25002+4+6+..=:2550.[root@station78 scirtp]#

3)、方法三 使用单个for循环

[root@station78 scirtp]# cat 5.sh#!/bin/shoddsum=0evensum=0for i in {1..100}do    if [ $[$i%2] -eq 0 ]         then          oddsum=$[$oddsum+$i]        else            evensum=$[$evensum+$i]fidoneecho "2+4+6+..=$oddsum"echo "1+3+5+..=$evensum"[root@station78 scirtp]# sh 5.sh2+4+6+..=25501+3+5+..=2500[root@station78 scirtp]#

二、批量创建用户

1、添加10个用户user1, user2, ..., user10;并设置密码,但要先判断用户是否存在,不存在而后再添加;

2、添加完成后,显示一共添加了几个用户;当然,不能包括因为事先存在而没有添加的;

3、最后显示当前系统上共有多少个用户

   删除用户同理,这里就偷懒了。。

三、位置变量的应用($!,$@,$2....)

1)、使用位置变量($1),查看输入的用户是否是计算机已有的用户,如果是则比较其uidgid是否相同。

[root@station78 scirtp]# cat 4.sh#!/bin/shif id $1 &> /dev/nullthen    echo "$1  exists"  if [ `id -u $1` -eq  `id -g $1` ]    then    echo "$1 uid 等于gid"    else    echo "$1 uid 不等于gid"   fielse    echo "$1 no exists"fi                                                                                                                                                                                                                                                                                      [root@station78 scirtp]# sh 4.sh rootroot  existsroot uid 等于gid[root@station78 scirtp]# sh 4.sh roottroott no exists[root@station78 scirtp]# sh 4.sh uucpuucp  existsuucp uid 不等于gid

2)、通过参数传递一系列用户名(a,b,c,d) 给脚本,让脚本添加这些用户;但要先判断用户是否存在,不存在而后再添加;添加完成后,显示一共添加了几个用户;当然,不能包括因为事先存在而没有添加的;

使用$@位置变量

[root@station78 scirtp]# cat 7.sh#!/bin/bash#Count=0for UserName in $@; do  if id $UserName &> /dev/null; then    echo "$UserName exists."  else    useradd $UserName    echo "Add $UserName successfully."    Count=$[$Count+1]  fidoneecho "Add $Count new users."[root@station78 scirtp]# sh 7.sh a b c dAdd a successfully.Add b successfully.Add c successfully.Add d successfully.Add 4 new users.[root@station78 scirtp]# tail -4 /etc/passwda:x:4039:4039::/home/a:/bin/bashb:x:4040:4040::/home/b:/bin/bashc:x:4041:4041::/home/c:/bin/bashd:x:4042:4042::/home/d:/bin/bash[root@station78 scirtp]#si

四、shell中的字符串比较

通过位置变量判断一个用户是否是bash用户

改进之后

后续。。。。。。

在使用for循环在遇到的疑惑,曾经以为for循环定义的变量在for循环中一定要调用,这样说可能不太理解,我也有点晕了,还是举例吧

没有使用变量,只是循环了5次

补充

将某个文件夹下所有的文件名字里的大写字母改成小写字母

for file in `ls | grep '[A-Z]'`dostr=`echo $file|tr 'A-Z' 'a-z'`mv $file $strdone1)ls | grep '[A-Z]' :ls 出所有含有大写字母的文件2)for file in `command` :for 循环3)echo AVdxFV | tr 'A-Z' 'a-z' : 把'AVdxFV' 中所有的大写换成小写字母; tr :translate的意思,具体看help

小编shell脚本能力有限,不足之处还望各位博友多提宝贵意见,继续改进,希望我们共同进步。。