Linux shell script windows

What’s the Windows equivalent of a UNIX shell script?

I want to have an executable file that will call some other programs. The way I would do this in Linux is with a simple bash script that looks like this:

#!/bin/bash echo "running some-program" /home/murat/some-program arg1 arg2 

15 Answers 15

Take a look at PowerShell, which is the closest you will get to a true scripting language like you have in Unix. Other than that, for simple things such as simply runnning an application, take a look at Windows command script/MS-DOS batch files.

The primary issue with PowerShell (for me, at least) is that it is currently not installed by default on most versions of Windows (including XP and Vista). However, it will be included out-of-the-box on Windows Server 2008 and on Windows 7: tinyurl.com/5jhh2f

Yes, PowerShell is not installed by default on any versions of Windows except Win7, which will actually be PowerShell v2.

You could go with either an old school batch file (http://en.wikipedia.org/wiki/Batch_file) or you can go with a brand new shiny Windows PowerShell script (http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx).

I use batch for simple operations and PowerShell for complex scripts that also involve data processing.

There are a number of scripting options on windows:

  • Batch files
  • VBScript/CScript/JScript (very powerful when combined with WMI)
  • PowerShell
  • You could install Perl, Python, or other engine
  • You can install unix tools, via Cygwin for example, to get make most of the common unix commands available in windows as well.
  • Since we’re all (mostly?) programmers here, there’s nothing stopping you from making a .exe file to do what you want.

You can use a Windows batch file. Find more information in the Wikipedia article.

The Windows NT family of Operating Systems (NT 3.51, NT 4.0, 2000, XP, Vista) have a reasonably powerful Command Shell even if it is a bit esoteric. This shell does not have the power of the typical UNIX shells however it is ubiquitous on Windows machines and it is pretty amazing what can be achieved. A great book for this is Windows NT Shell Scripting by Timothy Hill. I always found Rob van der Woude’s Scripting Pages a great resource for batch files.

PowerShell is Microsoft’s latest offering in this field and takes scripting to a whole new level and gives you access to all of the power of the .NET framework as well as WMI, COM and any other interface into Windows. There is a pretty steep learning curve with this but it is definitely the way to go and I think it has the potential to become one of those foundational technology skills (like RegEx, XPath etc) in the Microsoft world. A good starting point is Microsoft Windows PowerShell Technology Center but there are a huge number of resources out there for this.

Читайте также:  How to add permissions in linux

I used to use batch files. And for simple examples like you gave above I still do, but more and more I have been using python to do these sorts of things. It has the added benefit of working across platforms for those rare scripts that are actually meaningful on multiple platforms.

Python is just one of many scripting languages you could use for this sort of thing. Perl, ruby, or php may be more your speed. I find that a scripting language gives me a richer and easier to understand syntax if I want to do something a little more complicated than just start a program with a few default arguments.

As long as we’re suggesting *nix command-line tools, I’d recommend checking out Cygwin (http://www.cygwin.com) as it emulates a *nix-like environment.

Update: Wow, this is woefully out of date now (though that’s inevitable). Powershell is definitely becoming more and more the language of choice for automation in Windows. It has many of the advantages of any .net language (like C#) combined with much of the expediency that exists in any good shell language, without the serious downsides of batch files.

For what it’s worth, the most popular options I’ve seen at a certain large, Redmond based software company (in rough order of popularity):

Note that Perl doesn’t require an install, it only requires a few environment variable updates, so it’s easy to just shove a full distro into your source control system along with other tools.

My personal preference would be to avoid batch files as much as possible, even with the most careful and rigorous of processes the benefit for the cost is just not worth it. You’ll never end up with a satisfactory solution if you use batch. Perl and Javascript are tempting to use but there are troublesome fundamental deficiencies which leave a lot to be desired, without strong development guidelines these methods will more likely than not lead to low quality code.

Python and Powershell are good candidates but I’m not sufficiently adept with them and, more importantly, a lot of people aren’t that familiar with them. Perhaps that’s not a good enough excuse. Tiny exes can have many problems as well and require discipline and process in order to ensure that the source is always checked in, that the exes conform to certain UI norms (e.g. they have useful help if you run -? or /?), that they have an obvious owner, that the process for updating the exes is discoverable, etc.

Источник

How to run Linux shell script on windows environment

So i want to know what would be the best option to select.

Thanks in advance for any help

it depends on what the script does. windows batch are not powerfull as shell-script. of course you can use javascript or powershell if you have knowledge of them.

exactly, but using the right tools for the right task can help. Take a look at robocopy, is easy to use it in simple .cmd / .bat and may just solve your whole problem with just a oneline.

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

It’s impossible to say what the correct approach is without some details about the script, preferably seeing it in its entirety. What commands do you rely on? Of those, which are standard tools and which are RH-specific? Do you touch any files and how do you reference the file paths? All of these questions would need to be answered.

5 Answers 5

It depends on how complex the scripts are, whether you want to make changes to them and what utilities the existing ones use.

If the scripts are very complex, you will be better off keeping the original and use Cygwin.

If you want to maintain the scripts and port changes back and forth, you will want to use the original (and use Cygwin)

If the script uses some Unix utilities that have not been ported to Cygwin, you either need to do the porting yourself (to be able to keep the original scripts) or convert the scripts to use Windows equivalent (or similar) programs

So I’d recommend you using Cygwin in most cases, unless the scripts are very easy to translate to Windows batch format

Источник

How to Run .SH Files in Linux & Windows Systems

run sh files windows and linux

A .SH file is a simple text file that contains the commands and a little logic to it. There are several ways to run a .SH file. A .SH file is very similar to the batch file of the Windows operating system and can be run in the Linux-based operating system. It is also possible to run .SH file in Windows 10 using Windows Subsystem for Linux.

In this tutorial, we will show you how to run .SH files in Linux and Windows 10 systems.

Run .SH Files in Linux

In this section, we will show you how to run .SH file in Linux with examples.

Basic Steps to Run .SH Files

Before running .SH files, you will need to give execute permission on your file that you want to run. You can set execute permission with the following command:

Next, you can run your .SH file by running the following command:

Run .SH file as a root User

In some cases, you will need root access to install application or make system level modifications with .SH file. You can run .SH file as a root user by using the sudo command as shown below:

Type your user password and hit Enter to execute the .SH file.

You can also use su command to login with superuser and execute the .SH file.

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

Next, run .SH file with the following command:

Run .SH file with CHMOD Command

You can also use chmod command to run .SH file. First, set the execute permission on your file with the following command:

Next, run .SH file with the following command:

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

Читайте также:  Install oracle linux ubuntu

Run .SH file with GUI Method

You can also run .SH file with Graphical user interface. To do so, first right click on the file that you want to run as shown below:

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

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

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

Run .SH files in Windows 10

There are several ways to run .SH files in Windows 10 operating systems.

Run .SH file with WSL

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:

Run .SH File with Ubuntu on Windows 10

If you want to run .SH file in a Linux environment on Windows 10 then you will need to install Ubuntu or any other Linux distros on Windows 10.

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:

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:

Conclusion

In the above tutorial, we learned how to run .SH files in Linux and Windows 10 operating systems. I hope you have now enough knowledge to run .SH files in Linux and Windows 10.

Recent Posts

  • Forcepoint Next-Gen Firewall Review & Alternatives
  • 7 Best JMX Monitoring Tools
  • Best PostgreSQL Backup Tools
  • Best CSPM Tools
  • Best Cloud Workload Security Platforms
  • Best Automated Browser Testing Tools
  • Event Log Forwarding Guide
  • Best LogMeIn Alternatives
  • Citrix ShareFile Alternatives
  • SQL Server Security Basics
  • Cloud Security Posture Management Guide
  • Cloud Workload Security Guide
  • The Best JBoss Monitoring Tools
  • ITL Guide And Tools
  • 10 Best Enterprise Password Management Solutions

Источник

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