Shell- shell脚本的小应用范例

1 expr 对整型变量进行算术运算

+ 加       \* 乘         % 取模       - 减      / 除                                    注意:符号两边必须有空格

例如:vim  test.sh                    //计算水仙花数

#!/bin/bash
a=100
while   [  $a   -lt    1000  ]
do
    x=`expr  $a  /  100`
    z=`expr  &a  %  100`
    b=`expr  &a  /   10`
    y=`expr  &b  %  10`
    q=`expr  $x  \*  $x  \*  $x`
    w=`expr  $y  \*  $y   \*  $y`
    e=`expr  $z  \*  $z   \*  $z`
    let  r=$q+$w+$e
    if  [ $a  -eq  $r ]
    then
        echo  $a
        let  a=$a+1
    else
        let  a=$a+1
    fi
done

2 一键配置本地yum源

#!/bin/bash
echo  “Creating  yum …Please  Wait…”
mkdir  /mnt/cdrom                            //没有挂载点,要创建
mount  /dev/sr0  /mnt/cdrom   >  /dev/dull   2>&1
mkdir    /etc/yum.repos.d/save
mv   /etc/yum.repos.d/C*   /etc/yum.repos.d/save
echo  “[iso]”  >>  /ect/yum.repos.d/my.repo
ehco  “name=xxx”  >>  /ect/yum.repos.d/my.repo
echo  “baseurl=file:///mnt/cdrom”  >> /ect/yum.repos.d/my.repo
echo  “enabled=1”  >>  /ect/yum.repos.d/my.repo
echo  “gpgcheck=0”  >> /ect/yum.repos.d/my.repo
yum  makecache | grep Metadata
echo  “All  done,thanks  for  use this  program!”

3 使用Case语句配置本地yum源

#!/bin/bash
while true
do
    echo "########## menu ##########"
    echo "##this is a menu for yum##"
    echo "1)check the cdrom mounted or not"
    echo "2)mount the cdrom"
    echo "3)create the repo file"
    echo "4)install the software"
    echo "5)remove the software"
    echo "6)quit this menu"
    echo "please input your choice:"
    read a
    case $a in
    1)echo "checking the cdrom mounted or not . . ."
        b=`df|grep sr0|wc -l`
        if [ $b -eq 1 ]
        then
            echo "the cdrom has been mounted"
            c=`df|grep sr0|awk '{print $6}'`
            echo "the mountpoint is $c"
        else
            echo "the cdrom doesn't mounted"
        fi;;

    2)echo "please input the directory where U want to mount:"
        read  d
        echo "now mounting . . ."
        if [ -d $d ]
        then
            mount /dev/sr0 $d & > /dev/null
            echo "the cdrom has been mounted on $d"
        else
            mkdir -p $d
            mount /dev/sr0 $d > /dev/null
            echo "the cdrom has been mounted on $d"
        fi;;

    3)echo "please input the repo file's name:"
        read  e
        rm -rf /etc/yum.repos.d/*.repo
        touch /etc/yum.repos.d/$e.repo
        echo "please input the repo's name:"
        read  f
        echo "[$f]" >> /etc/yum.repos.d/$e.repo
        echo "name=$f" >> /etc/yum.repos.d/$e.repo
        f=`df|grep sr0|awk '{print $6}'`
        echo "baseurl=file://$f" >> /etc/yum.repos.d/$e.repo
        echo "enabled=0" >> /etc/yum.repos.d/$e.repo
        echo "gpgcheck=0" >> /etc/yum.repos.d/$e.repo
        echo "the repo file has been created!";;
    4)echo "please input which software U want to install:"
        read  g
        echo "installing . . ."
        a=`cat /etc/yum.repos.d/*.repo|grep name|awk -F "=" '{print $2}'`
        yum  makecache &> /dev/null
        yum --enablerepo=$a install $g -y &> /dev/null
        echo "the $f has been installed";;
    5)echo "please input which software U want to remove:"
        read  h
        echo "eraser the software . . ."
        yum remove $h -y &> /dev/null
        echo "the $h has been removed";;
    6)break;;
    *)echo "Hey,buddy.Can‘t U input the correct number?!";;
    esac
done

4 查看光盘是否挂载

#!/bin/bash
echo "##############this is a script for mount the cdrom#############"
a=`df|grep sr0|wc -l`
if [ $a -eq 1 ]
then
    echo "the cdrom has been mounted,that's ok,big brother"
else
    echo "the cdrom doesn't mounted"
    mount /dev/cdrom /mnt/cdrom
    echo "big brother,I have gua le na ge guang pan,don't thanks me,my name is LeiFeng"
fi

5 磁盘检查

#!/bin/bash
date "+20%y-%m-%d %H:%M:%S" >> /tmp/disk.log
df -h | tail -n +2 > /tmp/disk.tmp
while read a                //一行一行读取,读的是done<的文件
do
    b=`echo $a | awk '{print $5}'`
    b2=${b%%%*}                 //%%是表示删除右侧的%*,*代表%后面的所有东西
    if [ $b2 -ge 70 ];
    then
        echo $a |awk '{print $1,$4,$5,$6}' >> /tmp/disk.log
    fi
done < /tmp/disk.tmp

echo " " >> /tmp/disk.log
rm -rf /tmp/disk.tmp

6 对变量的值进行截取

x=abc123
y=${x%%1*}:%%表示删除右侧的指定字符,此时y=abc
z=${x##*c}:##表示删除左侧的指定字符,此时z=123

echo $y
echo $z

7 抓取以空格结尾的行

df | grep part\ $

8 计算1+2+3+……+100

#!/bin/bash                               
a=1                                      
sum=1                                   
while [  $a  -lt  10  ]                     
do                                       
    let  a=$a+1                              
    let  sum=$sum+$a                        
done                                    
echo  $sum  

           

9 计算100!

#!/bin/bash
a=1
sum=1
while  [  $a  -lt  10  ]
do
    let  a=$a+1
    sum=`expr  $sum   \*   $a`
done

echo  $sum

转载请注明出处,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼刺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值