Operating System - HP-UX
1751849 Members
5311 Online
108782 Solutions
New Discussion юеВ

Re: How to make scripts executable automatically?

 
SOLVED
Go to solution
Allanm
Super Advisor

How to make scripts executable automatically?


I want to make any scripts that I create executable by default, how can I achieve that?

By scripts I mean .sh, .bash & .pl.

Thanks,
Allan.
7 REPLIES 7
Matti_Kurkela
Honored Contributor
Solution

Re: How to make scripts executable automatically?

To be "executable" in Unix, a script should have three things:

a) it should have a valid interpreter specified in the first line, e.g. "#!/bin/sh", "#!/usr/bin/perl" or whatever.

b) it should be placed in a directory that is listed in your PATH

(You can add the current directory "." to your path if you really want; but if you feel you must do it, please place it as the _last_ element in your PATH. Don't do it at all if you are root, or the next entry in some "Unix sysadmin horror stories" thread might be yours.)

c) it should have the execute permission bit set: chmod a+x
I guess your problem is most likely c).

Create a script, shell function or alias that will do two things:
1.) invoke your favorite editor to create/edit your script(s),
2.) after the editor process ends, runs chmod a+x on the same file.

Use this script to edit and create scripts instead of your regular editor command.

For a trivial example:

---begin script "/usr/local/bin/es"---
#!/bin/sh
${EDITOR:-vi} "$@"
chmod a+x "$@"
---end script---

es = Edit Script.
Set the environment variable EDITOR to your favorite editor. If the variable is not set, "vi" is used, as is the common convention in Unix systems.

Example:
es somescript.sh

If you use a "programmable" editor (like Emacs for example), it might even be possible to make the editor detect when you're writing a script and automatically set the execute permission on the script.

MK
MK
James R. Ferguson
Acclaimed Contributor

Re: How to make scripts executable automatically?

Hi Allan:

As I understand your question, you can't, not directly. The permissions (modes) of a file are set at the file's 'open()' by supplying an octal 'mode'. For files, this mode is generally, 0666 as set by the shell (and most programs). For directories, a mode of 0777 is used.

Thus, any file created by the shell is going to have derive its permissions from 0666 "and-ed" with the process's umask. Thus, even with a 'umask' of 000, your file will still not have execute permissions.

Regards!

...JRF...
Allanm
Super Advisor

Re: How to make scripts executable automatically?

Thanks MK...

one small thing , can you explain "${EDITOR:-vi}" ?


Thanks,
Allan,
James R. Ferguson
Acclaimed Contributor

Re: How to make scripts executable automatically?

Hi Allan:

> one small thing , can you explain "${EDITOR:-vi}" ?

See your friendly manpages for 'sh-posix':

${parameter:-word}

If parameter is set and is nonnull, substitute its value; otherwise, substitute word.

Thus, set your EDITOR to 'vi' unless its already set to something else.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to make scripts executable automatically?

>I want to make any scripts that I create executable by default?

Why would you want that? How would vi tell if it is a script?

You can always use chmod after you use vi. Or you can use:
:!chmod a+x %
after you write the file.
Pradosh_1
New Member

Re: How to make scripts executable automatically?

Hi Allan,

There is a better way to do, what you are looking for, if you are using vi editor.

You can make use of 'autocmd' of vi.

Say, if you are going to create an expect script, with .exp extension, you need to follow the following steps.

1. Add a file say, ~/exp_header.txt
2. Add following lines in ~/exp_header.txt
:insert
#!/usr/bin/expect -f
[You can add additional header, e.g., Author, Date, Description etc. here]
3. Add following lines in your ~/.exrc file,
autocmd bufnewfile *.exp so ~/exp_header.txt
autocmd BufwritePost *.exp execute "!chmod +x " .expand("%")

So, whenever you are creating a new .exp file, vi will add "#!/usr/bin/expect -f" on the first line of the file. And when you are saving the file vi will change the file mode to executable.

Now you can use ./abc.exp

Thanks,
Pradosh
Dennis Handly
Acclaimed Contributor

Re: How to make scripts executable automatically?

>Pradosh: You can make use of autocmd of vi.

This is for vim, not vi.