readlink: invalid option -- 'b' and dirname: invalid option -- 'b'.

19 views
Skip to first unread message

hongy...@gmail.com

unread,
Apr 5, 2022, 10:37:10 AMApr 5
to
Today, when I tried to connect to my computer using SSH, I noticed the following strange information after logging in:

```
$ ssh werner@localhost
[...]
dirname: invalid option -- 'b'
Try 'dirname --help' for more information.
readlink: invalid option -- 'b'
Try 'readlink --help' for more information.
```

What do they mean, and what triggered them?

Regards,
HZ

Lew Pitcher

unread,
Apr 5, 2022, 11:09:27 AMApr 5
to
What do they mean? Well, on the face of it, they mean that
neither dirname(1) nor readlink(1) take a 'b' option.

As for what triggered these error messages, your guess
is as good as ours. Most likely, you have some poorly
formed commands in the various shell scripts invoked
when you establish an SSH connection and login.

Look for dirname and readlink in those scripts, and ensure
that the arguments passed to those commands properly
resolve to syntatically correct commands. Specifically,
be suspicious of any substitution variables, as they may
not have the values you expect.

HTH
--
Lew Pitcher
"In Skills, We Trust"

hongy...@gmail.com

unread,
Apr 5, 2022, 9:01:21 PMApr 5
to
The following is some code snippet in one of the various shell scripts invoked when I establish an SSH connection and login:

```
unset scriptdir_realpath
unset script_realdirname script_realname
unset script_realbasename script_realextname
unset script_realpath pkg_realpath

scriptdir_realpath=$(cd -P -- "$(dirname -- "${1:-${BASH_SOURCE[0]}}")" && pwd -P)

script_realdirname=$(dirname -- "$(realpath -e -- "${1:-${BASH_SOURCE[0]}}")")
script_realname=$(basename -- "$(realpath -e -- "${1:-${BASH_SOURCE[0]}}")")

script_realbasename=${script_realname%.*}
script_realextname=${script_realname##*.}

script_realpath=$script_realdirname/$script_realname
pkg_realpath=${script_realpath%.*}

unset script_dirname script_name
unset script_basename script_extname
unset script_path

script_dirname=$(cd -- "$(dirname -- "${1:-${BASH_SOURCE[0]}}")" && pwd)
script_name=$(basename -- "${1:-${BASH_SOURCE[0]}}")

script_basename=${script_name%.*}
script_extname=${script_name##*.}

script_path=$script_dirname/$script_name
```

Is my usage reasonable in the above code snippet?

Regards,
HZ

Lew Pitcher

unread,
Apr 6, 2022, 9:00:40 AMApr 6
to
On Tue, 05 Apr 2022 18:01:18 -0700, hongy...@gmail.com wrote:

> On Tuesday, April 5, 2022 at 11:09:27 PM UTC+8, Lew Pitcher wrote:
>> On Tue, 05 Apr 2022 07:37:07 -0700, hongy...@gmail.com wrote:
>>
>> > Today, when I tried to connect to my computer using SSH, I noticed the following strange information after logging in:
>> >
>> > ```
>> > $ ssh werner@localhost
>> > [...]
>> > dirname: invalid option -- 'b'
>> > Try 'dirname --help' for more information.
>> > readlink: invalid option -- 'b'
>> > Try 'readlink --help' for more information.
>> > ```
>> >
>> > What do they mean, and what triggered them?
>> What do they mean? Well, on the face of it, they mean that
>> neither dirname(1) nor readlink(1) take a 'b' option.
>>
>> As for what triggered these error messages, your guess
>> is as good as ours. Most likely, you have some poorly
>> formed commands in the various shell scripts invoked
>> when you establish an SSH connection and login.
>>
>> Look for dirname and readlink in those scripts, and ensure
>> that the arguments passed to those commands properly
>> resolve to syntatically correct commands. Specifically,
>> be suspicious of any substitution variables, as they may
>> not have the values you expect.
>
> The following is some code snippet in one of the various shell scripts invoked when I establish an SSH connection and login:
>
> ```
[snip]
> scriptdir_realpath=$(cd -P -- "$(dirname -- "${1:-${BASH_SOURCE[0]}}")" && pwd -P)
>
> script_realdirname=$(dirname -- "$(realpath -e -- "${1:-${BASH_SOURCE[0]}}")")
> script_realname=$(basename -- "$(realpath -e -- "${1:-${BASH_SOURCE[0]}}")")
[snip]
> script_dirname=$(cd -- "$(dirname -- "${1:-${BASH_SOURCE[0]}}")" && pwd)
> script_name=$(basename -- "${1:-${BASH_SOURCE[0]}}")
[snip]
> Is my usage reasonable in the above code snippet?

It depends.

What is the snipped /supposed/ to do, and what does ${BASH_SOURCE[0]} expand to?

Lew Pitcher

unread,
Apr 6, 2022, 9:01:37 AMApr 6
to
And, what does $1 expand to?

hongy...@gmail.com

unread,
Apr 6, 2022, 9:17:45 AMApr 6
to
I use it as a bash based library script, which, when sourced from another bash script with this script's name as the argument, i.e., $1, will give some path/script name related information of the invoking script. OTOH, if the script is sourced without any argument, the ${BASH_SOURCE[0]} will be used to gather path/script name related information of the library script itself.

Regards,
HZ

Lew Pitcher

unread,
Apr 6, 2022, 10:59:27 AMApr 6
to
And now we know what the snippet is supposed to do.

As for your failure mode, you still need to answer the questions
(specifically, when you encounter the error messages),
1) what does ${BASH_SOURCE[0]} expand to?
2) what does $1 expand to

hongy...@gmail.com

unread,
Apr 6, 2022, 9:00:45 PMApr 6
to
See the following:

$ cat bash-lib.sh
unset scriptdir_realpath
unset script_realdirname script_realname
unset script_realbasename script_realextname
unset script_realpath pkg_realpath

scriptdir_realpath=$(cd -P -- "$(dirname -- "${1:-${BASH_SOURCE[0]}}")" && pwd -P)

script_realdirname=$(dirname -- "$(realpath -e -- "${1:-${BASH_SOURCE[0]}}")")
script_realname=$(basename -- "$(realpath -e -- "${1:-${BASH_SOURCE[0]}}")")

script_realbasename=${script_realname%.*}
script_realextname=${script_realname##*.}

script_realpath=$script_realdirname/$script_realname
pkg_realpath=${script_realpath%.*}

unset script_dirname script_name
unset script_basename script_extname
unset script_path

script_dirname=$(cd -- "$(dirname -- "${1:-${BASH_SOURCE[0]}}")" && pwd)
script_name=$(basename -- "${1:-${BASH_SOURCE[0]}}")

script_basename=${script_name%.*}
script_extname=${script_name##*.}

script_path=$script_dirname/$script_name

echo ''
echo '${BASH_SOURCE[0]}=' ${BASH_SOURCE[0]}
echo '$1=' $1

$ cat test-bash-lib.sh
. bash-lib.sh

echo ''
echo '${BASH_SOURCE[0]}=' ${BASH_SOURCE[0]}
echo '$1=' $1

$ . bash-lib.sh

${BASH_SOURCE[0]}= bash-lib.sh
$1=

$ . test-bash-lib.sh

${BASH_SOURCE[0]}= bash-lib.sh
$1=

${BASH_SOURCE[0]}= test-bash-lib.sh
$1=

Regards,
HZ

Lew Pitcher

unread,
Apr 7, 2022, 12:05:02 PMApr 7
to
[snip]

That tells me nothing that I can't see from your previous snippet.

So, let's recap.

You have a script or scripts, buried somewhere within the sum total
of scripts that ssh executes on your behalf when you log in to a
remote system, that generate specific error messages. You wish to
debug and rectify these error messages. The error messages are:
dirname: invalid option -- 'b'
Try 'dirname --help' for more information.
readlink: invalid option -- 'b'
Try 'readlink --help' for more information.

First off, the error messages are mostly self-explanatory. Somewhere
in your scripts, you invoke dirname(1) with the "-b" option, and
elsewhere, you invoke readlink(1), again with the "-b" option. Neither
dirname(1) nor readlink(1) /have/ a "-b" option, and tell you so.

So, you have to determine
1) where the offending dirname(1) is,
2) how that dirname(1) gets an errant "-b" option,
3) where the offending readlink(1) is, and
4) how that readlink(1) gets an errant "-b" option

Then, you have to change things so that neither the
dirname(1) nor the readlink(1) get the bad "-b" option.

By implication, you have isolated the offending dirname(1) and
readlink(1) invocations to a specific code snippet, which you
posted here. These invocations depend on the /specific values/
of two parameters: $1 and ${BASH_SOURCE[0]}. The values of these
two parameters, on login via ssh, will tell you if you have
located the offending code snippet. At least one of these values
will contain a "-b", the offending option passed to dirname(1)
and readlink(1).

Find that, and fix it.

HTH
--30--

Helmut Waitzmann

unread,
Apr 7, 2022, 3:09:31 PMApr 7
to
"hongy...@gmail.com" <hongy...@gmail.com>:
>On Tuesday, April 5, 2022 at 11:09:27 PM UTC+8, Lew Pitcher wrote:
>> On Tue, 05 Apr 2022 07:37:07 -0700, hongy...@gmail.com wrote:
>>
>>> Today, when I tried to connect to my computer using SSH, I
>>> noticed the following strange information after logging in:
>>>
>>> ```
>>> $ ssh werner@localhost
>>> [...]
>>> dirname: invalid option -- 'b'
>>> Try 'dirname --help' for more information.
>>> readlink: invalid option -- 'b'
>>> Try 'readlink --help' for more information.
>>> ```
>>>
>>> What do they mean, and what triggered them?
>>>
>> What do they mean? Well, on the face of it, they mean that
>> neither dirname(1) nor readlink(1) take a 'b' option.
>>
>> As for what triggered these error messages, your guess
>> is as good as ours. Most likely, you have some poorly
>> formed commands in the various shell scripts invoked
>> when you establish an SSH connection and login.
>>
>> Look for dirname and readlink in those scripts, and ensure
>> that the arguments passed to those commands properly
>> resolve to syntatically correct commands. Specifically,
>> be suspicious of any substitution variables, as they may
>> not have the values you expect.
>
>The following is some code snippet in one of the various shell
>scripts invoked when I establish an SSH connection and login:
>

[…]

In that code snippet, there is no invocation of "readlink".


Also, in that code snippet, all invocations of "dirname" are of the
form

dirname -- further parameters

=> Even, if a "-b" were part of the further parameters, it would not
be interpreted by "dirname" as an option "b" because of the option
terminator "--".

=> That code snippet is not the cause of the error messages.

hongy...@gmail.com

unread,
Apr 7, 2022, 10:03:58 PMApr 7
to
I should have written the following:

$ cat test-bash-lib.sh
. bash-lib.sh ${BASH_SOURCE[0]}

echo ''
echo '${BASH_SOURCE[0]}=' ${BASH_SOURCE[0]}
echo '$1=' $1



> $ . bash-lib.sh
>
> ${BASH_SOURCE[0]}= bash-lib.sh
> $1=
>
> $ . test-bash-lib.sh
>
> ${BASH_SOURCE[0]}= bash-lib.sh
> $1=
>
> ${BASH_SOURCE[0]}= test-bash-lib.sh
> $1=

And accordingly:

$ . test-bash-lib.sh

${BASH_SOURCE[0]}= bash-lib.sh
$1= test-bash-lib.sh

${BASH_SOURCE[0]}= test-bash-lib.sh
$1=

> Regards,
> HZ
Reply all
Reply to author
Forward
0 new messages