Linux

Running 'Linux scripts' on Mac or Windows systems

Marco Fioretti addresses the issue of taking scripts written for a Linux-based system and trying to use them on different systems like OS X and Windows.

Some days ago, after reading, "Creating and deleting files using the Mac Terminal", a faithful reader wrote to TechRepublic.com:

I have a Mac folder at work, which currently contains more than 10,000 images. Every month we add a few thousands images to that folder, and must remove some thousands. I do always have a complete list of the images that should be removed from the folder, but I need some sort of script for Mac that will do the job for me.

This week's post is a general, but direct answer to that call for help. I will first summarize what scripts are and how they work, then present a simple script to do the requested job on Linux, and finally mention some resources to reuse "Linux scripts" on both Mac and Windows systems.

Disclaimer: The script and resources below were prepared specifically because a reader asked for it, but please understand that, at the moment, I do not have any Mac or Windows computer available to actually test them myself. Back up everything (which is something you should regularly do anyway) before trying them! For the same reason, readers with such systems are very welcome to point out errors, omissions, or better resources for reusing Linux tips and scripts on other platforms!

What is a script, or why a "Linux script" may not run elsewhere

Before even getting started, it is useful to review and clarify a term or two, because it will save you lots of frustration when some "Linux script" doesn't work on other platforms. Strictly speaking, there is no such thing as a "Linux script".

Computer programs are executable files, that is, blobs of instructions in machine-readable format. Launching a program means telling the processor in your computer to go execute that sequence of machine-level codes. Those sequences are created by special programs, called compilers, by reading the source code of that program. That code describes how the program works in a language that is much easier for humans to read and write.

Scripts are different. In the software world, that word usually defines a sequence of commands in some language, saved in a plain text file, that must not be compiled. When you "launch" a script, the operating system first detects the language in which it is written. Immediately after, the whole script file is passed to the special executable program that acts as interpreter for that language. The interpreter reads and executes all the commands directly, more or less one at a time. A Perl script is nothing else but a plain text file, written in a language that the executable program perl understands.

Gnu/Linux is not a language, but an operating system that can run all sorts of programs. That's why there cannot be a "Linux script", only scripts that run on Linux, because their interpreter is available, and usually preinstalled, on Linux. Shell, Perl, Python, Ruby... there are tens of scripting languages, but none of them will work if the corresponding interpreter is not installed.

To make things more complicated, er... stimulating, one single script in one language may call scripts in other languages and/or executable programs that are installed by default only on some operating system. So, don't be surprised if the first run of a "Linux script" fails because some piece is missing. It's not a big deal, and it is often very easy to fix.

How to remove all the files mentioned in a list

Back to the original question now! Let's assume that you have a plain text file called thelist.txt, with the names of all and only the files you want to delete, one per line:

  customer_list.xls
  invoice_1.doc
  old_company_logo.pjpg
  home_page.html

On Linux, you may remove all those files by opening a terminal window in the folder that contains them and typing the following command:

/complete/path/to/cleaner.sh thelist.txt

where cleaner.sh is the script I'll show you in a moment, and "/complete/path/to/" is the actual folder in which it was saved. The command above assumes that thelist.txt is in the same folders of the files to be removed. If this is not the case, you should prepend the complete path also to thelist.txt. besides, cleaner.sh must have executable permissions. Here is its content:

  1 #! /bin/bash
  2
  3 LIST=$1
  4 while read LINE; do
  5    echo "Now deleting: $LINE;"
  6    #rm "$LINE"
  7 done < $LIST

Line 1, often called the "shebang line" is the one that tells the operating system what interpreter should be used for the current script: the file above is written for the Bash shell interpreter /bin/bash. Line 3 assigns the first argument passed to the script to the $LIST variable. The while loop in lines 4 to 7 scans the $LIST file one line at a time. Line 5 prints to the terminal the content of the current $LINE. Line 6 would remove the corresponding file... if it were not commented with the hash character #.

The way to run in a Mac terminal that or any other shell script is explained here. I suggest that you run the script as is and then, only when you are sure that it is reading the file names correctly, remove the # character from line 6 and rerun it. For a general introduction on how to reuse "Linux scripts" on Mac or Windows, check the tutorials and programs below.

Mac OS Windows

The first step to run on Windows scripts developed for Linux is to install the Cygwin. To learn how to use it, look at:

About

Marco Fioretti is a freelance writer and teacher whose work focuses on the impact of open digital technologies on education, ethics, civil rights, and environmental issues.

6 comments
joelschuster
joelschuster

Check out msys first from mingw. A much simpler install and much smaller one as well.

Alex Post
Alex Post

Another good option for Windows to run Linux shell scripts is using GnuWin utilities. You can download package CoreUtils for Windows from http://gnuwin32.sourceforge.net/packages/coreutils.htm. It takes only about 6 Mb (compare to Cygwin's size!) and let me run your sample script on Windows7 really!

joecamaro
joecamaro

Because, under the hood, it's UNIX.

mikifinaz1
mikifinaz1

I am NOT a programmer or a script maven, but I often have to cobble together a script or two to get specific things done. In an attempt to get things done I have scanned through many texts in books and online and never has this simple explanation be provided. I have only read two books that have taken an overview of creating instructions, (one was an old NT scripting book that I still have somewhere and the other was a book that took the view that you could look at programming through the tasks you need to do and figure out how to do things you want to do by asking the question, "How would you do that task in this language.") both helpful, but I would like to see a book with this overview written about both programming and scripting. There are many good books on specific languages that usually dive right into information packed pages on specific uses of the language. This article would be a great introduction to a simple book on the simple tasks that most people need to do with a script as well as a book of the same caliber on doing simple programming as a follow-up. I would like to see the first chapter be something like: I want to do these things. Is it better to learn a programming language and use it to fix my problem or can it be done with a scripting language? How do you pick which tool for which job? A task oriented book or series of articles is rare to be found. No one actually discusses the problem to be FIXED first and then starts at the beginning to explain how you get to what tools to use, and THEN how you use them as well as why they are the best solution. The best tool for the best job. As an example the problem of your reader, lay out a task a simple task and show how it is done with simple examples of scripts in each OS. Mike

daboochmeister
daboochmeister

cat file.txt | xargs rm -f I know i know, you wanted to show a script.

SiO2
SiO2

you can easily forget about learning a full programming language like C+, you wont need it. Scripts and programs are the same thing; lists of instructions. The difference is that a script controls an OS, giving it complex instructions eg moving tagged or named files from one place to another. A program is a list of lower level instructions that by comparison, creates file structures, reads and writes data bytes and performs complex mathematics. Programs often have interfaces too, where scripts do not, although this is not a definition, just a convention. All script languages should be able to to anything the OS can, so choosing one is a matter of preference. The author mentions Perl, however Ruby is more usually installed on a Mac or Windows, and Linux will often have both. However, I'd recommend Python, because it runs on everything and is simpler to learn in my opinion. It is also more 'OS agnostic' than the others, behaving the same regardless of the host OS. This is helpful when googling how to do something; you will get one approach instead of 2 or 3 different ones.

Editor's Picks