1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
Bash vs. vista_power_shell
1. COVER STORY Bash vs. Vista PowerShell
Comparing Bash with the Windows Vista shell
SHELL GAMES
es.war.einmal.., photocase.com
Microsoft’s new PowerShell relies on .NET framework libraries and
thus has access to a treasure trove of functions and objects. How does
PowerShell measure up to traditional shells like Bash?
means that you trust the author. Power-
BY MARCUS NASAREK Shell will not run “unknown” scripts at
all. To run a script without a signature,
you need to change the execution policy
B
oth Bash and the Windows Vista Listing 1 compares outputting a counter to RemoteSigned at the command line
PowerShell include commands with for in PowerShell and Bash. like so:
for navigating directories, man- Bash and PowerShell are similar with
aging files, and launching other pro- respect to case-based program flow con- PS:> Set-ExecutionPolicy U
grams. System administration is an im- trol with if or switch. The definition of RemoteSigned
portant duty for the shell, and Bash and functions, the use of environmental vari-
PowerShell are equipped to help manage ables, scoping (restricted validity of vari- Listing 1: Loops
systems from the command prompt. ables), the use of regular expressions,
A for loop in the Windows Vista
Whereas Bash typically relies on a and the evaluation of program return
PowerShell:
combination of newer tools and classic values are all similar in both shells.
PS:> for ($i=1;$i -le 3;$i++) {
Unix utilities, the PowerShell has its own
Restricted Launch Write-Host $i }
set of command-line programs. Win-
dows refers to PowerShell commands as Permission 1
cmdlets. The PowerShell cmdlet called An initial difference between PowerShell 2
Get-Process is a counterpart to ps, and and Bash occurs when a script is exe- 3
the cmdlet Get-Content corresponds to cuted. PowerShell will not launch script PS:>
less. PowerShell differs significantly from files by default and thus only supports The same loop in Bash:
previous Windows command shells. In interactive use. However, PowerShell bash[~]$ for ((i=1;i<=3;i++); do
this article, I look at how Windows Vista will run scripts if they are digitally echo $i; done
PowerShell compares with Bash. signed. The digital signature identifies 1
To support program control, a shell the script’s author because the author is 2
needs elements for conditional execu- the only person capable of creating the 3
tion. for or while evaluate a variable to signature by cryptographic means. Ac-
bash[~]$
support a defined number of iterations. cepting the author’s signature certificate
38 ISSUE 78 MAY 2007 W W W. L I N U X - M A G A Z I N E . C O M
2. Bash vs. Vista PowerShell COVER STORY
input and output files. Bash trusts that
Listing 2: Get-Member Output the next command in the chain can do
01 PS:> Get-Content Textdatei.txt | Get-Member something meaningful with the output.
02 In PowerShell, all cmdlets create de-
03 TypeName: System.String fined objects as their output data instead
04 of text-only output. Even if it appears
that only strings are passed in text out-
05 Name MemberType Definition
put, what happens is that the complete
06 ---- ---------- ----------
output is converted to an object.
07 Clone Method System.Object Clone() Objects can be queried with the
08 CompareTo Method System.Int32 CompareTo(Object Get-Member command, which outputs
value), System.Int32 CompareTo(String strB) the elements and functions of the object.
09 Contains Method System.Boolean Contains(String See Listing 2. For example, the command
value)
10 CopyTo Method System.Void CopyTo(Int32 PS:> Get-Content Textdatei.txt |
sourceIndex, Char[] destination, Int32 destinationIn... Sort { $_.Length }
11 ...
12 Length Property lets you sort the lines in a text file by
length with the object's Length property.
13
Although passing data objects is
14 PS:>
slightly more complex, this object orien-
tation helps standardize operations and
This command tells PowerShell to accept gards everything as a filesystem and not supports handling of complex data struc-
all local scripts. If you downloaded the only navigates the filesystem and drives, tures. Bash cannot compete here; in-
data, or if the data is attached to an but the Registry, the certificate store, and stead, it relies on the abilities of external
email, the shell will continue to insist on environmental variables. You can copy, programs to handle data structures.
a signature. The ability to prevent com- rename, and move Registry values just For example, Bash needs an external
mands from external sources from exe- as you would files on a drive. The Pow- XML parser (like Saxon or Xalan-J) to
cuting is a whole new league security- erShell refers to these virtual filesystems parse XML files. Listing 3 is a short Pow-
wise, and scripted viruses such as “Love as Providers, thus implementing a phi- erShell script that loads an RSS feed off
Letter” [1] lose their threat. losophy that Linux has always offered to the Internet in the form of an XML file.
In contrast to this, Bash does not rely Bash: “Everything is a file.” The script defines a Show-SpiegelRSS
on digital signatures to evaluate a function that gets the current RSS Feeds.
script’s execution permissions. Instead, Forwarding
filesystem permissions determine PowerShell’s most powerful tool is the Conclusions
whether the script is permitted to run. pipe, which supports ordered passing of In one respect, PowerShell relies on the
Both shells share some surprising values, allowing the use of output from Unix concept that many small utilities
common ground in the way they handle one command as input to the next com- are preferable to one large, custom-made
system configuration. New to the world mand. Bash supports pipes too, but it utility. At the same time, it adopts an ob-
of Windows is the way PowerShell re- does not make particular demands on ject-oriented approach that makes larger
scale projects simpler at the cost of a
Listing 3: Show-SpiegelRSS.ps1 steeper learning curve. The major prob-
lem with objects is that you need to in-
01 # Show-RSS.ps1
vest significant time in discovering
02 # Declaration of variables for URL which function or object you need. The
03 $feed="http://rss.news.yahoo.com/rss/linux") Get-Member cmdlet is likely to see much
04 Write-Host -ForegroundColor "green" "RSS-Feed: " $feed use in PowerShell.
05 # Download RSS feed Bash is useful as a plain but straight-
06 $wco = New-Object System.Net.WebClient
forward tool for most daily tasks. If it
comes to the need for advanced uses
07 $rss = [xml]$wco.DownloadString($feed)
and complex data structures, you can
08 # Show title
branch out into object-oriented Python
09 Write-Host -ForegroundColor "red" $rss.rss.channel.title or the graphical capabilities of Tcl/Tk. ■
10 # Display short form
11 $rss.rss.item | Select-Object title,description | format-table INFO
12 # Display title and description of entries [1] CERT-Advisory CA-2000-04 “Love
13 $rss.rss.channel.item | Select-Object title,pubDate,description Letter Worm”: http://www.cert.org/
| format-list advisories/CA-2000-04.html
W W W. L I N U X - M A G A Z I N E . C O M ISSUE 78 MAY 2007 39