Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Permission denied" on dirname and basename

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 
like image 807
user2021539 Avatar asked Nov 15 '25 22:11

user2021539


1 Answers

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")
like image 94
Jonathan Leffler Avatar answered Nov 17 '25 10:11

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!