background:
The database audit platform for Group self-research has implemented the ability to pull Oracle database performance reports in a single click in a Windows environment.
Recently received the small task of the Audit platform development: The Windows environment implementation of a one-click Pull Oracle Database performance report of the bat script changed to Linux
Shell script.
Script rewrite quickly, more than 300 lines of bat script, half a day to rewrite the shell, the next is the most painful test link.
Start by stepping on the pit:
BAT script when defining a variable, define the password of the connection database as a variable named PWD (below), and the pit begins ...
@echo off...set PWD=xxx...
When I define a user password to connect to Oracle, I refer to the bat script and still use PWD as the password variable:
#################setting variables######################。。TNS=dbnamePWD=pass1234。。#################Making gather&scan Files######################。。sqlplus dbaudit/[email protected]$TNS <<EOF >>sqlplus.log --第1部分here is command1!EOF。。#################Spooling Reports######################cd $SHELL_PATH/$1echo -e "Spooling Reports..."for(( i = 0; i < 10; i++ )) do { sqlplus dbaudit/[email protected]$TNS <<EOF >>sqlplus.log --第2部分 here is command2! EOF}& done wait 。。
In order to pull the Oracle report, there are several operations connected to the database in the script:
sqlplus username/[email protected]$TNS <<EOF >>sqlplus.log...EOF
The script seems to have no flaws, but the 1th part of the execution of the Sqlplus command, you can successfully connect to the database and perform related operations, and execute to the script 2nd part of the Sqlplus command, but always reported sqlplus syntax error!
Why is this?!
Reasoning no fruit, Baidu is impossible to have results, because the error is straightforward, is sqlplus grammar is not correct.
Spooling Reports...SQL*Plus: Release 11.2.0.1.0 Production on Thu May 17 12:28:13 2018Copyright (c) 1982, 2009, Oracle. All rights reserved.SQL*Plus: Release 11.2.0.1.0 ProductionCopyright (c) 1982, 2009, Oracle. All rights reserved.Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus statements.Usage 1: sqlplus -H | -V -H Displays the SQL*Plus version and the usage help. -V Displays the SQL*Plus version.Usage 2: sqlplus [ [<option>] [{logon | /nolog}] [<start>] ]
So, the variable part and the error part of the script extracted, written in the test.sh alone, the evil thing happened, the execution process is very successful!
Strangely, the script before and after the same command, before the success, the back of the failure, why?!
Read the script carefully, in the middle of only one "CD $SHELL _path/$1" command, is this command caused? Oh, no!
Continue to take the error script out alone, an accidental "echo $PWD" revealed the truth!
[[email protected] ~]$ echo $PWD/paic/dba/tmp/padba
Nani! PWD is actually the variable that comes with the Linux system, and the current directory is displayed!
Then, after executing the change directory command such as "CD $SHELL _path/$1", the system's PWD variable overrides the start of the script
Define the pwd=pass1234, that is, after switching directories, the PWD value is not "pass1234", but a directory, that is, "$SHELL _path/$1" corresponding value!
Validation inference:
[[email protected] ~]$ export PWD=pass1234 --手动定义PWD变量[[email protected] pass1234]$ echo $PWDpass1234 --显示PWD变量值为pass1234,没毛病![[email protected] pass1234]$ cd --切换目录[[email protected] ~]$ echo $PWD/paic/dba/tmp/padba --PWD的值变为当前目录!
So far, the truth!
After modifying the PWD in the script to passwd, the error disappears and the script completes the mission of pulling the report successfully!
Summarize:
There are many variables like $pwd in Linux, and when we define variable names in scripts, we want to avoid the variable names that come with the system, otherwise it can lead to strange and addictive error!
Shell Pit Tour--a murder caused by variable $pwd