Shell Variable - Nassau Community College

Download Report

Transcript Shell Variable - Nassau Community College

Session 25
Shell Programming
and PowerShell
Fall 2011
Nassau Community College
ITE153 – Operating Systems
1
Session 25
Ubuntu & Linux
Shell Programming
Fall 2011
Nassau Community College
ITE153 – Operating Systems
2
What Is the Shell?
The UNIX shell is a
program that starts
running when you log in.
It is a:
• command interpreter
• programming language
There are many
different shells. The
most popular are:
• Korn Shell (ksh)
• Bourne Shell (sh)
• C Shell (csh)
• Bourne Again (bash)
Shell Variables
A Shell Variable
stores a value
The value can be a:
• pathname
• command line
• text string
There are two basic
types of shell variables
• User defined variables
• Special variables
Special Shell Variables
Name
HOME
PATH
PS1
TMOUT
Value
Absolute pathname of your
home directory
The directory names that are
searched in command
execution
Primary shell prompt
Number of seconds shell will
wait for input before exiting
User Defined Variables
Action
Formula
Example
Create
Name=”value”
la=”ls –la”
Use
$name
$la
Find value echo $name
echo $la
Remove
unset la
unset name
The Environment
• An environment is associated with each
process you run
• What makes up your environment?
• Special & user-defined variables
• Your home directory
• Your current directory
Environment Commands
export Puts shell variables into the
environment
set
Displays the names and values of
variables available to the current
shell
unset Removes the value of specified
variables
env
Displays exported variables
The korn Shell
•
•
•
•
•
The features of ksh include:
Compatibility with sh
Command line history
Interactive, command line editing
The use of aliases
Aliases
Action
Formula
Example
Create
alias name=”value”
alias la=”ls –la”
Use
name
la
Find value alias name
alias la
Remove
unalias la
unalias name
Command History
Essential Shell Variables
• $HISTFILE
• $HISTSIZE
• $EDITOR
Commands are stored in
.history
Procedure
• <esc> - (minus)
• Brings up last
command
• Repeat pressing - to
step back through
previous commands
• set –o vi works
also
Editing Command Lines
• If $EDITOR is set you
/bin/vi
• Use vi commands that
work with a line
vi command review:
• a append after cursor
• i insert before cursor
• dw delete word
• x delete character
• r replace character
Customizing .profile
• When you login, UNIX reads your .profile
• Edit .profile to:
• Set shell variables
• Set aliases
• Run commands
• Changes take effect when you logout/login
or by typing . .profile
Lab A: Linux Shell Commands
Fall 2011
Nassau Community College ITE153
– Operating Systems
14
What Is a Shell Program?
• An executable text file
containing shell
commands
• Also called ‘shell script’
• Automates your work
Example
$ cat secureIt
pwd
ls - ld
chmod 700 .
echo It is changed.
ls - ld
$ chmod secureIt 777
$ secureIt
$ /home/richard/Stuff
drwxrwxrwx 2 richard office 1024 Dec 20 6:20
Stuff
It is changed.
drwx------ 2 richard office 1024 Dec 20 6:20
Stuff
Positional Parameters
• Special shell variables
• Identifies the location of a command line
argument
cmd $1 $2 $3 $4 $5 $6 $7 $8 $9
$ wc abc Stuff/mypassword fileA
$1
$2
$3
Example
$ cat secureIt2
cd $1
pwd
ls - ld
chmod 700 .
echo It is changed.
ls - ld
$ chmod secureIt2 777
$ secureIt /home/richard/NEW
$ /home/richard/NEW
drwxrwxrwx 2 richard office 1024 Dec 20 6:20
NEW
It is changed.
drwx------ 2 richard office 1024 Dec 20 6:20
NEW
Other Special Variables
$$The process ID#
$#The number of arguments
$*All the arguments
The read Command
• Accepts user input
• Stores it in a variable
$ cat showMe
echo Enter absolute directory name: \c
read Dname
ls -l $Dname
Shell Programming Guidelines
$ cat backwards
#! /bin/ksh
# A shell program should have lots of
# comments. This program reads the
# arguments on the command line and
# returns them in reverse order.
echo $3 $2 $1
$ backwards 7 8 9
9 8 7
Session 25
Windows 7 Professional
PowerShell
Fall 2011
Nassau Community College
ITE153 – Operating Systems
22
Windows PowerShell
• Windows PowerShell is a new Windows commandline shell designed especially for system
administrators
• The Windows PowerShell includes an interactive
prompt and a scripting environment that can be
used independently or in combination
• Unlike most shells, which accept and return text,
Windows PowerShell is built on top of the .NET
Framework, and accepts and returns .NET
Framework objects.
• This fundamental change in the environment brings
entirely new tools and methods to the management
and configuration of Windows
Fall 2011
Nassau Community College ITE153
– Operating Systems
23
Windows PowerShell
• Windows PowerShell introduces the concept of a
cmdlet (pronounced "command-let"), a simple,
single-function command-line tool built into the
shell.
• You can use each cmdlet separately, but their
power is realized when you use these simple tools in
combination to perform complex tasks.
• More than one hundred basic core cmdlets
• Windows PowerShell providers enable you to access
other data stores, such as the registry and the
digital signature certificate stores, as easily as you
access the file system.
Fall 2011
Nassau Community College ITE153
– Operating Systems
24
CMDLET’s
• A cmdlet (pronounced "command-let") is a single-feature
command that manipulates objects in Windows
PowerShell.
• You can recognize cmdlets by their name format -- a verb
and noun separated by a dash (-), such as Get-Help, GetProcess, and Start-Service
• In traditional shells, the commands are executable
programs that range from the very simple (such as
attrib.exe) to the very complex (such as netsh.exe).
Fall 2011
Nassau Community College ITE153
– Operating Systems
25
CMDLET’s
• In Windows PowerShell, most cmdlets are very simple,
and they are designed to be used in combination with
other cmdlets. For example, the "get" cmdlets only
retrieve data, the "set" cmdlets only establish or change
data, the "format" cmdlets only format data, and the "out"
cmdlets only direct the output to a specified destination.
• Each cmdlet has a help file that you can access by typing:
get-help <cmdlet-name> -detailed
• The detailed view of the cmdlet help file includes a
description of the cmdlet, the command syntax,
descriptions of the parameters, and an example that
demonstrate use of the cmdlet.
Fall 2011
Nassau Community College ITE153
– Operating Systems
26
How To Get Started
• First Time You May Get:
WARNING: File
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDiagnostics\PSDiagnostics.psm
1 cannot be loaded because the execution of scripts is disabled on this system. Please
see "get-help about_signing" for more details.
• Enter:
set-executionpolicy remotesigned
Fall 2011
Nassau Community College ITE153
– Operating Systems
27
What Version Am I Running?
• What Version Am I Running?
• Type: $Host or $HOST (notice the case insensitivity)
• PS C:\Windows\system32> $Host
Name
: ConsoleHost
Version
: 2.0
InstanceId
: 94c4d446-15cf-45d1-a6d9-53205a8ef39d
UI
:
System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture
: en-US
CurrentUICulture : en-US
PrivateData
: Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace
: System.Management.Automation.Runspaces.LocalRunspace
Fall 2011
Nassau Community College ITE153
– Operating Systems
28
What Version Am I Running?
• Notice that we can use Objects!
• PS C:\Windows\system32> $Host.Version
Major
----2
Fall 2011
Minor
----0
Build
-----1
Revision
--------1
Nassau Community College ITE153
– Operating Systems
29
We Can Find Out About Processes
• PS C:\Windows\system32> get-help get-process examples
• PS C:\Windows\system32> get-process explorer
Handles
NPM(K)
PM(K)
WS(K) VM(M)
CPU(s)
Id ProcessName
-------
------
-----
----- -----
------
-- -----------
1298
55
76724
74468
327 1,949.50
2116 explorer
• Try some Linux commands:
ps
ls
pwd
Fall 2011
Nassau Community College ITE153
– Operating Systems
30
What About Services?
• PS C:\Windows\system32> get-service
• PS C:\Windows\system32> get-service spooler
Status
-----Running
Fall 2011
Name
---spooler
DisplayName
----------Print Spooler
Nassau Community College ITE153
– Operating Systems
31
What About Services?
• PS C:\Windows\system32> get-help service
Name
---Get-Service
Stop-Service
Start-Service
Suspend-Service
Resume-Service
Restart-Service
Set-Service
New-Service
Fall 2011
Category
-------Cmdlet
Cmdlet
Cmdlet
Cmdlet
Cmdlet
Cmdlet
Cmdlet
Cmdlet
Synopsis
-------Gets the services on a local or remote computer.
Stops one or more running services.
Starts one or more stopped services.
Suspends (pauses) one or more running services.
Resumes one or more suspended (paused) services.
Stops and then starts one or more services.
Starts, stops, and suspends a service
Creates a new Windows service.
Nassau Community College ITE153
– Operating Systems
32
What About Services?
• PS C:\Windows\system32> stop-service -force spooler
• PS C:\Windows\system32> get-service spooler
Status
Name
DisplayName
------------------Stopped spooler
Print Spooler
• PS C:\Windows\system32> start-service spooler
• PS C:\Windows\system32> get-service spooler
Status
Name
DisplayName
------------------Running spooler
Print Spooler
Fall 2011
Nassau Community College ITE153
– Operating Systems
33
Lab B: PowerShell
Fall 2011
Nassau Community College ITE153
– Operating Systems
34
Important URLS
• Scripting With Windows PowerShell - a series of five
webcasts on how to use this scripting language
• Windows PowerShell Getting Started Guide - this Getting
Started Guide provides an introduction to Windows
PowerShell: the language, the cmdlets, the providers, and
the use of objects.
• PowerGUI - A free script editor that doubles as a fullyextensible administrative console.
• Windows PowerShell- a no non-sense guide from the
Microsoft Technet library
Fall 2011
Nassau Community College ITE153
– Operating Systems
35
Important URLS
• What Can I Do With Windows PowerShell? - This series
provides a task-based introduction to Windows PowerShell
cmdlets: rather than focusing on the individual cmdlets
themselves, the emphasis is on the tasks you can carry out
using those cmdlets. These tasks include everything from
reading and writing text files to managing event logs to
sorting and filtering data. This is part of a much larger and
very helpful Scripting Chapter in Microsoft's TechNet
Library.
• PowerShell Tutorial - This is a very compact overview
document, by Erik Hjelmås, that will get you started with
some basic program constructions which youwill recognize
based on some-sort-of-programming-background.
Fall 2011
Nassau Community College ITE153
– Operating Systems
36
Important URLS
• The Windows PowerShell Toolbox - Essential tools and
resources for working with Windows PowerShell. If you
don't already have it installed, download Windows
PowerShell.
• Unix shell scripting with ksh/bash - a very good online
tutuorial by Richard Brittain, Dartmouth College
Computing Services. also available in a single
file, printable version.
Fall 2011
Nassau Community College ITE153
– Operating Systems
37
Homework
 Review the Slides
 Review the Important URLs
 Play with PowerShell
Fall 2011
Nassau Community College ITE153
– Operating Systems
38