3

That's all. They just won't work for me. What did I do wrong this time?

    # nquo is: /home/bryan/renametest/C D/y z

    # script:
    dirn=dirname "$nquo"
    echo "dirn $dirn"
    bnam=basename "$nquo"
    echo "bnam $bnam"

Run result:

    ./script3.sh: 208: /home/bryan/renametest/C D/y z: Permission denied
    dirn 
    ./script3.sh: 208: /home/bryan/renametest/C D/y z: Permission denied
    bnam 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2021539
  • 949
  • 3
  • 14
  • 31
  • 1
    you can check the permissions of the file/directory with `ls -ld "$nquo"` – shx2 Apr 04 '13 at 05:37
  • 2
    Is this bash? If so you are missing `$(...)` or backticks around `dirname` and `basename` invocations. Plus, working with filenames containing spaces is very tricky. – Jim Garrison Apr 04 '13 at 05:40
  • @JimGarrison No, it isn't tricky at all if you know what you do. – glglgl Apr 04 '13 at 06:35
  • @glglgl: It requires care and attention to detail. It is much harder to handle names with spaces, etc., than names without. Or, put another way, when the names contain no spaces, you can get away with sloppy techniques that happen to work only because there are no spaces in the names. – Jonathan Leffler Apr 04 '13 at 08:00
  • @JonathanLeffler I always put `"`...`"` around my variables and everything works. – glglgl Apr 04 '13 at 08:12
  • @glglgl: That is perhaps 95% of the battle; always use double quotes around the variable names. You still have to exercise caution. For those of us who learned shell programming when spaces in file names were abhorred and such files ignored or renamed, there is culture shock. Most of my scripts written in the last 10 years will work with names-with-spaces, but probably not all. Names with newlines are a good deal harder to manage because you can't pass them around using one-line-is-one-name conventions. The utilities don't always help. Yes, it can be done, most of the time. No, it ain't easy. – Jonathan Leffler Apr 04 '13 at 14:47
  • @JonathanLeffler That's right; a little bit of care is necessary in these extreme cases. I try to use the `-0` options wherever possible - but that is not always possible... – glglgl Apr 04 '13 at 15:08

1 Answers1

10

As it stands, your script is trying to run the file named in $nquo first with the environment variable dirn set to the value dirname, and then with the variable bnam set to the value basename. Since it is not executable, you get the error message about not being able to execute the file.

You presumably intended to run the commands on the name of the file, which requires either back-ticks or (preferably) $(...) around it:

dnam=$(dirname "$nquo")
bnam=$(basename "$nquo")
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278