Shell script: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
 
(Added ConEmu to CMD alternatives)
 
(11 intermediate revisions by 3 users not shown)
Line 2: Line 2:
== Definition ==
== Definition ==


A shell program executes commands that a user directly types or that are read from a file (the shell script).
* A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Usually, shell script refers to scripts written for a Unix shell, while COMMAND.COM (DOS) and cmd.exe (Windows) command line scripts are usually called batch files ([http://en.wikipedia.org/wiki/Shell_script Wikipedia], retrieved 14:30, 3 August 2007 (MEST)).
 
* A shell program executes commands that a user directly types or that are read from a file (the shell script). As an alternative to shell scripts, you can in some cases, use a scripting language like Perl or PhP.


An educational technologist should know some shell scripting for the following reasons:
An educational technologist should know some shell scripting for the following reasons:
# Often, educational software is programmed in java. When these programs are distributed as *.jar files they may not launch properly (even on Windows) and you will have to write a shell script.
# Often, educational software is programmed in Java. When these programs are distributed as *.jar files they may not launch properly (even on Windows) and you will have to write a shell script.
# Some tools that may be useful (e.g. the most powerful XSLT processors) also may need a script to be launched.
# Some tools that may be useful (e.g. the most powerful XSLT processors) also may need a script to be launched.
# To deploy Internet Software it is better to use a linux system (it's cheaper and slightly safer than windows). To do so, you need a lit bit of scripting knowledge.
# To deploy Internet Software it is better to use a Linux system (it's cheaper and slightly safer than windows). To do so, you need a lit bit of scripting knowledge.
 
See also:
* [[:fr:Fichier_de_commande|Fichier_de_commande]] (in french).


== Overview of Shell Scripts ==
== Overview of Shell Scripts ==


The following definitions are based on [http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_01_01.html Garrels'] and various articles from Wikipedia.
'''Acknowledgment''': The following definitions are based on [http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_01_01.html Garrels'] and various articles from Wikipedia.


=== sh or Bourne Shell ===
=== sh or Bourne Shell ===
The (almost) original shell still used on UNIX systems and in UNIX-related environments. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with UNIX programs.
The (almost) original shell still used on UNIX systems and in UNIX-related environments. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with UNIX programs.


===  ksh or the Korn shell ===
===  ksh or the Korn shell (Unix) ===


The Korn shell (ksh) is a Unix shell which was developed by David Korn (AT&T Bell Laboratories) in the early 1980s. A superset of the Bourne shell and upwards compatible with the Bourneshell.
The Korn shell (ksh) is a Unix shell which was developed by David Korn (AT&T Bell Laboratories) in the early 1980s. A superset of the Bourne shell and upwards compatible with the Bourneshell.


=== bash or Bourne Again shell ===
=== bash or Bourne Again shell (Unix) ===


Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification. Commands that work in sh, also work in bash. However, the reverse is not always the case.  
Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification. Commands that work in sh, also work in bash. However, the reverse is not always the case.  


Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification,
Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification.


=== csh or C shell ===
Example:


The C shell (csh) is a Unix shell developed by Bill Joy for the BSD Unix system. The syntax of this shell resembles that of the C programming language and therefore popular with C programmers. Today, mostly replaced by tsch.
=== csh or C shell (Unix) ===


=== tcsh or Turbo C shell ===
The C shell (csh) is a Unix shell developed by Bill Joy for the BSD Unix system. The syntax of this shell resembles that of the C programming language and therefore popular with C programmers. Today, mostly replaced by tcsh.
 
=== tcsh or Turbo C shell (Unix) ===


A superset of the common C shell, enhancing user-friendliness and speed.
A superset of the common C shell, enhancing user-friendliness and speed.
=== cmd.exe (Windows) ===
cmd.exe is the command line interpreter on OS/2, Windows CE and on Windows NT-based systems (including Windows 2000, XP, Vista, and Server 2003). It is the analog of COMMAND.COM in MS-DOS and Windows 9x systems, or of the shells used on Unix systems.
To open a simple command promt (command line interface, CLI) under Windows 7, SHIFT-Click on a directory. You then will see menu item that allows opening a command prompt window for a given directory. Alternatively, type "cmd" in the search box of the start menu.
Shell script files are either called *.bat or *.cmd
Example file ''sax.cmd'' to launch the saxon [[XSLT]] processor
java -jar c:\bin\saxon8.jar %1 %2 %3 %4 %5 %6 %7 %8 %9
Example file ''xquery.cm'' to launch the saxon [[XQuery]] processor (a bit more complicated)
rem  set the variable JAVA to the place where the java command is located
rem  not the case if you just have one Jave that is correctly installed), for example
rem  set JAVA="c:\Program Files\java\jdk1.5.0\bin\java"
set JAVA=java
rem    modify the following line to set the classpath (saxon.jar)
set CP=-cp c:\soft\saxon861b\saxon8.jar
%JAVA% %CP% net.sf.saxon.Query %1 %2 %3 %4 %5 %6 %7 %7
Example file ''jedit.bat'' to launch jedit
start javaw.exe -jar "c:\program files\jEdit\jedit.jar"
=== Windows PowerShell ===
   
   
=== What shells do you have ===
Windows PowerShell, previously Microsoft Shell or MSH (codenamed Monad) is an extensible command line interface (CLI) shell and scripting language product developed by Microsoft based on object-oriented programming and version 2.0 of the .NET Framework. It is available for Windows XP, Windows Server 2003 and Windows Vista and planned for inclusion with Windows Server 2008.
 
Links:
* You may get Version 1 from here [http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Microsoft] (in case the links break, just search for "PowerShell" at Microsoft).
* [http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=60deac2b-975b-41e6-9fa0-c2fd6aa6bc89 Version 2] (preview as of sept. 2009).
* [http://msdn.microsoft.com/en-us/library/dd835506(VS.85).aspx documentation for powershell v.2].
* [http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx tutorial]
 
Example to help you see and remove a file (e.g. a troyan horse exe)
dir -force          (will list hidden stuff too)
del -force dell.sdr  (will kill the hidden *.sdr file)
del -force -recurse tmp (will kill the tmp dir)
 
=== Alternative shells for Windows? ===
 
There are several non-Microsoft shell alternatives for cmd.exe and PowerShell. Which one you choose depends on your needs, background or other preferences.
 
* '''Unix-like Shells'''. There are some projects to bring the power of [http://en.wikipedia.org/wiki/POSIX POSIX] shells (Unix/BSD/Linux etc) to Windows. Most notably is [http://en.wikipedia.org/wiki/Cygwin Cywin]. Related to Cygwin shells are the following projects MinGW, Mintty, msysgit, RXVT. As these tools offer a compatibility layer to support non-native components the tools may be to heavy for 'just' a cmd alternative. [http://www.cygwin.com/ Cygwin homepage]
* '''Terminals'''. There are several alternative terminals which gives you better support for the history of commands, improved tab-completion, normal copy and paste etc. These tools can be seen as direct replacements for cmd. Examples:<br/>


On a linux system type:
** [http://sourceforge.net/projects/pycmd/ pyCMD]
cat /etc/shells
** [http://jpsoft.com/download_windows_batch_scripting.html TCC/LE]
** [http://www.gammadyne.com/cmdline.htm#gs GS.Exe]
** [http://sourceforge.net/projects/fvpconsole/ FVP Console]


On Windows XP:
* '''Commercial shells'''. There are advanced commercial products like [http://www.powercmd.com/ PowerCMD] and [http://jpsoft.com/take_command_windows_scripting.html Take Command] which brings improvements to the terminal, commands and window handling. These options are mostly used in professional environments.
* Usually you only have cmd.exe
* '''Window enhancers'''. These are not really shell replacements, but enhances the shell with a GUI (like tabs). The most used tools are [http://sourceforge.net/projects/console/ Console2] and [http://code.google.com/p/conemu-maximus5/ ConEmu]. Other window enhancers (though not actively developed) are [http://en.poderosa.org/ poderosa] and [http://gnuwin32.sourceforge.net/summary.htmlhttp://www.gnu.org/software/screen/ GNU screen ]. There are possibilities to combine windows enhancers with the terminals listed above. Console2 for example has the possibility to select alternative terminals. This gives you powerful shell features together with the ease of window enhancements like tabs.
* Some people however, install
* '''GNU tools'''. These tools are native ports of Unix tools including lots of command line tools like grep [http://gnuwin32.sourceforge.net/]
* '''ColorConsole'''. This an all-in-one replacement for CMD with a GUI. The software is developed by Nenad Hrg and also comes as a portable version. [http://www.softwareok.com/?seite=Freeware/ColorConsole]
* '''Stylish Dosbox'''. A tool that gives cmd.exe a modern look [http://stylishdosbox.codeplex.com/]
* '''Automation''': If you need to automate tasks on Windows than besides PowerShell there are several other script languages available like [http://www.autoitscript.com/site/autoit/ Autoit] or [http://www.autohotkey.com/ AutoHotKey]. Some multi-platform scripting languages are [http://www.tcl.tk/ TCL/TK], [http://python.org/ Python] and [http://www.perl.org/ Perl]. Another option is to script your tasks with a GUI tool like [http://delphistep.cis.si/prettyflow.htm Pretty Flow].


== Unix CSH ==


== MS Windows ==
=== What more shells do you have ? ===
 
On a Linux system type:
cat /etc/shells


== Links ==
== Links ==
Line 53: Line 110:
; Introductions, comparisons
; Introductions, comparisons
* [http://en.wikipedia.org/wiki/Shell_script Shell Script] (Wikipedia)
* [http://en.wikipedia.org/wiki/Shell_script Shell Script] (Wikipedia)
* [http://en.wikipedia.org/wiki/Shell_%28computing%29 Shell (computing)]
* [http://en.wikipedia.org/wiki/Shell_%28computing%29 Shell (computing)] (Wikipedia)
* [http://en.wikipedia.org/wiki/Comparison_of_computer_shells Comparison of computer shells] (Wikipedia).  
* [http://en.wikipedia.org/wiki/Comparison_of_computer_shells Comparison of computer shells] (Wikipedia).  


Line 60: Line 117:
* [http://en.wikipedia.org/wiki/Bash Bash] (Wikipedia). The default Linux / MacOS X shell.
* [http://en.wikipedia.org/wiki/Bash Bash] (Wikipedia). The default Linux / MacOS X shell.
* [http://en.wikipedia.org/wiki/Cmd.exe cmd.exe] (Wikipedia). The Windows shell.
* [http://en.wikipedia.org/wiki/Cmd.exe cmd.exe] (Wikipedia). The Windows shell.
* [http://en.wikipedia.org/wiki/Windows_PowerShell Windows PowerShell] (Wikipedia).


=== Resource sites ===
=== Resource sites ===
Line 70: Line 128:


; Bash
; Bash
* [http://steve-parker.org/sh/sh.shtml Steve's Bourne / Bash shell scripting tutorial]
* [http://steve-parker.org/sh/sh.shtml Bourne / Bash shell scripting tutorial] by Steve Parker
* [http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash Guide for Beginners] by Machtelt Garrels.
* [http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash Guide for Beginners] by Machtelt Garrels.




[[Category: Technologies]]
 
[[Category: Installation tips]]
[[Category:Server administration]]

Latest revision as of 00:10, 20 October 2012

Draft

Definition

  • A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Usually, shell script refers to scripts written for a Unix shell, while COMMAND.COM (DOS) and cmd.exe (Windows) command line scripts are usually called batch files (Wikipedia, retrieved 14:30, 3 August 2007 (MEST)).
  • A shell program executes commands that a user directly types or that are read from a file (the shell script). As an alternative to shell scripts, you can in some cases, use a scripting language like Perl or PhP.

An educational technologist should know some shell scripting for the following reasons:

  1. Often, educational software is programmed in Java. When these programs are distributed as *.jar files they may not launch properly (even on Windows) and you will have to write a shell script.
  2. Some tools that may be useful (e.g. the most powerful XSLT processors) also may need a script to be launched.
  3. To deploy Internet Software it is better to use a Linux system (it's cheaper and slightly safer than windows). To do so, you need a lit bit of scripting knowledge.

See also:

Overview of Shell Scripts

Acknowledgment: The following definitions are based on Garrels' and various articles from Wikipedia.

sh or Bourne Shell

The (almost) original shell still used on UNIX systems and in UNIX-related environments. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with UNIX programs.

ksh or the Korn shell (Unix)

The Korn shell (ksh) is a Unix shell which was developed by David Korn (AT&T Bell Laboratories) in the early 1980s. A superset of the Bourne shell and upwards compatible with the Bourneshell.

bash or Bourne Again shell (Unix)

Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification. Commands that work in sh, also work in bash. However, the reverse is not always the case.

Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification.

Example:

csh or C shell (Unix)

The C shell (csh) is a Unix shell developed by Bill Joy for the BSD Unix system. The syntax of this shell resembles that of the C programming language and therefore popular with C programmers. Today, mostly replaced by tcsh.

tcsh or Turbo C shell (Unix)

A superset of the common C shell, enhancing user-friendliness and speed.

cmd.exe (Windows)

cmd.exe is the command line interpreter on OS/2, Windows CE and on Windows NT-based systems (including Windows 2000, XP, Vista, and Server 2003). It is the analog of COMMAND.COM in MS-DOS and Windows 9x systems, or of the shells used on Unix systems.

To open a simple command promt (command line interface, CLI) under Windows 7, SHIFT-Click on a directory. You then will see menu item that allows opening a command prompt window for a given directory. Alternatively, type "cmd" in the search box of the start menu.

Shell script files are either called *.bat or *.cmd

Example file sax.cmd to launch the saxon XSLT processor

java -jar c:\bin\saxon8.jar %1 %2 %3 %4 %5 %6 %7 %8 %9

Example file xquery.cm to launch the saxon XQuery processor (a bit more complicated)

rem  set the variable JAVA to the place where the java command is located 
rem  not the case if you just have one Jave that is correctly installed), for example
rem  set JAVA="c:\Program Files\java\jdk1.5.0\bin\java"
set JAVA=java
rem    modify the following line to set the classpath (saxon.jar)
set CP=-cp c:\soft\saxon861b\saxon8.jar
%JAVA% %CP% net.sf.saxon.Query %1 %2 %3 %4 %5 %6 %7 %7

Example file jedit.bat to launch jedit

start javaw.exe -jar "c:\program files\jEdit\jedit.jar"

Windows PowerShell

Windows PowerShell, previously Microsoft Shell or MSH (codenamed Monad) is an extensible command line interface (CLI) shell and scripting language product developed by Microsoft based on object-oriented programming and version 2.0 of the .NET Framework. It is available for Windows XP, Windows Server 2003 and Windows Vista and planned for inclusion with Windows Server 2008.

Links:

Example to help you see and remove a file (e.g. a troyan horse exe)

dir -force           (will list hidden stuff too)
del -force dell.sdr  (will kill the hidden *.sdr file)
del -force -recurse tmp (will kill the tmp dir)

Alternative shells for Windows?

There are several non-Microsoft shell alternatives for cmd.exe and PowerShell. Which one you choose depends on your needs, background or other preferences.

  • Unix-like Shells. There are some projects to bring the power of POSIX shells (Unix/BSD/Linux etc) to Windows. Most notably is Cywin. Related to Cygwin shells are the following projects MinGW, Mintty, msysgit, RXVT. As these tools offer a compatibility layer to support non-native components the tools may be to heavy for 'just' a cmd alternative. Cygwin homepage
  • Terminals. There are several alternative terminals which gives you better support for the history of commands, improved tab-completion, normal copy and paste etc. These tools can be seen as direct replacements for cmd. Examples:
  • Commercial shells. There are advanced commercial products like PowerCMD and Take Command which brings improvements to the terminal, commands and window handling. These options are mostly used in professional environments.
  • Window enhancers. These are not really shell replacements, but enhances the shell with a GUI (like tabs). The most used tools are Console2 and ConEmu. Other window enhancers (though not actively developed) are poderosa and GNU screen . There are possibilities to combine windows enhancers with the terminals listed above. Console2 for example has the possibility to select alternative terminals. This gives you powerful shell features together with the ease of window enhancements like tabs.
  • GNU tools. These tools are native ports of Unix tools including lots of command line tools like grep [1]
  • ColorConsole. This an all-in-one replacement for CMD with a GUI. The software is developed by Nenad Hrg and also comes as a portable version. [2]
  • Stylish Dosbox. A tool that gives cmd.exe a modern look [3]
  • Automation: If you need to automate tasks on Windows than besides PowerShell there are several other script languages available like Autoit or AutoHotKey. Some multi-platform scripting languages are TCL/TK, Python and Perl. Another option is to script your tasks with a GUI tool like Pretty Flow.


What more shells do you have ?

On a Linux system type:

cat /etc/shells

Links

Overviews

Introductions, comparisons
Various Flavors

Resource sites

Tutorials

Bash