How to execute sh files

How to execute sh files

How to execute sh files

It’s rather copy to Desktop folder and make it executable than install.
.sh is shell script. It doesn’t need to be «installed».

Step 3 is wrong. with cd

Anyway, you can put sh script anywhere, where you are able to write. There is no need to have it on the Desktop.

@IcyFlame It’s up to you if you want to run your *.sh file outside the terminal.

I updated my post.

Mint updated Firefox to the latest version (12), but failed to change its language from English to Spanish, as all other apps and desktop items did flawlessly.

After 20 minutes of fruitlessly searching some terminal command that would force Firefox to acknowledge the language change, I opted to manually download the darn thing and now I’m faced with the following:

Setup:
Firefox 12 for Linux, downloaded from www.firefox.com
Linux Mint 12 64-bit, using GNOME Classic / MATE

Thanks in advance!

NOTE: Everybody is politely asked to please refrain from replying with any of the customary, albeit totally useless, Linux replies:
— «Why not use instead X app?» (because I want THIS app. Period.)
— Presenting another Distro’s (or version’s) positive outcome. Pointless.
— Flaming comments (they will be prosecuted with no exceptions)
— Redirecting to an external link (I too used google, so no thanks.)
— Redirect to use man pages (see previous.)
— Try to showoff by asking for a cryptic system log output. 99% of the time they never produce a *usable* follow-up answer. It’s a fact.

It is really weird that I didn’t have to do this when I was using openSUSE, but I had to do this with the LMDE. But, Mint is a much better OS than SUSE, so I am not complaining.

How To Run Shell Script (.sh File) In Linux?

The Bash and Bash Shell is the main management component for the Linux systems. Commands run on bash in order to complete tasks or jobs. These commands can be stored in a file which is called a shell script. The shell script is used to run single or more commands by just calling the shell script file without writing commands again and again. The shell scripts have the *.sh extension for the Bash or Sh. In this tutorial, we will learn how to run shell script files in different ways.

Create Shell Script or Script File

Make Shell Script Executable and Run

The most command way to execute or run a shell script is making it executable and then call. The shell file is just a file with some text by default. It can be made executable by the shell by using the chmod command. In the following example we make the systeminfo.sh shell script file executeable for the current user.

We can check if the shell script file is executable with the ls command like below.

The output is like below where the user has the execute permission which is depicted with the x letter.

Now we can run or execute the script file directly by calling with its name and path. If the script file is in the current working directory it can be called like below.

Alternatively if the shell script if is another directory then the current working directory we can specify the full or absolute path to execute it. This is the more reliable way where the script file can be executed properly for every situation.

Run Shell Script Directly with bash Command

Alternatively the full path of the shell script file can be specified like below.

Run Shell Script As Root

By default, the shell script is executed as the current user with the current user privileges. But in some cases, we may need to run the shell script file as root or with root privileges. We can use the sudo command during execution of the shell script file.

With the absolute path of the shell script file the sudo command can be merged like below.

Run Shell Script via GUI (Desktop Environment)

Shell script files can be also run via the GUI or Desktop environment. But first it should be enabled for the execution. Right click on the script file and click Properties like below.

In the Properties windows navigate to the Permissions tab and check the Execute configuration. This allow executing file as program.

Debug Shell Script While Running

The shell script file is like an application that executed different commands. The shell script can be debugged in order to detect bugs or errors. Also, the shell script file can be debugged to get detailed information about the execution of the shell script. The -x parameter is used with the bash command to debug the shell script file.

What are SH Files and How to Execute Them?

Script files contain commands that are executed by a certain program or scripting engine. These commands are executed without being compiled. Instructions are written in scripting languages for run time environment. There are many scripting languages with different purposes and environments. However, the one we are going to discuss in this article is a bash scripting language that is used to work in Linux. The files containing the commands or syntax of bash scripting language are also known as SH files or Shell Script files.

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh filesWhat are SH Files and How to Execute Them?

What are SH Files in Linux?

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh filesSH Files in Linux

How to Create an SH File?

Just like other coded files created in the simple text editor, this can be done the same. All you need to know is the commands and syntax for the language to create one. However, the most important thing for the bash shell script file is that it starts with a shebang that is shown below:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh filesShebang in a script file

This will be added to the first line of code to make a bash script executable. You can also check the location of bash interpreter by the following command:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh filesWhich bash command in Terminal

Also, we need to use another command so that the system will recognize permission for this as an executable file. However, this might not be required in some cases:

Note: Filename can be any name that you give to your file.

Executing SH Files through Terminal in Linux

You can execute SH files if text commands are typed within the Terminal. The syntax of code in your SH file must be correct before executing it. We are just using sample code to demonstrate; how the script file works. You can have a different code that you are working on.

How to Execute SH Files on Windows

Bash shell script files are written in bash scripting language for Linux. It contains commands that you can normally run on the command line. These files can be called in Terminal to execute and can be opened by double-clicking. However, Windows does not know what an SH file is and how to execute it. If you are looking for a way to execute SH file in Windows then this article will provide you with all possible methods.

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh filesExecute SH files on Windows

What are the SH Files?

SH files (also known as script files) are the scripts that the Bash application programs and use. Bash language is used to write the instructions in these files. Program developers mostly use these files. Since they contain the commands to execute programs, they are indeed important. However, these files are for Linux, so executing them on Windows will require some software or environment that we will provide in methods of this article.

For further information about SH files, you can check our article here. Moving on towards methods for executing SH files on Windows.

Method 1: Using Cygwin to Execute SH Files

Cygwin is an open source command line interface environment similar to Unix. It allows Unix or Linux applications to be compiled and run on a Windows operating system from within a Linux-like interface. By using Cygwin, we can easily execute SH files without any problems.

This will change the directory to the desktop. Now type the following command to execute SH File:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh filesExecuting script file through Cygwin

Method 2: Using the Windows Subsystem for Linux Feature in Windows 10

The Windows Subsystem for Linux (aka WSL) is a feature that allows users to run Linux command lines on a computer with a Windows 10 operating system installed. This feature was first added to Windows 10 in 2016. Just like Linux has Wine software for running Windows software, now Windows has WSL for running Linux software. However, enabling and working through this feature will require many steps, which is why we divided this method into three major steps and they are listed as follow:

Step 1: Enabling the Windows Subsystem for Linux

Step 2: Installing a Linux Distro for the Windows

There are three different ways to install a distro for Windows. However, sometimes one of the methods will not work for a specific user, so we are going to provide all three methods below:

Step 3: Initializing a Newly Installed Distro and Executing SH File

This will mount your Windows drives. Now you can go to the location you want to:

Method 3: Using Git Command Line for Executing SH Files

Git is a free and open source distributed version control system tool available for all operating systems. By default, Git is installed on Linux and macOS computers as a command line option. However, you can also download it from the official site for all operating systems.

Method 4: Translate Script File into Batch File

This method is not recommended for average users, but for the users who know script and bat files commands very well. You can just change the format and extension of SH file. It does require the skill of translating the script file into a batch file, so you should avoid using this method unless you know what format both these files use. We are going to show you the simple steps required for the sample code changes.

However, you will notice that the syntax that works in script files are not working in a batch file. As you can see, the comment is being ignored and the text will be printed twice without echo ‘on/off’.

Last Updated: October 31st, 2019 by How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files Hitesh J in Guides, Linux

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

chmod +x yourfilename.sh

chmod 755 yourfilename.sh

sudo bash yourfilename.sh

First, switch the user to root with the following command:

chmod +x yourfilename.sh

If your file is located at /opt directory then run the following command:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

Next, click on the Properties. You should see the following screen:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

Next, click on the permisssion tab and select Allow executing file as a program as shown below.

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

Next, double click on the file to execute the file.

WSL stands for “Windows Subsystem for Linux” is a compatibility layer for running Linux binary executables natively on Windows 10 and Windows Server 2019. It allows you to run native Linux command-line tools directly on Windows.

First, you will need to install WSL in your Windows 10 systems.

To do so, go to Settings > Update & Security > For Developers and check the Developer Mode radio button. Then, search for “Windows Features”, choose “Turn Windows features on or off”.

Next, scroll down, find WSL, check the box and install it. After installing WSL, you will need to restart your system to finish the installation.

Next, open the Command prompt, change the directory where yourfilename.sh is located and type bash yourfilename.sh to execute your file as shown below:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

You can install Ubuntu on Windows 10 by following the below steps.

First, go to Settings > Update & Security > For Developers and check the Developer Mode radio button. Then, search for “Windows Features”, choose “Turn Windows features on or off”.

Next, scroll down, find “Windows Subsystem for Linux (Beta)”, check the box and click Ok to install it. Once the installation has been finished, you will need to restart your system to finish installing the requested changes.

Next, open your Command prompt, type bash and hit Enter to install Ubuntu on Windows 10. Once the installation has been finished you should see the following screen:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

Now, change the directory with cd command to the directory where yourfilename.sh is located and hit Enter to run the file as shown below:

How to execute sh files. Смотреть фото How to execute sh files. Смотреть картинку How to execute sh files. Картинка про How to execute sh files. Фото How to execute sh files

Conclusion

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *