Examples of Linux shell scripts

Source: Internet
Author: User


Shell scripting is the most basic knowledge of our OPS management, and here are a few examples of my learning process (compared to Daniel). The purpose of this blog is to record the course of learning scripts, but also to be able to discuss learning with the reader.

# example1:  template   Script name for automatically creating scripts:creat_scripts.sh #  function Description: Creat_scripts.sh scripts_name   If the created script name file does not exist, create a script file;#  if the corresponding file exists and is a script file, open the file to the last line;#  if the corresponding file exists but is not a script file, you are prompted to exit. #!/bin/bash# description: create a script model# version: 0.0.1#  author: alex# date: 2014-07-09#  the problem of judging parameters if [ $# -lt 1 ];then         echo  "usage:  ' basename $0 '  scrpipt_name."         exit 2fiif [ ! -e  "$"  ];then/ bin/touch  "$" cat >  "$"  <<eof#!/bin/bash# description:# version:#  date:# author: Alex# license: GPLEOFvim +  "$" #  here to determine if the script is normally exited, Determine whether there is  . SCRIPTS_NAME.SWP hidden files if [ ! -e  "'/usr/bin/dirname $1 '. $1.swp"  ];then         [ !  -x $1 ] && /bin/chmod +x  "$"          bash -n $1 &>/dev/null        result =$?         [  $result  -ne 0 ] &&  echo  "$ (bash -n $1)"  fielse[[   '/usr/bin/file  "'  =~   bourne-again\ shell\ script\ text\ executable$ ]] && vim  +  "$  |" |  echo  "this is not scripts."  && exit 2if [ ! -e  "'/usr/bin/dirname $1 ' $1.swp"  ];then                 [ !  -x $1 ] && /bin/chmod +x  "$"                  bash -n $1 &>/dev/null                 result=$?                  [  $result  -ne 0 ] &&  echo  "$ (bash -n $1)"          fifi
# example2: To interpret whether a IP&NBSP;V4 address is a a,b,c class address, and if so, print out the default mask #  This script is created by Example1 script #!/bin/bash#  description:# version:0.0.0# date:2014-07-16# author: alex# license: gpl#  The following is the IP address decimal representation of the four-bit regular expression fistip= "[1-9]| [1-9] [0-9]|11[0-9]|12[1-68-9]|1[3-9][0-9]|2[0-1][0-9]|22[0-3] "secondip=" [0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] "thirdip=" [0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] "fourip=" [1-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4] "read -p " PLEASE&NBSP;INPUT&NBSP;IP&NBSP;ADDR: " ip_ addrwhile true ;d o         #  equivalent to   while  : ;d oif echo  "$ip _addr"  | grep -E  "^\< ($fistip) \>\.\< ($ SECONDIP) \>\.\< ($thirdip) \>\.\< ($fourip) \>$ " &>/dev/null;then#  The following sentence and above should have the same function, but in fact not, the author is studying #if [[  "$ip _addr"  =~ ^\<\ ($fistip \) \>\.\<\ ($ secondip\) \>\.\<\ ($thirdip \) \>\. \<\ ($fourip \) \>$ ]];thenifconfig eth0  $ip _addr &>/dev/nullhead_ip= ' echo   "$ip _addr"  | cut -d. -f1 ' if [  $head _ip -ge 1 -a  $head _ ip -le 126 ];thenecho  "$ip _addr mask is 255.0.0.0" elif [  $head _ip  -ge 128 -a  $head _ip -le 191 ];thenecho  "$ip _addr mask is  255.255.0.0 "elif [  $head _ip -ge 192 -a  $head _ip -le 223 ]; then                         echo  "$ip _addr mask is 255.255.255.0" Fiexit  3elseread -p  "PLEASE&NBSP;AGAIN&NBSP;INPUT&NBSP;IP&NBSP;ADDR:"  ip_addrfidone

Will not be updated periodically .....

This article from "Upstream Cold" blog, reproduced please contact the author!

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.