Make scripts executable linux

Creating executable files in Linux

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). Perhaps I’m just being lazy, but I figure there must be a more convenient way than chmodding every single script one creates.

You have to print the output of every file. You have to import the right libraries of every file. This seems like another step in the process of programming, and one that is dangerous to circumvent.

5 Answers 5

This should return something like

Then in the first line of your script add:

Then you can execute the 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.

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.

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.

Читайте также:  Раздача интернета локальной сети linux

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 '' | 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 

Источник

How to Make a File Executable in Linux terminal?

New to Linux command line and wondering how to make a bash script or some other file executable? Here’s how to do it.

Each file that is in a POSIX-compatible file system (Ext4, Btrfs, XFS, JFS etc) has «mode bits» assigned to itself.

To make a file executable in Linux, the executable mode bit needs to be enabled.

To set the executable mode bit, the chmod command is used like this:

With that, you can execute said file from the terminal:

That was the quick summary. Let’s see things a bit in detail.

Make a file executable in Linux

There are several ways you can make a file executable in Linux. The most common methods involve using the chmod command, in different ways.

To check if you can execute a file, use the -l flag with ls command.

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

In the first column, -rw-r—r— represents that the owner can read and write, users from the group can only read and everyone else can only read the file.

Note the lack of x from the permission symbols. This means that lhb.txt is not executable.

I highly recommend reading about Linux file permissions and brushing up your basics to better understand these commands.

Читайте также:  Oracle linux поддержка до какого года

Method 1: Make file executable for everyone

The first method, and the most straightforward one, is to make a file executable using the following command:

The x flag is to set or unset the executable permission of a file. And using + sign before x means we want to set it as an executable file.

This will make the file executable for the owner, group and everyone else. Anyone on the system, will be able to execute it as well.

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod +x lhb.txt $ ls -l lhb.txt -rwxr-xr-x 1 pratham staff 0 Mar 10 20:49 lhb.txt

As you can see now, everyone, the owner, group and others have the executable x bit set. This means everyone can execute this file now.

Method 2: Make file executable only for certain user or group

If you do not want to make everyone able to execute a file, you should specify classes before adding/removing file permissions.

The available classes are:

  • u : permissions for owner/user
  • g : permissions for group
  • o : permissions for others
  • a : permissions for everyone

When you specify a class, the syntax is mostly similar to the one you saw in the first method.

This means, if I want to make the file executable in a manner such that only the owner of the file can execute it, and no one else, then I should use the following command:

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod u+x lhb.txt $ ls -l lhb.txt -rwxr--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

Similarly, to make it executable for everyone in the group owning that file, I should use the following command:

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod g+x lhb.txt $ ls -l lhb.txt -rw-r-xr-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

When you specify the a class, it is almost as if you did not specify any. Let’s have a look.

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod +x lhb.txt $ ls -l lhb.txt -rwxr-xr-x 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod 644 lhb.txt #reset permissions $ chmod a+x lhb.txt $ ls -l lhb.txt -rwxr-xr-x 1 pratham staff 0 Mar 10 20:49 lhb.txt

These commands might appear differently, but they have the same outcome.

Method 3: Use the octal numbers

If you follow the first method, you only enable the executable flag of a file by doing a +x , making it such that anyone can execute it.

But, sometimes you might not want everyone to execute a file, maybe it should need some privileges. In that case, you can specify the full set of permissions.

That is usually done by either providing an octal value or a symbolic value to enable file permissions.

This allows you to have a more granular control over how the flags are set. Below is the syntax for using octal values with chmod command:

If you are not good with numbers, you can use an online chmod calculator, which helps you generate correct octal values and thus avoid using the wrong value and messing up file permissions.

Читайте также:  Linux перейти в консольный режим

Say, I want to make the file executable only for the owner and group, I need to know its current permissions first, and add the executable bits to it on top of it using [chmod-calculator].

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod 754 lhb.txt $ ls -l lhb.txt -rwxr-xr-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

As you can see, only the owner and group can execute the file.

Conclusion

This article covers making a file executable in a Linux system. It mainly focused on the different ways to use the chmod command. If you want to know more about it, we have a few examples on how to use the chmod command.

Источник

How to Make Bash Script Executable Using Chmod

chmod executable

In this tutorial, I am going through the steps to create a bash script and make the script executable using the chmod command. After that, you will be able to run it without using the sh or bash commands.

Step 1: Creating a Bash File

The first step is to create a new text file with .sh extension using the following command.

Step 2: Writing a Sample Script

Open the newly created file using any of your favorite editors to add the following bash script to the file.

$ vim hello_script.sh #!/bin/bash echo "Hello World"

Save and close the file using ESC +:wq!

Step 3: Executing the Bash Script

There are two ways to run the bash file. The first one is by using the bash command and the other is by setting the execute permission to bash file.

Let’s run the following command to execute the bash script using bash or sh command.

Execute the bash script

Step 4: Set Executable Permissions to Script

The second way to execute a bash script is by setting up the executable permissions.

To make a script executable for the owner of the file, use u+x filename.

To make the file executable for all users use +x filename or a+x filename.

Step 5: Running Executable Script

After you have assigned the executable permissions to the script, you can run the script without bash command as shown.

Running a executable script

Another Example

In the following example, I am going to write and execute a bash script to take a backup from source to destination.

$ vim backup_script.sh #!/bin/bash TIME=`date +%b-%d-%y` DESTINATION=/home/ubuntu/backup-$BACKUPTIME.tar.gz SOURCE=/data_folder tar -cpzf $DESTINATION $SOURCE

Save and close the file using :wq! and give it the executable permissions for all users using the following command:

bash example script and execute

Conclusion

At the end of this tutorial, you should be familiar with how to set a script executable in Linux.

I hope you enjoyed reading it, please leave your suggestion in the below command section.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector