1

I'm using Ubuntu and I'm trying to install this Python package via Anaconda. I followed the steps as they are in the link, and when I type cp -Ri $THIS_DIR/PackageFiles/Scripts $CMDIR (line 59 onwards), I get:

cp: invalid option -- 'h'

What is the reason and how can I correct it?

EDIT: The outputs of echo $THIS_DIR and echo $CMDIR are dirname -bash and /home/myusername/cellmodeller, respectively.

muru
  • 69,900
  • 13
  • 192
  • 292
Fabulini
  • 23
  • 1
  • 6
  • 3
    Please attach the output of `echo $THIS_DIR` and `echo $CMDIR` – cutrightjm Mar 14 '20 at 01:19
  • @cutrightjm It is ''dirname -bash'' and "/home/myusername/cellmodeller". – Fabulini Mar 14 '20 at 01:24
  • 3
    The script is meant to be run as one file - i.e. save the script as `installer.sh`, `chmod +x installer.sh`, then call it using `./installer.sh` - have you tried to run it that way, or are you running it one line at a time? The way `$THIS_DIR` is defined using `$0`, it is meant to be used inside of a script – cutrightjm Mar 14 '20 at 01:26
  • The fact that `$THIS_DIR` contains a command rather than a directory shows that you've modified the installation script somehow, or tried and failed to recreate what it is doing (possibly by pasting the contents of the script into a terminal). The error comes from interpreting the `h` in `-bash` as an option. – Kusalananda Mar 14 '20 at 09:47

1 Answers1

2

A couple of things: if THIS_DIR contains dirname -bash, cp -Ri $THIS_DIR/... expands to the equivalent of cp -Ri dirname -bash/... (because of word splitting), that is, cp gets dirname and -bash/... as distinct arguments. That second one starts with a dash, so it tries to interpret the letters in it as options. GNU cp doesn't have -h as an option, so it gives an error.

You could prevent the splitting with quotes, but that doesn't mean much since you probably don't have a directory called dirname -bash with the space and all.

Looking at the script, THIS_DIR is set via

THIS_DIR="`dirname $0`"

note the backticks, they start a command substitution, running the dirname command. If you remove them, a literal string dirname ... is assigned.

Then again, $0 is the name of the running shell or script. The script probably uses that command to find out where the script itself resides, e.g. the path /foo/bar if you ran /foo/bar/scriptname.sh. But in an interactive shell launched normally $0 probably just contains bash, or -bash if it's a login shell.

Like cutrightjm said in comments, that script is meant to run as a script, not as commands individually copied to the terminal. (It still could use quotes around the expansions.)

Of course you could change the assignment to THIS_DIR in the script, or otherwise modify it before running.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • Also, they get an "unknown option" error rather than an "file not found" error (or similar) for trying to copy `dirname` since GNU utilities generally recognizes operands that _look like_ options, even though option processing should have ended at that point (since `dirname` is not an option). – Kusalananda Mar 14 '20 at 10:33
  • @Kusalananda, yes, that's what the GNU utilities do. It's the documented behaviour ([coreutils: "Normally options and operands can appear in any order"](https://www.gnu.org/software/coreutils/manual/html_node/Common-options.html#Common-options), getopt: ["The default is to permute the contents of argv while scanning ... This allows options to be given in any order"](https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt)). ... – ilkkachu Mar 14 '20 at 12:02
  • ... They also document being non-POSIX in some points, and that you can change that by setting `POSIXLY_CORRECT=1` (coreutils: ["In a few cases, the GNU utilities’ default behavior is incompatible with the POSIX standard. To suppress these incompatibilities, define the POSIXLY_CORRECT environment variable."](https://www.gnu.org/software/coreutils/manual/html_node/Standards-conformance.html#Standards-conformance), getopt mentions that on the same page as linked above.) ... – ilkkachu Mar 14 '20 at 12:05
  • ... In any case, that doesn't really seem a very important point for this question, and I have a hard time seeing how it could become a problem for someone. If you have unknown filenames expanded from variables, you should use `--` anyway. Here, if `$THIS_DIR` expanded to `-hoodly doo`, the problem would come up even if the expansion was quoted or `cp` took the strict view on option processing. (FWIW, I find the GNU behaviour useful, it allows me to easily add, e.g. `-l` at the end of an `ls` command line even if I happen to think about needing after writing the rest.) – ilkkachu Mar 14 '20 at 12:09
  • `"$()"` is preferred over over back-ticks. It is easier to use. – ctrl-alt-delor Mar 14 '20 at 16:56
  • @ctrl-alt-delor, the script the question links to has backticks. – ilkkachu Mar 14 '20 at 17:12