例1:

#!/bin/bash
sum=0;
for i in {1..100..2}
do
 let "sum+=i"
done
echo "the sum is $sum"

例2:不知道循环次数,批量解压缩

#!/bin/bash
cd /root
ls *.sh > ls.log
y=1
for i in $(cat ls.log)
  do
   echo $y
    y=$(( $y + 1 ))
done

例3:知道解压缩

#!/bin/bash
s=0
for(( i=1;i<=100;i=i+1 ))
do

s=$(( $s +i ))
echo "1jiadao100 is $s"
done

例4:批量添加用户

#!/bin/bash
read -t 30 -p "input name" name
read -t 30 -p "input num"  num
read -t 30 -p "input pass" pass
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
then
y=$(echo $num|sed 's/^[0-9]*$'//g)
if [  -z "$y" ]
then
for (( i=1;i<$num;i=i+1 ))
do
useradd $name$i &>/dev/null
echo $pass | passwd --stdin $ "$name$i" &>/dev/null
done
fi
fi---------------

格式为 if  then fi  。if后有空格,【】 !前有空格 -z之前有空格

例5:

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

#!/bin/bash
groupadd class1
for ((i=1;i<=30;i++))
do
if [ $i -lt 10 ];then
username="std0"$i
else
username="std"$i
fi
useradd -G class1 $username
done

例6:向脚本传递参数

脚本内容:vim xjbcdcs

--------------------------

#!/bin/bash
echo "helloworld"
echo $?
echo $@
echo $#
-------------------------

./xjbcdcs a b c

结果:

helloworld
0
a b c
3

例7:for循环 里面有shell命令行

输出文件名

#! /bin/bash

#使用ls命令的执行结果作为列表
for file in $(ls)
do
   #输出每个文件名
   echo "$file"
done

例8:九九乘法表

#!/bin/bash
for ((i=1;i<=9;i++))
do
 for((j=1;j<=i;j++))
 do
 let "cheng=i*j"
 printf "$i*$j=$cheng"
 if [[ "$cheng" -gt 9 ]]
 then
 printf "   "
 else
 printf "    "
 fi

done

echo

done

-------------------

注:用expr 如何实现呢。

[root@localhost ~]# expr 10/2
10/2
[root@localhost ~]# expr 10 / 2
5
[root@localhost ~]# expr 10 * 2
expr: syntax error
[root@localhost ~]# expr 10 \* 2
20

用let计算好像更方便快捷一些

[root@localhost ~]# let a=5-1
[root@localhost ~]# echo $a
4
[root@localhost ~]# let a=8*9
[root@localhost ~]# echo $a
72

例9:备份mysql数据库(本例稍改动,只打包某个目录)

#!/bin/bash
date=$(date +%y%m%d)
size=$(du -sh /etc)
if [ -d /tmp/dbback ];then
  echo  "Date is :$date"  > /tmp/dbback/db.txt
  echo  "size is :$size" >>/tmp/dbback/db.txt
  cd /tmp/dbback
  tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
  rm -rf /tmp/dbback/db.txt

else
          mkdir /tmp/dbback
 echo  "Date is :$date"  > /tmp/dbback/db.txt
  echo  "size is :$size" >>/tmp/dbback/db.txt
  cd /tmp/dbback
  tar -zcf etc_$date.tar.gz /etc db.txt &>/dev/null
  rm -rf /tmp/dbback/db.txt

例十:监控脚本

linux下监控cpu、memo、io、swap性能数据

直接贴脚本:
1、cpu

#!/bin/bash
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime\t`top -b -n 1 | grep Cpu\(s\)`"
echo -e $mytext >> /home/www/monitor/log/cpu$CurrentDate.log

2、memo

#!/bin/bash
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime\t`top -b -n 1 | grep Mem:`"
echo -e $mytext >> /home/www/monitor/log/memo$CurrentDate.log

3、io

#!/bin/bash
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime\t`iostat -p sda | grep -w sda`"
echo -e $mytext >> /home/www/monitor/log/io$CurrentDate.log

4、swap

#!/bin/bash
CurrentDate=`date -d today '+%Y%m%d'`
CurrentTime=`date -d today '+%Y%m%d%H%M'`
mytext="$CurrentTime\t`top -b -n 1 | grep Swap:`"
echo -e $mytext >> /home/www/monitor/log/swap$CurrentDate.log

shell 实例脚本的更多相关文章

  1. [Python]在python中调用shell脚本,并传入参数-02python操作shell实例

    首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell ...

  2. (转)shell实例手册

    原文地址:http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02d46d5c0d9 实在是太好的资料了,不得不转 shell实例手册 0说明{ 手册制 ...

  3. shell常见脚本30例

    shell常见脚本30例 author:headsen chen  2017-10-19  10:12:12 本文原素材出自网上,特此申明.有些地方加入我自己的改动 常见的30例shell脚本 1.用 ...

  4. shell实例利用crontab自动清除日志

    shell实例利用crontab自动清除日志 程序运行会产生很多的日志,对于无用的日志手动删除比价麻烦,写一个自动执行的命令是很有必要的. 删除文件shell命令 find 对应目录 -mtime + ...

  5. shell 实例

    转载自:https://github.com/liquanzhou/ops_doc    这里只作为笔记使用,不做他用 shell实例手册 0 说明{ 手册制作: 雪松 更新日期: 2018-09-1 ...

  6. 【转载】shell实例手册

    原文地址:shell实例手册  作者:没头脑的土豆 shell实例手册 0说明{ 手册制作: 雪松 更新日期: -- 欢迎系统运维加入Q群: 请使用"notepad++"打开此文档 ...

  7. (转) shell实例手册

    shell实例手册 1文件{ touch file              # 创建空白文件rm -rf 目录名           # 不提示删除非空目录(-r:递归删除 -f强制)dos2uni ...

  8. 《Linux就该这么学》培训笔记_ch04_Vim编辑器与Shell命令脚本

    <Linux就该这么学>培训笔记_ch04_Vim编辑器与Shell命令脚本 文章最后会post上书本的笔记照片. 文章主要内容: Vim编辑器 Shell脚本 流程控制语句 if语句 f ...

  9. Linux 就该这么学 CH04 VIM编辑器和Shell命令脚本

    0 概述 1 Vim编辑器 在linux 中一切都是文件,而配置一个服务就是修改其配置文件的参数. vim 编辑器有三种模式:命令模式,末行模式和编辑模式. 命令模式:控制光标移动,对文件进行操作. ...

随机推荐

  1. 【HDOJ】4057 Rescue the Rabbit

    挺有意思的一道题目,解法是AC自动机+DP.AC自动机建立fail指针时,一定要注意结点的属性也需要传递.AC自动机结合了trie和kmp的优点.需要注意的是,每个模式串仅计算一次,否则这题很难解. ...

  2. trash目录: ~/.local/share/Trash

    trash目录:~/.local/share/Trash

  3. Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces

    问题解决:缺少jar包 cglib-2.1.3.jar

  4. epub显示特殊字体

    You need to open the ePub in an archive program (they are just ZIP files) and add an XML file to the ...

  5. Nginx实现七层负载均衡配置指导

    本文描述了如何使用Nginx实现在应用层实现7层负载均衡功能,Nginx支持虚拟主机,可以按照轮询,IP哈希,URL哈希,权重方式对后端服务器做负载均衡,还支持后端服务器健康检查功能.废话不多说,详细 ...

  6. r.js 配置文件 build.js 不完整注释

      -----------------------------------------------------------------------r.js 配置文件 example.build.js ...

  7. 【JS】Beginner7:Functions

    1.Function=Reusable blocks of code Passed arguments &  return a value save functions as the valu ...

  8. (4)I2C总线的7bit从机地址

    时钟拉伸(Clock stretching)clock stretching通过将SCL线拉低来暂停一个传输.直到释放SCL线为高电平,传输才继续进行.clock stretching是可选的,实际上 ...

  9. Azure VM对远程桌面登录的支持-示例

    我们在开发Windows Azure的应用程序,虽然在大部分的情况下都可以使用Azure Emulator模拟器来模拟在云端计算节点(Azure VM)的执行结果,但是并不能100%模拟真正在Azur ...

  10. HW3.10

    public class Solution { public static void main(String[] args) { int number1 = (int)(Math.random() * ...