avatar
run sh file linux

Guest 96 18th Aug, 2019

MARKUP 5.30 KB
                                           
                         Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can't find any option similar to Right Click → Open With → Other Application... → Terminal.

How do I open this file in the terminal?



Give execute permission to your script:

chmod +x /path/to/yourscript.sh

And to run your script:

/path/to/yourscript.sh

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

./yourscript.sh





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

    Right click on your .sh file and select Properties:

    enter image description here

    In the Permissions tab, check Allow executing file as program:

    enter image description here

    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




Open a terminal and navigate to the folder where the .sh file is located. Then type:

sh <name of file>.sh


Prerequisite

Before you can run the .sh file, you need to make it executable:

    Right-click on the file
    Select Properties
    Select Permissions
    Select Allow executing file as a program

Warning

Make sure you trust the source where you got the file from. It could be a virus.
The very simple way

    Double-click on the file
    Click run in terminal

This has problem. The terminal will close immediately and you will not be able to see the output.
The simple way

    Open Applications -> Accessories -> Terminal
    Drag and drop the .sh file into the terminal and press Enter

The way professionals do it

    Open Applications -> Accessories -> Terminal

    Find where the .sh file
        Use the ls and cd commands
        ls will list the files and folders in the current folder. Give it a try: type "ls" and press Enter.
        Once you see the folder that you want to go in to, run cd, followed by a space, followed by a folder name
        If you when into a folder that you did not want, run cd .. to go one level up

    Run the .sh file

        Once you can see for example script1.sh with ls run this:

        ./script.sh

Why do it the complicated way?

The terminal has a rich set of powerful tools that are accessible by typing the commands. Professionals locate the .sh file by typing ls and cd. Once you are in the correct current folder you can run the script like this:

./script1.sh

or you can run and redirect the output to a file:

./script1.sh > out.txt

or you can filter the output for keywords (e.g. "apples") an then redirect to a file:

./script1.sh | grep apples > ./only-apples

There are thousands of things you can to to that file just by typing a few commands.

Another one, you can download a file from the Internet with one simple command:

wget www.google.com/images/logos/ps_logo2.png

And then open the file like this:

shotwell ps_logo2.png




On Ubuntu 13.04 executable files opened in Nautilus are now opened in gedit by default rather than prompting the user to execute them. To enable the classic behavior you need to adjust the preferences:

Nautilus → Edit menu → Preferences → Behaviour tab → Click the radio button near Ask each time.


Go to the directory where the .sh file is by using cd. In this example I have stored my sh file as ~/Desktop/shell_practice/test.sh

first do pwd to figure out where you are, and if it returns /home/username (where username is your real username), you can run

cd Desktop/shell/practice

If you seem to be somewhere else, you can use the absolute path

cd ~/Desktop/shell/practice

or

cd $HOME/Desktop/shell/practice

or even

cd /home/$USER/Desktop/shell/practice

these are all ways of describing the same place. Once you've made it to the location of your script, type

ls

If you can see the sh file in the output, you can use chmod to make it executable. In my case, remember, the filename is test.sh, so I would run

chmod u+x test.sh

Now that we are in the same directory as the script, we have to specify to the shell that we want to execute the file by giving its location ./ (the current directory followed by a path separator, to distinguish it from the filename). To run my file I would type:

./test.sh

If your script has been written correctly it will run without errors...




The problem I have found on a few distributions is they have hidden the preferences option in Nautilus, but to fix it in Ubuntu and other distributions using Gnome3 is the same (literally just done the Fedora version of this and posting the actual fix to remind me how in the future).

    Install dconf-editor

    sudo apt-get install dconf-editor

    Run dconf-editor using the user account you want this on, i.e NOT root

    dconf-editor

    Navigate to the following schema:

    org.gnome.nautilus.preferences

    Change the default option to not open by default:

    Find executable-text-activation click the word display and change to ask

that will give you the option to edit, view or run the file going forward
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes