views:

1307

answers:

5

Hi, I'm trying to progress from merely dabbling with Linux to actual full-blown use, but am still very much a novice, so please excuse this extremely basic question. One thing I plan to be doing is writing (painfully simple) Perl scripts, and I'd like to be able to run them without explicitly calling Perl from the terminal. I appreciate that, to do this, I need to grant them execute permissions. Doing this with chmod is easy enough, but it also seems like a slightly laborious extra step. What I would like is one of two things:

Firstly, is there a way to set the execute flag when saving a file? Currently I'm experimenting with gedit and geany, but would be willing to switch to a similarly- (or better-) featured editor if it had this capability.

Failing that, is there a way to declare that all files created in a particular directory should have execute permissions?

My umask is set to 022, which should be OK, as far as I understand, but it would appear that the files are created as text files (with 666 default permissions) rather than executable files (with 777 default permissions).

Again, sorry if this is achingly obvious, but if it is I've been looking in the wrong places. Perhaps I'm just being lazy, but I figure there must be a more convenient way than chmodding every single script one creates.

Thanks in advance for any advice you can give me.

A: 

It's really not that big of a deal. You could just make a script with the single command:

chmod a+x *.pl

And run the script after creating a perl file. Alternatively, you could open a file with a command like this:

touch filename.pl && chmod a+x filename.pl && vi filename.pl # choose your favorite editor
BobbyShaftoe
I was more looking for a way to do everything into the GUI, but perhaps this isn't possible. Is it a security thing?
JamesM
yes, it's a security thing. It could be a huge problem if the shell started creating everything executable by default.
JimB
You could make a simple program that does this for you. But honestly, you'd be trying to do something no one else who programs in a UNIX environment really does.
BobbyShaftoe
+3  A: 

Make file executable:

chmod +x file

Find location of perl:

which perl

This should return something like

/bin/perl sometimes /usr/local/bin

Then in the first line of your script add:

#!"path"/perl with path from above e.g.

#!/bin/perl

Then you can execute the file

./file

There may be some issues with the PATH, so you may want to change that as well ...

Thanks, but that's not quite my problem. I've already gotten into the habit of starting my scripts with #!/usr/bin/perl. I can run scripts fine once I've given them executable permissions; I was just looking for a simpler way of doing so.
JamesM
May I recommend `#!/usr/bin/env perl` instead? The `env` program basically finds the argument given (in this case, "perl") on the PATH and runs that. It's useful when you're sending scripts to other people - for example, I use a Mac, and Perl is located in /opt/local/bin.
Samir Talwar
A: 

What you describe is the correct way to handle this.

You said that you want to stay in the GUI. You can usually set the execute bit through the file properties menu. You could also learn how to create a custom action for the context menu to do this for you if you're so inclined. This depends on your desktop environment of course.

If you use a more advanced editor, you can script the action to happen when the file is saved. For example (I'm only really familiar with vim), you could add this to your .vimrc to make any new file that starts with "#!/*/bin/*" executable.

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

I think the problem you're running into is that, even though you can set your own umask values in the system, this does not allow you to explicitly control the default permissions set on a new file by gedit (or whatever editor you use).

I believe this detail is hard-coded into gedit and most other editors. Your options for changing it are (a) hacking up your own mod of gedit or (b) finding a text editor that allows you to set a preference for default permissions on new files. (Sorry, I know of none.)

In light of this, it's really not so bad to have to chmod your files, right?

grossvogel
+1  A: 

No need to hack your editor, or switch editors.

Instead we can come up with a script to watch your development directories and chmod files as they're created. This is what I've done in the attached bash script. You probably want to read through the comments and edit the 'config' section as fits your needs, then I would suggest putting it in your $HOME/bin/ directory and adding its execution to your $HOME/.login or similar file. Or you can just run it from the terminal.

This script does require inotifywait, which comes in the inotify-tools package on Ubuntu,

sudo apt-get install inotify-tools

Suggestions/edits/improvements are welcome.

#!/usr/bin/env bash

# --- usage --- #
# Depends: 'inotifywait' available in inotify-tools on Ubuntu
# 
# Edit the 'config' section below to reflect your working directory, WORK_DIR,
# and your watched directories, WATCH_DIR. Each directory in WATCH_DIR will
# be logged by inotify and this script will 'chmod +x' any new files created
# therein. If SUBDIRS is 'TRUE' this script will watch WATCH_DIRS recursively.
# I recommend adding this script to your $HOME/.login or similar to have it
# run whenever you log into a shell, eg 'echo "watchdirs.sh &" >> ~/.login'.
# This script will only allow one instance of itself to run at a time.

# --- config --- #

WORK_DIR="$HOME/path/to/devel" # top working directory (for cleanliness?)
WATCH_DIRS=" \
    $WORK_DIR/dirA \
    $WORK_DIR/dirC \
    "                          # list of directories to watch
SUBDIRS="TRUE"                 # watch subdirectories too
NOTIFY_ARGS="-e create -q"     # watch for create events, non-verbose


# --- script starts here --- #
# probably don't need to edit beyond this point

# kill all previous instances of myself
SCRIPT="bash.*`basename $0`"
MATCHES=`ps ax | egrep $SCRIPT | grep -v grep | awk '{print $1}' | grep -v $$`
kill $MATCHES >& /dev/null

# set recursive notifications (for subdirectories)
if [ "$SUBDIRS" = "TRUE" ] ; then
    RECURSE="-r"
else
    RECURSE=""
fi

while true ; do
    # grab an event
    EVENT=`inotifywait $RECURSE $NOTIFY_ARGS $WATCH_DIRS`

    # parse the event into DIR, TAGS, FILE
    OLDIFS=$IFS ; IFS=" " ; set -- $EVENT
    E_DIR=$1
    E_TAGS=$2
    E_FILE=$3
    IFS=$OLDIFS

    # skip if it's not a file event or already executable (unlikely)
    if [ ! -f "$E_DIR$E_FILE" ] || [ -x "$E_DIR$E_FILE" ] ; then
     continue
    fi

    # set file executable
    chmod +x $E_DIR$E_FILE
done
Dylan