简体   繁体   中英

Owner cannot create file in folder with all permission Linux

I'm trying to create few files under folder with script, but not able to. following is the folder privilege under which some files are to be created

su pnaid

The partial script content, responsible to create folder and create files in it

MKDIR_CMD="/bin/mkdir -p \"${PATH_TO_WRITE}\" > /dev/null 2>&1"
"${MKDIR_CMD}"
echo "Checking Dir Path exist"
if [ -d "${PATH_TO_WRITE}" ]; then
   echo "Calling another script to create files under this folder"
   "/createFiles.sh \"${PATH_TO_WRITE}\""
else
   echo "WARNING: unable to create folder"

The parent folder to the $(PATH_TO_WRITE) has following privileges

drwxr-x---.  2 pnaid pnaid  4096 Dec  3 12:31 work_directory

Each time the statement "WARNING: unable to create folder" is displayed.

I tried creating a folder with pnaid user having 777 permission and feeding that in script instead of "${MKDIR_CMD}", in that case the statement "Calling another script to create files under this folder" is displayed but the other script is not able to write to this folder.

Also the echo statements from createFiles.sh when called from original script are not displayed, is there any way to view it.

If we perform the same commands on shell prompt instead of script, the commands work and desired output is obtained; ie folder is created with all the files in it.

Also the same script works if we run it with user root.

I believe this should work across Linux flavors, in this case I'm using CentOS

Please help me resolve this issue and let me know if I have missed mentioning any details.

Thanks

This line:

"${MKDIR_CMD}"

will not work. It treats the entire value of $MKDIR_COMMAND as the name of the program to run, it doesn't split it into the program and arguments, because you put quotes around it. Also, redirections are not processed when expanding a variable.

You need to use eval to re-parse the string:

eval "$MKDIR_CMD"

You have the same problem with:

"/createFiles.sh \"${PATH_TO_WRITE}\""

This should be:

/createFiles.sh "$PATH_TO_WRITE"

These problems don't depend on permissions, I doubt the script really works when run as root.

Here's a related question that shows how to store command parameters best in variables:

Setting an argument with bash

However, the solution there (using an array instead of a string) won't work if you're also storing shell operators like redirection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM