Error dirname in shell script: Invalid option--B

Source: Internet
Author: User

Today, when I write a shell script, I find that debugging will make a mistake.

[Email protected] autoawrrpt]$. a.sh dirname: Invalid option--B Please try to perform "dirname--help" for more information. BaseName: Invalid option--B Please try executing "basename--help" for more information.

The script reads as follows:

[[email protected] autoawrrpt]$ cat a.sh#!/bin/bash####################################### ######################################### #自动生成最新的AWR报表 # Version: v1.0# variable description: #LOGFILE: Save log file name ########################## ######### #修改历史 ################################################################################################# ################### #WORKDIR =$ (dirname $0)                        #获取到脚本所在目录cd  ${WORKDIR}                                #改变当前目录为脚本所在目录WORKDIR = ' pwd '                                 #获取当前目录的具体路径SCRIPTNAME =$ (basename $0)                    #获取脚本名称FPATH = "${workdir}\${scriptname}"             #脚本全路径LOGFILE = "${workdir}/${scriptname}.log"      #设置日志文件名称AWR_TYPE = "html"                              #设置AWR的类型, the value should be HTML or txtawr_home= "${workdir}/awr_report"            #设置存放AWR报表的目录AWR_DAYS =1                                   #设置AWR   Snap retrieved days Hostname= ' hostname '                          #主机名mkdir  -p ${AWR_HOME}                        #如果没有此目录, the Create  -p option represents the creation of none, There is a direct return

The error is in workdir=$ (dirname) This line of content, which makes me feel very strange, first, I did not add the-B option after this command ah, why will the error option-B?

Then add the echo $ output below this line to see what the content is

[Email protected] autoawrrpt]$. A.shdirname: Invalid option--B Please try executing "dirname--help" for more information. -bashbasename: Invalid option--B Please try executing "basename--help" for more information.


That's strange, isn't it? Should I return the current path of the script? How did it become a-base?

We then test:

[[email protected] autoawrrpt]$./a.sh./a.sh

Run directly, the output turns out to be correct, and the output executes the relative path of the script.

Why does it lead to such a problem? To prove this, we run echo $ directly inside the shell.

[Email protected] ~]$ echo $0-bash

And then we look back at the dot command under Linux

Executes the script directly in the current shell with the point command, without opening a new shell execution script

It's not hard to understand what we see here. Because. The command does not open a new shell execution script, so echo $ is the value of our original shell, that is,-bash, and the basename command will be-bash as a parameter to execute, so will error, the wrong option-B.

Know the reason, our problem is very good to solve, run directly, do not run the script by the Point command or the source command.


This article is from the "Database Road" blog, make sure to keep this source http://dbaway.blog.51cto.com/7099215/1693490

Error dirname in shell script: Invalid option--B

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.