Create file with run permission

Asked

Viewed 906 times

3

When I create file .sh, i need to give permission for it to run, is there any file that can be configured or comando applied so that I can already create the file with the permission to run?

  • 1

    The only way I know is chmod +x filename.sh

  • Because, per command line you do not create files, open and then when saving they are created...

  • 2

    @Jorgeb. I believe he wants an automated way to do this

  • I know @Maiconcarraro, only I don’t see any, unless it’s a script to create scripts...

3 answers

3


If you use vim, this can be an automated solution:

Add the next line to your ~/.vimrc:

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif

It will detect whenever there is a file where the first line has "/bin/" or starts with "#!" and runs chmod +x automatically.

1

If this is something common to you, why don’t you create a command for it and save in your Path with their bin? Kind of:

echo -e "#!"`which bash`"\ntouch \$1 && chmod +x \$1" > mkscript && chmod +x mkscript

There whenever you want to create an executable script, just call the command like this:

mkscript <nome_script>

1

Cannot add execution privilege by default when creating files.

The configuration umask (file-Creation mode Mask) system controls the default file and directory permissions. The default value is 0002, which results in the 0664 permissions (666 - 002, fmask - umask) for archives and 0775 (777 - 002, dmask - umask) for directories. And cannot change fmask and dmask.

Source: Making new files Automatically Executable


The solution would be to create a scheduled task using the command crontab -e and inserting the line below at the end of the file

* * * * * find ~/*.sh -type f -exec chmod +x {} \;

The command will run every minute, it locates the . sh files inside your user folder and adds execution permissions.

It may be interesting to run every 5 minutes (or more), according to use demand, so change the start of the line to */5 * * * *

  • I thought this way too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.