Shell script: Write data to disk in bulk

Source: Internet
Author: User

First, about this article

The monitoring system to be done requires monitoring the usage of disk space and alerting. When testing this feature, it is necessary to simulate two scenarios: one is the need to trigger the alarm mechanism when the disk space is constantly growing above the set threshold, and the second is that the disk space drops below the alarm threshold and no more alarms. To test these two scenarios, I wrote the following three scripts:

1) initializer.sh: Create a directory TestDir, and create a large file template

2) duplicator.sh: Copy the file template continuously until the disk space usage exceeds the input parameters

3) cleaner.sh: Remove the traces left by the previous two scripts, i.e. delete the directory TestDir

Second, initializer.sh

#!/bin/sh# This script is used to initialize the work, create a folder TestDir and write to a directory of size 100M # Create a file Testdirif [-X "./testdir"]; Then RM-RF testdirfimkdir testdircd testdirmkdir templatecd templatetouch template.txt# make file size 100K template.txtstring = "" Repetend= "012345678901234|" For ((i=1;i<6400;i++)) do string= $string $repetenddoneecho $string >> template.txt# Copy 1000 of the file i=0while [true]; do if ["$i"-GT 1020]; Then the break fi cp template.txt $i ((i++)) Doneecho "file is finished, space occupancy information is as follows" PWD. Du-sh. cd. /.. Exit 0

This is less efficient and the code is longer, and then the DD command is used to implement the function (initializer.sh):

#!/bin/sh# This script is used to initialize the work, create a folder TestDir and write to a directory of size 100M # Create a file Testdirif [-X "./testdir"]; Then RM-RF testdirfimkdir testdircd testdirdd if=/dev/zero of=template bs=1m count=1024pwd. Du-sh. cd. Exit 0

This script creates the TestDir directory and writes a 1.1GB file template inside it.

Third, duplicator.sh

The script duplicator.sh accepts a 5-95 number as the threshold value. This script keeps copying the template file created by initializer.sh until the disk space usage specified above the input threshold is exceeded.

#!/bin/sh# run the script before running this script  initializer.sh# this script is used to keep copying files until the parameter threshold is given exceeds the current disk space utilization # input parameter: Disk space usage threshold # function: Print script usage description function  usage ()  {    echo  "Usage: ./duplicator [threshold"      echo  "threshold is an integer in the range of [1,99]"     echo  "*run initializer.sh before run this script"      exit 0} #脚本有且只有一个输入if  [  "$#"  -ne 1 ]; then     echo  "script should have only one input" the input of the     usagefi# script must be a positive integer between 5-95 threshold= ' echo $1 &NBSP;|&NBSP;BC ' if [  "$threshold"  -lt 5 -o  "$threshold"  -gt 95 ];  then    echo  "The input of the script must be a positive integer between 5-95"     usagefi# Directory TestDir must exist if [ ! -d ./testdir ]; then     echo  "Missing directory  testdir"     usagefi# file testdir/template must exist If [ ! -f ./testdir/template ]; then     echo  "Missing file  testdir/template"     usageficd testdir# Copy the file over the input threshold until i=0while [ true ]; do    cur= ' df -h |  grep /dev/sda3 | awk  ' {printf substr ($5,1,length ($ 1)} '      echo  "current usage:  $cur  | Object usage:  $threshold"      if [  "$cur"  -gt  "$threshold"  ]; then         break;    fi    cp template  $i      echo  "  $i  duplication complete!"      ((i++)) donecd    #TestDirecho   "script finished!" Exit 0

Iv. cleaner.sh

This script is used to clear traces of the first two scripts left in the system.

#!/bin/sh# This script clears the traces left by the script initializer.sh and duplicator.sh # Check if the file exists if [!-X './testdir "]; then echo "file./testdir does not exist, do not need to clear the" Exit 0fi# user Confirmation after clear file echo "really want to erase all the data? (y/n) "Read Inputcase" $input "in y* |    y*) rm-rf./testdir echo "Data deletion complete";; n |    N) echo "Discard deleted data";; *) echo "input not recognized";; Esacexit 0

V. Effect of the call

END

Shell script: Write data to disk in bulk

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.