Linux bash executable file

How do I create a script file for terminal commands?

In Windows I can write a file containing commands for cmd (usually .cmd or .bat files). When I click on those files it will open cmd.exe and run them commands the file contains. How would I do this in Ubuntu? I’m sure this is a duplicate, but I can’t find my answer.
Its similar to these questions, but they don’t answer the question: Store frequently used terminal commands in a file CMD.exe Emulator in Ubuntu to run .cmd/.bat file

3 Answers 3

First, the most common is to write a file, make sure the first line is

Then save the file. Next mark it executable using chmod +x file

Then when you click (or run the file from the terminal) the commands will be executed. By convention these files usually have no extension, however you can make them end in .sh or any other way.

  • Any (and I mean any) file can be executed in Linux provided the first line is a path to the program that should interpret the file. Common examples include /bin/python , /bin/sh , /bin/dash , but even odd ball things work like /bin/mysql
  • Bash is a full language. It is vastly more complex than cmd.exe in windows. It has a strong programming language that supports functions, loops, conditionals, string operations, etc.
  • These documents may help if you run into problems.
  • If you do not wish to make the file executable then you can run it by passing it as an argument to bash: bash file/to/run.sh

A Simple Bash Example

#!/bin/bash echo "This is a shell script" ls -lah echo "I am done running ls" SOMEVAR='text stuff' echo "$SOMEVAR" 

The second method is to record commands using script . Run script then just do stuff. When you are done doing stuff type exit and script will generate a file for you with all the «stuff» you did. This is less used but works quite well for making things like macros. man script for more info.

Источник

How to Make a File Executable in Linux

Making bash script files executable is an efficient way to run your bash programs because by default the scripts are not executable. This is useful mainly for system administrators, as they need to develop several bash scripts daily to automate their tasks. For example, you might wish to run bash scripts to back up your work or log certain events on your server. So this article will guide you on how you can make your bash scripts executable, and for that, there are two main ways:

  • Make a bash file executable using the CHMOD command
  • Make a bash file executable using file properties

How to make a file executable using the chmod command

The “chmod” command stands for change mode, and this command can be used to make changes in the file permissions, such as you can make a file writable, readable, and executable. To see these permissions for any file, first, let’s create a bash script file in nano editor:

Читайте также:  Linux узнать пользователя apache

Then we have written some text and saved the file as shown below:

echo ”This is a test file to make it executable.”

Use the below-mentioned command to check whether the file is executable or not:

In the above image, the letter ‘r’ shows that the file is readable, and ‘w’ shows that the file is writeable and ‘x’ shows that the file is executable, which is currently missing on the above output, and that means you cannot execute this file for now. To verify this, you can execute this file by following the general syntax shown below.

As you can see, it shows an error after trying to execute the text file, and this is where the chmod command comes in handy. To make this file executable, all you need to do is to follow the general syntax shown below:

In the above image, ‘u+x’ shows that you are giving permission to the user to execute a specific file, and you can see that it added a letter ‘x’ in the image and after that, you need to run this bash file by typing.

It can be seen that the content of the bash file is now executed without any error.

How to make a file executable using file properties

You can also make a file executable by right-clicking on the bash file and selecting its ‘Properties’ as shown below:

The next step is to select the ‘Permissions’ tab and then check the option of ‘Allow executing file as program’.

This will also allow you to execute the bash file for which you have selected this option, upon double clicking the file, you will get the prompt as shown below:

You can either get output in the terminal or directly run the script for the desired result.

Conclusion

Making a bash script executable allows you to execute your bash scripts without having to type bash in the terminal. This is especially useful for system administrators, who need to write several bash scripts every day to automate their jobs. So in this article, we have taught you how you can make any bash file executable, and there are two ways for doing this. One is by using the “chmod” command, and the other is by using the file properties.

About the author

Taimoor Mohsin

Hi there! I’m an avid writer who loves to help others in finding solutions by writing high-quality content about technology and gaming. In my spare time, I enjoy reading books and watching movies.

Источник

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.

Читайте также:  Remote assistance on linux

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.

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 

Источник

Читайте также:  Smart pss dahua linux

making a bash script executable programatically

I need to create a bash script to create and mount a drive. So, two simple commands. Both «work» when entered at the command line. The script is created and executed every time a normal user logs in, so I need a method to make this script executable at that time. So far, I cannot get this to work. For example, the first part mkdir /vvv/gggg doesn’t proceed because the script is not executable (I’m guessing). Hope this makes sense. Is this possible? Any thoughts on how to make this work will be appreciated. Update: Thanks for your responses. I probably should add some additional information other than that I’m new to Linux. I’m using an open source virtual desktop application called Ulteo. This App runs on top of Ubuntu and has very little support — that why i’m here. Basically, I’m learning by fire. So, there application has a login script management function where I can tie a script to a user. A simple windows script with net use works perfectly fine. However, when I try to apply a Linux bash script nothing happens. I’m thinking that because I need to perform a chmod +x against the script first to make the script executable, this is why its failing. By the way, Ulteo runs in a chroot jail. I’ve created a script, saved it and could not find the script. I searched both inside and outside the chroot jail. I like the approach by dan08 to have the initial script reference another script that i can find and make executable manually. Would I run into the same problem? Does this additional information clarify the situation? Thanks in advance. Photos attached. ![login scipt management console][1] ![windows scripts that work][2] ![simple linux script that doesn’t work][3] Sorry I Can’t post images yet

Are you going to write a cron job to run the script every time a user is logged in? This guide should help you with making them executable too.

«The script is created and executed every time a normal user logs» why? I would fix this since it sounds unlogical to me. I would feed the script the user as a parameter and leave the script untouched after created.

There is a strange mix of tenses in the question that leaves me confused about what currently happens and what you’d like it to do. It would help if you could be a little more specific about what the current situation is.

Please edit your question and show us the script. Also show us the output of ls -l scriptname . You should also clarify how exactly the script is created and why you don’t just create it once and leave it alone. Finally, your normal user shouldn’t have write access to /var and should therefore not be able to create directories there.

Источник

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