0

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; i.e. 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

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try to change to use eval, the script does run perfectly fine with root user. thanks
@Pixy: This is not related to the user, because it is simply an error in the script, but in any case, I wonder what's the point in putting the whole mkdit into a variable.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.