1

Script is on exec partition (under /home/~~~ ) and have +x flag (-rwxr-xr-x ).

executable on bash shell (not with script) but not on script. script is quite simple.

#!/bin/bash

data=cat $PWD/.git/config | awk '{for(i=1;i<NF;i++)if($i~"merge")print$(i+2)}'
echo data : $data

result :

/home001/myaccount/uploader.sh: line 3: /home001/myaccount/mydirectory/.git/config: Permission denied

target file's permission is symbolic and 777 like below.

lrwxrwxrwx

But executable and working well at bash command line.

myaccount@myserver:~/mydirectory$ cat $PWD/.git/config|awk '{for(i=1;i<NF;i++)if($i~"merge")print$(i+2)}'
mybranch

Script has +x flag and on executable partition. Also executable without script but not on script. Why?

2

The problem is in this line:

data=cat $PWD/.git/config

This temporarily sets the shell variable data to have the value cat and then attempts to execute the file $PWD/.git/config. That is unfortunate because you probably didn't want to execute it.

You likely intended:

data=$(cat $PWD/.git/config | awk '{for(i=1;i<NF;i++)if($i~"merge")print$(i+2)}')

The above will run your cat command, sending its output to awk and saving awk's output in the variable data. If this is what you wanted, then the cat is superfluous. Replace the above with:

data=$(awk '{for(i=1;i<NF;i++)if($i~"merge")print$(i+2)}' $PWD/.git/config)

Interpreting the error message

/home001/myaccount/uploader.sh: line 3: /home001/myaccount/mydirectory/.git/config: Permission denied

From the above, we know that uploader.sh was executing. This means that there was no problem with the permissions of uploader.sh. The error occurs on line 3 of uploader.sh which is the command:

data=cat $PWD/.git/config | awk '{for(i=1;i<NF;i++)if($i~"merge")print$(i+2)}'

The Permission denied error occurred while the shell was attempting to execute this line.

| improve this answer | |
  • It works with your replace. I already know from the error msg, problem is on my command at script. But with same command, without script, it works. Is there any difference with/without script running? – Yoon-Geun Kwon Mar 11 '15 at 6:25
  • Generally, if something works without a script, it should also work in a script. Exceptions to this occur when aliases or other interactive features are used: they work on the command line but (by default, anyway) aren't recognized inside scripts. – John1024 Mar 11 '15 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.