1. ホーム
  2. シェル

シェルスクリプティングユーティリティ関数 Module 1

2022-02-25 20:05:11
<パス

記事目次


    Stacking from 1 to 100{
        echo $[$(echo +{1..100})]
        echo $[(100+1)*(100/2)]
        seq -s '+' 100 |bc
    }
    Determine if the argument is null-empty exit and print null{
        #! /bin/sh
        echo $1
        name=${1:? "null"}
        echo $name
    }
    Loop through the array {
        for ((i=0;i<${#o[*]};i++))
        do
            echo ${o[$i]}
        done
    }
    Determine the path {
        if [ -d /root/Desktop/text/123 ];then
            echo "Found 123"
            if [ -d /root/Desktop/text ]
            then echo "found text"
            else echo "No text found"
            fi
        else echo "Folder 123 not found"
        fi
    }
    Find the most frequent {
        awk '{print $1}' file|sort |uniq -c|sort -k1r
    }
    Determine if the script parameters are correct {
        . /test.sh -p 123 -P 3306 -h 127.0.0.1 -u root
        #! /bin/sh
        if [ $# -ne 8 ];then
            echo "USAGE: $0 -u user -p passwd -P port -h host"
            exit 1
        fi
        while getopts :u:p:P:h: name
        do
            case $name in
            u)
                mysql_user=$OPTARG
            ;;
            p)
                mysql_passwd=$OPTARG
            ;;
            p)
                mysql_port=$OPTARG
            ;;
            h)
                mysql_host=$OPTARG
            ;;
            *)
                echo "USAGE: $0 -u user -p passwd -P port -h host"
                exit 1
            ;;
            esac
        done
        if [ -z $mysql_user ] || [ -z $mysql_passwd ] || [ -z $mysql_port ] || [ -z $mysql_host ]
        then
            echo "USAGE: $0 -u user -p passwd -P port -h host"
            exit 1
        fi
        echo $mysql_user $mysql_passwd $mysql_port $mysql_host
        # results root 123 3306 127.0.0.1
    }
    regular match mailbox {
        ^[_a-z0-9-]+(\. [_a-z0-9-]+)*@[a-z0-9-]+(\. [a-z0-9-]+)*(\. [a-z]{2,4})$
    }
    Print the table {
        #! /bin/sh
        clear
        awk 'BEGIN{
        print "+--------------------+--------------------+";
        printf "|%-20s|%-20s|\n","Name","Number";
        print "+--------------------+--------------------+";
        }'
        a=`grep "^[A-Z]" a.txt |sort +1 -n |awk '{print $1":"$2}'`
        #cat a.txt |sort +1 -n |while read list
        for list in $a
        do
            name=`echo $list |awk -F: '{print $1}'`
            number=`echo $list |awk -F: '{print $2}'`
            awk 'BEGIN{printf "|%-20s|%-20s|\n","'"$name"'","'"$number"'";
            print "+--------------------+--------------------+";
            }'
        done
        awk 'BEGIN{
        print " *** The End *** "
        print " "
        }'
    }
    Determine if the date is legal {
        #! /bin/sh
        while read a
        do
          if echo $a | grep -q "-" && date -d $a +%Y%m%d > /dev/null 2>&1
          then
            if echo $a | grep -e '^[0-9]\{4\}-[01][0-9]-[0-3][0-9]$'
            then
                break
            else
                echo "The date you entered is not legal, please enter it again! "
            " fi
          else
            echo "The date you entered is not legal, please enter a new one! "
          fi
        done
        echo "The date is $a"
    }
    Print all dates in the date field {
        #! /bin/bash
        qsrq=20010101
        jsrq=20010227
        n=0
        >tmp
        while :;do
        current=$(date +%Y%m%d -d"$n day $qsrq&
        i=`expr $i + 1`
        if echo "$lastrow" | grep "#include <[A-Z].h>"
        then
            if echo "$line" | grep -v "#include <[A-Z].h>"
            then
                sed -i ''$i'i\\/\/All header files are include' incl
                i=`expr $i + 1`
            fi
        fi
        lastrow="$line"
        done
    }
    Query the other engines of the database {
        #/bin/bash
        path1=/data/mysql/data/
        dbpasswd=db123
        #MyISAM or InnoDB
        engine=InnoDB
        if [ -d $path1 ];then
        dir=`ls -p $path1 |awk '/\/$/'|awk -F'/' '{print $1}'`
            for db in $dir
            do
            number=`mysql -uroot -p$dbpasswd -A -S "$path1"mysql.sock -e "use ${db};show table status;" |grep -c $engine`
                if [ $number -ne 0 ];then
                echo "${db}"
                fi
            done
        fi
    }
    Batch modify the database engine {
        #/bin/bash
        for db in test test1 test3
        do
            tables=`mysql -uroot -pdb123 -A -S /data/mysql/data/mysql.sock -e "use $db;show tables;" |awk 'NR ! = 1{print}'`
            for table in $tables
            do
                mysql -uroot -pdb123 -A -S /data/mysql/data/mysql.sock -e "use $db;alter table $table engine=MyISAM;"
            done
        done
    }
    Insert the data fetched by the shell into the mysql database {
        mysql -u$username -p$passwd -h$dbhost -P$dbport -A -e "
        use $dbname;
        insert into data values ('','$ip','$date','$time','$data')
        "
    }
    Number of days between two dates {
        D1=`date -d '20070409' +"%s"`
        D2=`date -d '20070304 ' +"%s"`
        D3=$(($D1 - $D2))
        echo $(($D3/60/60/24))
    }
    while executing ssh only loops once {
        cat - # let cat read the connection file stdin
        seq 10 | while read line; do ssh localhost "cat -"; done # The 9 times shown are eaten by ssh
        seq 10 | while read line; do ssh -n localhost "cat -"; done # ssh with -n argument to avoid looping only once
    }
    ssh batch execution command {
        # version 1
        #! /bin/bash
        while read line
        do
        Ip=`echo $line|awk '{print $1}'`
        Passwd=`echo $line|awk '{print $2}'`
        ssh -n localhost "cat -"
        sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no root@$Ip "id"
        done<iplist.txt
        #version2
        #! /bin/bash
        Iplist=`awk '{print $1}' iplist.txt`
        for Ip in $Iplist
        do
        Passwd=`awk '/'$Ip'/{print $2}' iplist.txt`
        sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no root@$Ip "id"
        done
    }
    Print the characters in the same location {
        #! /bin/bash
        echo -ne "\t"
        for i in `seq -w 100 -1 1`
        do
            echo -ne "$i\b\b\b"; # key \b backspace
            sleep 1;
        done
    }
    Simple control of multi-process background concurrency {
        #! /bin/bash
        test () {
            echo $a
            sleep 5
        }
        for a in `seq 1 30`
        do
            test &
            echo $!
            ((num++))
            if [ $num -eq 6 ];then
            echo "wait... "
            wait
            num=0
            fi
        done
        wait
    }
    shell concurrency {
        #! /bin/bash
        tmpfile=$$.fifo # Create pipe name
        mkfifo $tmpfile # Create pipeline
        exec 4<>$tmpfile # Create file labeled 4 to operate the pipe $tmpfile in a read-write manner
        rm $tmpfile # Clear the created pipeline file
        thred=4 # Specify the number of concurrent
        seq=(1 2 3 4 5 6 7 8 9 21 22 23 24 25 31 32 33 34 35) # Create a list of tasks
        # Create a placeholder for the number of concurrent threads
        {
        for (( i = 1;i<=${thre