-3

I am writing a script that needs to create a file if an argument is passed to he script. If no argument is passed then it will ask for fileName but it needs to have default permission as -rwx- --- - ---. I am using this command chmod a=-rwx to remove all the default permissions and then i am using chmod u=+rwx to get the desired permission as stated but it is not working. Can anyone help please?

#!bin\bash

if [ $#==0 ]; then
    echo "Please enter a file name?"
    read fileName
    if [ -f $fileName ]; then
        echo "File already exist! Opening for Editing"
        sleep 3
        nano $fileName
    else
        echo "File created with the name $fileName"
        echo "Opening $fileName for editing "
        sleep 3
        echo "#!bin\bash" >$fileName
        nano $fileName
    fi
elif [ -f $1 ]; then
    echo "File already exists with the name $1"
    echo "Opening for editing"
    sleep 3
    nano $1
else
    fileName="$1"
    chmod a=-rwx $fileName
    chmod u=+rwx $fileName
    echo "File created with the name $filename"
    echo "Opening $fileName for editing "
    echo "#!bin\bash" >$fileName
    sleep 3
    nano $1

fi
9
  • 1
    You are required to post a minimal reproducible example here, within your question, and not a link to any other site.
    – Rob
    Commented Jun 10, 2021 at 15:19
  • Check your script with Shell Check to catch various basic syntax errors. Commented Jun 10, 2021 at 15:20
  • 1
    "is not working" isn't helpful. Are you getting an error message? what is it doing? c.f. this guide to asking a question well. Commented Jun 10, 2021 at 15:20
  • 1
    Show the before, the after, and the output. Use set -x. Commented Jun 10, 2021 at 15:22
  • 1
    It's not =+ and =-, it's either, =, + or -.
    – Barmar
    Commented Jun 10, 2021 at 15:32

2 Answers 2

2

Your chmod syntax is incorrect. The operation is either = to set the mode to a specific value, + to add modes, or - to remove modes; you can't combine them with =+ and =-.

You can perform multiple operations on a single file by separating them with ,.

So it should be:

chmod go-rwx,u+rwx "$fileName"

Another problem:

if [ $#==0 ]

should be

if [ $# -eq 0 ]

Use -eq for numeric comparisons, and spaces are needed around operators in shell conditions.

Third problem: You're doing the chmod before you create the file. Put it after

echo "#!/bin/bash" >"$fileName"

Fourth problem: #!bin\bash should be #!/bin/bash.

Finally, remember to quote your variables, in case they contain spaces.

8
  • Yes you are right it works but from ubuntu terminal not inside from script If i use from terminal it changes permission but doesn't affect from shell script and that's probably because of opening nano text editor from this sript
    – user14196724
    Commented Jun 10, 2021 at 15:53
  • You're opening nano after you change the permissions.
    – Barmar
    Commented Jun 10, 2021 at 15:53
  • You're only doing the chmod in the case where the file doesn't exist, and you're doing it before you create the file. How is that supposed to work?
    – Barmar
    Commented Jun 10, 2021 at 15:56
  • Then should i use touch command to first create the file and then apply these permissions?
    – user14196724
    Commented Jun 10, 2021 at 15:58
  • The echo command will create the file, you just have to do the chmod after that.
    – Barmar
    Commented Jun 10, 2021 at 15:58
1

Add the actual path of chmod: Ex: which chmod

/usr/bin/chmod

In your script add the complete path of chmod and try it out.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 4, 2024 at 14:09

Your Answer

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