SoFunction
Updated on 2025-03-10

Shell script checks IP format and mysql operation example

Shell script checks IP format and mysql operation example

Updated: May 26, 2015 11:29:37 Submission: junjie
This article mainly introduces the shell script check IP format and mysql operation examples. This article directly gives the script code. Friends who need it can refer to it.

It is still part of the cronjob, which is to run interactively in the background in Rails timed tasks.

CheckIPAddress()
{
    echo $1 |grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null
    if [ $? = 1 ];  then
        return 1
    else
        a=`echo $1 | awk -F. '{print $1}'`
        b=`echo $1 | awk -F. '{print $2}'`
        c=`echo $1 | awk -F. '{print $3}'`
        d=`echo $1 | awk -F. '{print $4}'`
        #echo $a $b $c $d

        for loop in $a $b $c $d
        do
            if [ $loop -ge 255 ] || [ $loop -lt 0 ]; then
                return 2
            fi
        done
    fi  

}


ConfigureDefaultRegion() {
  echo "Please input Region ip"
  ret=1
  while [ $ret != 0 ]
  do
   read region_ip
   CheckIPAddress $region_ip
   ret=$?
   #echo $ret
   if [ $ret = 1 ]; then
    echo "Wrong IP address, please reinput Region IP:"
   fi
  done
  /usr/bin/mysql -u root realworx_production -e "update regions set ip='$region_ip' where id=1" 1>/dev/null 2>/dev/null
  if [ $? = 0 ]; then
          /usr/bin/mysql -u root realworx_production -e "update config_params set val=1 where ident=55" 1>/dev/null 2>/dev/null
          echo "set '$region_ip' as Default and Admin Region IP"
  else
          val=`/usr/bin/mysql -u root realworx_production -e "select id from regions where ip='$region_ip'" | awk '{if ($1 != "id") print $1}'`
          /usr/bin/mysql -u root realworx_production -e "update config_params set val='$val' where ident=55" 1>/dev/null 2>/dev/null
          region_name=`/usr/bin/mysql -u root realworx_production -e "select name from regions where ip='$region_ip'" | awk '{if ($1 != "name") print $1}'`
          echo "IP already exists. So set '$region_name' as Admin Region. "
  fi
  echo "Region Setting Successfull."
}

  • Shell
  • script
  • IP format
  • mysql
  • operate

Related Articles

  • Introduction to the use of linux commands and rpm commands for decompressing rpm package

    rpm is a RedHat package management tool that implements the function of adding/removing programs similar to Windows. This article introduces you to the introduction of the Linux command and rpm commands that decompress the rpm package. For those who need it, please refer to it.
    2015-11-11
  • Shell scripts to monitor MySQL master-slave synchronization

    This article mainly introduces the Shell script to monitor MySQL master-slave synchronization. This script should be able to adapt to various internal and external network environments and monitor the running status of MySQL at the same time. Friends who need it can refer to it
    2015-01-01
  • Detailed explanation of the output of the "time" command under shell(bash)

    This article mainly introduces the output of the shell(bash) "time" command. The article provides detailed sample code. I believe it has certain reference value for everyone's understanding and learning. Friends in need, let's take a look together.
    2016-12-12
  • How to split files and merge using the Split command in Linux

    Linux split is a command line tool used to split files into multiple smaller files. It can be divided according to file size, number of lines or specified split characters. This article introduces the operation method of Linux using the Split command to split files and merge. Friends who need it can refer to it.
    2024-03-03
  • Steps to install lnmp using shell script

    This article mainly introduces the methods and steps for installing lnmp using shell. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let's take a look with the editor
    2018-12-12
  • Shell scripts realize local files and server files synchronization

    This article mainly introduces the method of synchronizing shell scripts with local files and server files. In the article, the shell script backup file of the local server to the remote server example code. Friends who need it can refer to it
    2017-11-11
  • Getting started with comprehensive learning of Linux shell scripts

    This article mainly shares information about Linux shell scripts. For Linux systems, shell scripts are very practical and powerful.
    2013-10-10
  • Use Rsync to complete automated backups under Linux

    For operation and maintenance personnel, data backup is one of the most important tasks. rsync is a tool/software for backing up important data through the network.
    2018-09-09
  • bash shell gets the absolute path of the current script (pwd/readlink)

    Sometimes, we need to know the absolute path of the currently executed output shell script. This article mainly introduces the absolute path of bash shell to obtain the current script. The example code is introduced in this article very detailed and has certain reference value. Interested friends can refer to it.
    2022-02-02
  • Passwd command in Linux sets the method to modify user password

    Below, the editor will bring you an article on how to set the passwd command under Linux to modify the user password. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor
    2017-02-02

Latest Comments