Shell Script- Permission Denied

15,441

Solution 1

/home/dell/case3.3/private.key is not a script, and not executable. $(/home/dell/case3.3/private.key) tries to execute that file. It's unclear what you're trying to do, but perhaps what you want is:

test="$(cat /home/dell/case3.3/private.key)"
echo "$test"

Edit in response to OP's comments

You don't need to save a whole file to a variable in order to simply display its contents. Instead, use:

cat /home/dell/case3.3/private.key

If, however, you do want to save off some part of the file, use sed, grep, or awk. For example

test="$(grep 'somethingInTheFile' /home/dell/case3.3/private.key)"
echo "$test"

Or, you could process the file line by line:

while read line; do
  echo $line
done < /home/dell/case3.3/private.key

Solution 2

test="$(/home/dell/case3.3/private.key)"

executes the file /home/dell/case3.3/private.key before assigning it to the variable $test. Since /home/dell/case3.3/private.key does not have the executable bit set you get the permission denied message.

For example on my linux system:

david@david-Mate ~ $ touch xxx
david@david-Mate ~ $ ls -l xxx
-rw-r--r-- 1 david david 0 Aug  2 12:42 xxx
david@david-Mate ~ $ test="$(./xxx)"
bash: ./xxx: Permission denied
Share:
15,441

Related videos on Youtube

user1524529
Author by

user1524529

Updated on June 04, 2022

Comments

  • user1524529
    user1524529 almost 2 years

    I was trying out a very small script

    #!/bin/sh
    
    test="$(/home/dell/case3.3/private.key)"
    
    echo $test
    

    I call this file test1.sh

    I try to access a file named private.key Here is the permission set for the file private.key -rw-rw-r--

    Here is the permission set for the file test1.sh -rwxrwxrwx

    I have also tried to do

    chmod u+x test1.sh

    and

    chmod 755 test1.sh

    In my agony, I also tried chmod 777 test1.sh

    I believe that 777 should give it all the permissions like it did ..

    I don't understand what the problem is, why is there a permission denied

    I act as the super user

  • Addison
    Addison over 11 years
    Sorry run: sudo ./test1.sh But that might not work if your using windows. Idk
  • user1524529
    user1524529 over 11 years
    Is there a way to fetch the contents of the file other than that?
  • Tim Pote
    Tim Pote over 11 years
    I don't know what you mean "fetch the contents of the file." You can use sed, awk, grep, or cat to parse a file.
  • user1524529
    user1524529 over 11 years
    Oh no.. I have to do the dc ->obase and ibase since the file is too huge and it crosses the limit :(
  • user1524529
    user1524529 over 11 years
    By Fetch , I meant displaying the contents of the file.
  • Addison
    Addison over 11 years
    Sorry. I don't know then. Thats really strange
  • user1524529
    user1524529 over 11 years
    I was refering to this one link
  • user1524529
    user1524529 over 11 years
    Thanks a lot for that. However, I will still have to bc << EOF obase=10 ibase=2 $test EOF This way, I will be able to cut it down. without actually pulling out a part of the file and also get an arbitrary precision calculation which I can use later on :)
  • user1524529
    user1524529 over 11 years
    Thanks!. That gives me some more explanation