Error dirname in shell script: Invalid option -- B
When writing a Shell script today, an error is reported during debugging.
[Oracle @ smidb11 autoawrrpt] $. a. sh dirname: Invalid option -- B please try to execute "dirname -- help" for more information. Basename: Invalid option -- B. Please execute "basename -- help" to obtain more information.
The script content is as follows:
[Oracle @ smidb11 autoawrrpt] $ cat a. sh #! /Bin/bash #################################### ######################################## ##### automatically generate the latest AWR Report # version: v1.0 # variable Description: # LOGFILE: save the log file name ################################## modify history ####################################### ######################################## ###################################### WORKDIR = $ (dirname $0) # obtain the script directory cd $ {WORKDIR} # change the current directory to the script directory WORKDIR = 'pwd' # obtain the specific path of the current directory SCRIPTNAME =$ (basename $0) # obtain the Script Name FPATH = "$ {WORKDIR }\$ {SCRIPTNAME}" # full script path LOGFILE = "$ {WORKDIR}/$ {SCRIPTNAME }. log "# Set the log file name AWR_TYPE =" html "# Set the AWR type, the value should be html or txtAWR_HOME = "$ {WORKDIR}/awr_report" # Set the directory where AWR reports are stored AWR_DAYS = 1 # set the number of days for AWR Snap retrieval HOSTNAME = 'hostname' # host name mkdir -p $ {AWR_HOME} # If this directory does not exist, the "Create-p" option indicates that if no option is created, the system returns the result directly.
The error is reported in WORKDIR = $ (dirname $0), which makes me very strange. First, I didn't add the-B option after this command, why is the error option-B Reported?
Then add the echo $0 output under this line to see what the content of $0 is.
[Oracle @ smidb11 autoawrrpt] $. a. shdirname: Invalid option -- B please try to execute "dirname -- help" for more information. -Bashbasename: Invalid option -- B. Please execute "basename -- help" to obtain more information.
This is strange. Shouldn't $0 return the current path of the script? How can it be changed to-base?
We will try again:
[oracle@smidb11 autoawrrpt]$ ./a.sh ./a.sh
Run the command directly and the output result is correct. $0 indicates the relative path of the script to be executed.
Why is this problem? To prove this problem, we run echo $0 directly in the shell.
[mysql@test ~]$ echo $0-bash
Next, let's review the following commands in Linux:
Execute the script directly in the current shell when you execute the script using the dot command, instead of opening a new Shell to execute the script
It's hard to understand this. Because. the command does not open a new Shell to execute the script, so when echo $0, the value of $0 is still the original shell $0, that is,-bash, the basename command regards-bash as a parameter for execution, so an error is reported. The error option is-B.
Once we know the cause, our problem will be well solved. Run it directly. Do not run the script by clicking the command or source command.
This article permanently updates the link address: