Error dirname in shell script: Invalid option -- B

Source: Internet
Author: User

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:

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.