Learn the shell from someone else's scripting (2)

Keywords: Vmware Database less

Examples:

echo "[${_LogFlag}]:$(date "+%Y%m%d.%H%M%S"):${Use}@${Host}:${2}"|tee -a "${LogFile}";

1.tee command

The tee command is used to redirect data to files. On the other hand, it can provide a copy of redirected data as stdin for subsequent commands. Simply put, redirect data to a given file and screen.

There is a caching mechanism that outputs every 1024 bytes. If the input data is received from the pipeline, the buffer should be full before the data is transferred to the specified file. If the file content is less than 1024 bytes, after receiving the data read from the standard input device, the buffer will be refreshed and the data will be transferred to the specified file.

Grammar:

Tee (options) (parameters)

Options:

- a: Use additional mode when redirecting to files;
- i: Ignore the interrupt signal.

Parameters:

File: A file that specifies output redirection. Print stdout at the terminal and redirect it to the file at the same time:

So the command in the example is to add a log to the file.

This command displays the execution results of ls and outputs these contents to the test.log file

[root@bogon tmp]# ls | tee test.log
con.txt
db2drop.sh
db2reorg.sh
DB2Resource.sh
getopt.sh
keyring-3HEZN9
keyring-jRtzT4
keyring-lY40O6
keyring-OAarNF
keyring-Q1xEdc
keyring-u8JUex
orbit-gdm
orbit-ltt
passtess.txt
pd.key.user.93636
pulse-NbEuBP4f8qOo
SQLDIR.LK0
tess.file
testopt.sh
test.sh
testvariablescope.sh
VMwareDnD
vmware-root
[root@bogon tmp]# cat test.log
con.txt
db2drop.sh
db2reorg.sh
DB2Resource.sh
getopt.sh
keyring-3HEZN9
keyring-jRtzT4
keyring-lY40O6
keyring-OAarNF
keyring-Q1xEdc
keyring-u8JUex
orbit-gdm
orbit-ltt
passtess.txt
pd.key.user.93636
pulse-NbEuBP4f8qOo
SQLDIR.LK0
tess.file
testopt.sh
test.sh
testvariablescope.sh
VMwareDnD
vmware-root

The following sentence sends the results of ls to tee test.log and cat -n respectively, so the contents of test.log are numbered and the output of cat -n is numbered. It is important to understand the pipeline flow chart in the diagram.

[root@bogon tmp]# ls | tee test.log | cat -n
     1  con.txt
     2  db2drop.sh
     3  db2reorg.sh
     4  DB2Resource.sh
     5  getopt.sh
     6  keyring-3HEZN9
     7  keyring-jRtzT4
     8  keyring-lY40O6
     9  keyring-OAarNF
    10  keyring-Q1xEdc
    11  keyring-u8JUex
    12  orbit-gdm
    13  orbit-ltt
    14  passtess.txt
    15  pd.key.user.93636
    16  pulse-NbEuBP4f8qOo
    17  SQLDIR.LK0
    18  tess.file
    19  test.log
    20  testopt.sh
    21  test.sh
    22  testvariablescope.sh
    23  VMwareDnD
    24  vmware-root

sed '$d'

$(echo "${_output}"|sed '$d')

sed'$d'means deleting the last line

Use of awk

Example 1:

$(echo "${_output}"|tail -1|awk '{if($1~"^[-,0-9]*$"){printf "%d",$1}else{printf "%d",5}}')
[db2inst1@bogon tmp]$ db2 "force application all"
DB20000I  The FORCE APPLICATION command completed successfully.
DB21024I  This command is asynchronous and may not be effective immediately.

Example 2

[db2inst1@bogon tmp]$ echo '-1316,-1017'|awk -F, -v c='-1316' '{for(i=1;i<=NF;i++){if($i==c && c!=0){f=1}}}END{if(f==1){printf 100}else{printf "%d",c}}' 
100

If this code is not easy to understand, let's organize and format it.

awk -F, -v c='-1316' '
{for(i=1;i<=NF;i++)
  {if($i==c && c!=0)
    {f=1}
  }
}
END{if(f==1)
       {printf 100}
    else
       {printf "%d",c}
   }' 

Filter out unwanted rows -- the grep command

[db2inst1@bogon tmp]$ db2 list applications for database DBMDB

Auth Id  Application    Appl.      Application Id                                                 DB       # of
         Name           Handle                                                                    Name    Agents
-------- -------------- ---------- -------------------------------------------------------------- -------- -----
DB2INST1 db2bp          6289       *LOCAL.db2inst1.170607011702                                   DBMDB    1    
DB2INST1 db2jcc_applica 6235       192.168.225.1.8374.170607004338                                DBMDB    1    

[db2inst1@bogon tmp]$ db2 list applications for database DBMDB| grep -vE "SQL1611W|^Auth|^$|^-|Agents$"
DB2INST1 db2bp          6289       *LOCAL.db2inst1.170607011702                                   DBMDB    1    
DB2INST1 db2jcc_applica 6235       192.168.225.1.8374.170607004338                                DBMDB    1    

- The v option represents inversion;
- E uses the template style as an extended generic representation, meaning that extended regular expressions can be used.
SQL1611W is returned without application, so filtering out this line is nothing.

grep (global search regular expression(RE) and print out the line, searching regular expressions comprehensively and printing out lines) is a powerful text search tool, which can use regular expressions to search text and print out matching lines.
* Options*

  • - a Don't ignore binary data.
  • - A < Display Column Number > Displays the contents after the row, in addition to the line that conforms to the template style.
  • - b Displays the line that conforms to the template style and the contents before that line.
  • - C < Display Column Number > or -< Display Column Number > displays the contents before and after the column in addition to the one that conforms to the template style.
  • - e < Template Style > Specifies a string as a template style for finding the contents of a file.
  • - E uses the template style as an extended generic representation, meaning that extended regular expressions can be used.
  • - F treats the template style as a list of fixed strings.
  • - h does not indicate the name of the file to which the column belongs before displaying the column that conforms to the template style.
  • - i Hu column character case difference.
  • - Lists the file names whose contents conform to the specified template style.
  • - L lists file names whose contents do not conform to the specified template style.
  • - q does not display any information.
  • - s does not display error messages.
  • - v inversion lookup
  • - w shows only full-character columns.
  • - x shows only columns that match the full column.
  • - y This parameter has the same effect as "-i".
  • - o Outputs only the matched parts in the file.

Posted by redmonkey on Mon, 24 Jun 2019 14:24:21 -0700