Execute files in linux terminal

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can’t find any option similar to Right ClickOpen WithOther Application.Terminal. How do I open this file in the terminal?

You shouldn’t use extensions on scripts. At some point in the future, you may find that a different language is more suitable to do the task your current script is doing. And then you have a problem. Do you keep the old name, with a completely misleading extension, or do you rename it, possibly having to edit alot of places where your script is used?

You don’t need the file extension. It’s nice to have but is not needed. The OS doesn’t look at the file extension. It looks at the data

16 Answers 16

Give execute permission to your script:

chmod +x /path/to/yourscript.sh 

Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:

Actually, you can use . /path/to/yourscript.sh if the script have to set up some environment variables.

Nobody mentions the traditional: ./path/to/yourscript.sh (without the space after . )? I find that one is the simplest and easiest to use. But anyways, here is my alternative that should do almost the same as ./ would, though I don’t see why you wouldn’t use ./ : (FILENAME=~/rem4space.sh;SLL=$(cat $FILENAME|head -1|sed ‘s:^#!\(.*\):\1:g’);[ ! -z $SLL ] && exec $SLL $FILENAME;sh $FILENAME) . edit FILENAME to your liking. Also note that sh will be used if there is no alternative.

You need to mark shell scripts as executable to run them from the file manager:

  1. Right click on your .sh file and select Properties: enter image description here
  2. In the Permissions tab, check Allow executing file as program: enter image description here
  3. Close the Properties window and double-click the file. A dialog will pop up giving you the option to run the script in a terminal: enter image description here

This isn’t working in Ubuntu 13.04. Keeps opening in gedit anyway, never asks me to execute. Edit: Nvm, imjustmatthew answers this.

Before using this we need to make the file permission for execute using chmod. chmod +x filename.sh or chmod 755 filename.sh

Источник

How to Execute Files in Linux

Executing files isn’t as simple in Linux as it is in Windows. Many files that are designed to run in Linux aren’t naturally executable, because they don’t have the right permissions. And some files must be executed in the terminal with special commands. If you’re receiving an error that your file isn’t executable, these tips may help.

Читайте также:  Линукс кто использует порт

Mark the File as Executable

Marking a file as executable sometimes requires that you change its permissions. Here, you can use the chmod command. For example:

Run the File from the Terminal

Certain types of executable files, such as shell scripts, must be executed in a different way than other types of files. You must change into the directory where the file is stored to do this properly. For example:

This will run the autogen.sh program, if it has been marked as executable.

Change Execute Permissions from the GUI

You can mark some files as executable within the GUI. Start your favorite file manager, then navigate to the desired program. Right-click the program, then select the “Permissions” tab.

Under Execute, change the permissions to the desired value, such as Only Owner, Only Owner and Group, or Anyone.

change_file_properties_executable

Run the File from the GUI

Some executable files will run if you click on them in the GUI. Simply open your file manager and double click on the file.

If you can’t find the executable file that you’re looking for, it may be hidden, or in a hidden folder. Your file manager probably has a menu item or shortcut to show hidden. In my file manager, PCManFM, the Show Hidden option is in the View Menu.

Источник

How to Execute .RUN Files in Linux

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time.

This article has been viewed 54,593 times.

.RUN files usually contain program data and installation instructions for Linux programs. This wikiHow teaches you how to execute .RUN files in Linux using the Ubuntu terminal. Since RUN files install software, make sure it’s legitimate and won’t harm your computer before executing it. RUN files from suspicious links can contain malware and damage your computer.

Image titled Execute .RUN Files in Linux Step 1

Press Ctrl + Alt + T to open a Terminal window and navigate to the folder where your .RUN file is. You can also search for Terminal in the «dash» bar on the left side of your screen by clicking the «All Applications» icon.

Image titled Execute .RUN Files in Linux Step 2

Image titled Execute .RUN Files in Linux Step 3

  • If you get a «Permission denied» error during this process, add » sudo » to the beginning of the code so it will run with the appropriate permissions. [1] X Research source

Expert Q&A

Tips

You Might Also Like

Install Google Chrome Using Terminal on Linux

Can Linux Run Exe

Can Linux Run .exe Files? How to Run Windows Software on Linux

Add or Change the Default Gateway in Linux

Open Ports in Linux Server Firewall

How to Open Linux Firewall Ports: Ubuntu, Debian, & More

Take a Screenshot in Linux

Become Root in Linux

Execute INSTALL.sh Files in Linux Using Terminal

How to Run an INSTALL.sh Script on Linux in 4 Easy Steps

Ping in Linux

Use Ping in Linux: Tutorial, Examples, & Interpreting Results

Boot Linux from a USB on Windows 10

Delete Read Only Files in Linux

How to Delete Read-Only Files in Linux

Use Wine on Linux

Run Files in Linux

Install Linux

How to Install Linux on Your Computer

Install Puppy Linux

How to Install Puppy Linux

Читайте также:  Suse linux enterprise server sap applications

Источник

How do I execute a bash script in Terminal?

Yet another way to execute it (this time without setting execute permissions):

had a seperate issue (trying to install anaconda via terminal on mac) and this was the solution. only thing I had to do was close and restart the shell, follow the one step here ( /(your conda installation path)/bin/conda init zsh ) and then close and reopen the shell one more time

$prompt: /path/to/script and hit enter. Note you need to make sure the script has execute permissions.

If you already are in the /path/to directory, e.g. with the cd /path/to command, you can enter ./script to run your script.Don’t forget, in this case the ‘./’ before ‘script’

cd to the directory that contains the script, or put it in a bin folder that is in your $PATH

if in the same directory or

./scriptname.sh works for me but scriptname.sh gives scriptname.sh: command not found . -rwxr-xr-x are its permissions.

The advice to cd anywhere at all perpetrates another common beginner misunderstanding. Unless the script internally has dependencies which require it to run in a particular directory (like, needing to read a data file which the script inexplicably doesn’t provide an option to point to) you should never need to cd anywhere to run it, and very often will not want to.

This is an old thread, but I happened across it and I’m surprised nobody has put up a complete answer yet. So here goes.

The Executing a Command Line Script Tutorial!

Q: How do I execute this in Terminal?

The answer is below, but first . if you are asking this question, here are a few other tidbits to help you on your way:

Confusions and Conflicts:

The Path

  • Understanding The Path (added by tripleee for completeness) is important. The «path» sounds like a Zen-like hacker koan or something, but it is simply a list of directories (folders) that are searched automatically when an unknown command is typed in at the command prompt. Some commands, like ls may be built-in’s, but most commands are actually separate small programs. (This is where the «Zen of Unix» comes in . «(i) Make each program do one thing well.»)

Extensions

  • Unlike the old DOS command prompts that a lot of people remember, you do not need an ‘extension’ (like .sh or .py or anything else), but it helps to keep track of things. It is really only there for humans to use as a reference and most command lines and programs will not care in the least. It won’t hurt. If the script name contains an extension, however, you must use it. It is part of the filename.

Changing directories

  • You do not need to be in any certain directory at all for any reason. But if the directory is not on the path (type echo $PATH to see), then you must include it. If you want to run a script from the current directory, use ./ before it. This ./ thing means ‘here in the current directory.’

Typing the program name

  • You do not need to type out the name of the program that runs the file (BASH or Python or whatever) unless you want to. It won’t hurt, but there are a few times when you may get slightly different results.
Читайте также:  Extract exe file in linux

SUDO

  • You do not need sudo to do any of this. This command is reserved for running commands as another user or a ‘root’ (administrator) user. Running scripts with sudo allows much greater danger of screwing things up. So if you don’t know the exact reason for using sudo , don’t use it. Great post here.

Script location .

# A good place to put your scripts is in your ~/bin folder. > cd ~/bin # or cd $HOME/bin > ls -l 

You will see a listing with owners and permissions. You will notice that you ‘own’ all of the files in this directory. You have full control over this directory and nobody else can easily modify it.

If it does not exist, you can create one:

> mkdir -p ~/bin && cd ~/bin > pwd /Users/Userxxxx/bin 

A: To «execute this script» from the terminal on a Unix/Linux type system, you have to do three things:

1. Tell the system the location of the script. (pick one)

# type the name of the script with the full path > /path/to/script.sh # execute the script from the directory it is in > ./script.sh # place the script in a directory that is on the PATH > script.sh # . to see the list of directories in the path, use: > echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # . or for a list that is easier to read: > echo -e $ # or > printf "%b" "$" /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin 

2. Tell the system that the script has permission to execute. (pick one)

# set the 'execute' permissions on the script > chmod +x /path/to/script.sh # using specific permissions instead # FYI, this makes these scripts inaccessible by ANYONE but an administrator > chmod 700 /path/to/script.sh # set all files in your script directory to execute permissions > chmod +x ~/bin/* 

There is a great discussion of permissions with a cool chart here.

3. Tell the system the type of script. (pick one)

  • Type the name of the program before the script. (Note: when using this method, the execute(chmod thing above) is not required
> bash /path/to/script.sh . > php /path/to/script.php . > python3 /path/to/script.py . 
  • Use a shebang, which I see you have ( #!/bin/bash ) in your example. If you have that as the first line of your script, the system will use that program to execute the script. No need for typing programs or using extensions.
  • Use a «portable» shebang. You can also have the system choose the version of the program that is first in the PATH by using #!/usr/bin/env followed by the program name (e.g. #!/usr/bin/env bash or #!/usr/bin/env python3 ). There are pros and cons as thoroughly discussed here.

Note: This «portable» shebang may not be as portable as it seems. As with anything over 50 years old and steeped in numerous options that never work out quite the way you expect them . there is a heated debate. The most recent one I saw that is actually quite different from most ideas is the «portable» perl-bang:

#!/bin/sh exec perl -x "$0" "$@" #!perl 

Источник

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