Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Shell Commands & Script Programming

Similar presentations


Presentation on theme: "Operating Systems Shell Commands & Script Programming"— Presentation transcript:

1 Operating Systems Shell Commands & Script Programming
Kyung Eun Park, D.Sc. Department of Computer and Information Sciences Towson University

2 Contents Linux Shell Scripting PowerShell

3 Credits FreeOS Software Construction (J. Shepherd)
Operating Systems at Cornell (Indranil Gupta, Niranjan Nagarajan) Programming in C (Brian W. Kernighan, Bell Lab.)

4 Shell Script Basics Write script program using any editor
$ vi myShell Make is executable $ chmod 755 myShell $ chmod +x myShell Execute the script $ bash script_name $ sh script_name $ ./script_name

5 First Shell Script $ vi shellscript #  comment # First Shell Script # clear  clear the screen echo “Hello Shell Script”  print message $ chmod +x shellscript  set execution permission $ ./shellscript

6 System Variables in Shell
BASH=/bin/bash HOME=/home/tiger LOGNAME=tiger OSTYPE=linux-gnu PATH=/usr/lib64/qt- 3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/jdk1. 8.0_101/bin:/home/tiger/bin PWD=/home/tiger/tmp SHELL=/bin/bash USERNAME=tiger

7 Definition of User Defined Variables (UDVs)
Syntax: variable_name=value $ num=10 $ name=Park $ vehicle=Car Rules: - no spaces on either side of the equal sign - case-sensitive - to print: $ echo $NAME_OF_VARIABLE

8 Shell Arithmetic Operations
Syntax: expr op1 operator op2 $ expr 1 + 3 $ expr 5 - 2 $ expr 50 / 5 $ expr 20 \*  Use \* instead of * (used as wildcard) $ expr 1032 % 10 $ echo `expr ` $ echo $?  Prints exit status

9 Read Statement Syntax: read variable1, variable2, … variable
$ cat > sayName # echo “Please type your name” read name echo “Hello $name!”  CTRL-D to save $ chmod +x sayName $ ./sayName Please type your name Brad Hello Brad!

10 Wild Cards * : matching any string or group of characters (0 or more) ? : matching any single character […] : matching any one of the enclosed characters $ ls [abc]*  show all files beginning with letters a, b, c $ ls ?  show all files whose names are 1 character long $ ls data*.txt  show all files whose names begin with data $ ls data?.txt  show all files whose names are 5 character long and begin with data

11 Variables and Operations (1)
Variable definition: x with value 10 and print it on screen with echo and $ $ x=10 $ echo $x Variable definition: street with value YR and print it on screen $ street=YR $ echo $street Operation $ echo  print: 7800 not 807 as a string concatenation For math operations: use expr, space between number and operator $ expr  print: 7800

12 Variables and Operations (2)
Variable assignments $ num1=30 $ num2=6 Assignment with expression $ result=`expr 30 /6` $ echo $result String substitution $ echo “The address is $street 7800” Parameter substitute: command execution first $($ echo `expr `)  Parameter substitution with backquotes

13 Shell Commands Command in backquotes is executed first and the output is used with other commands $ pwd $ cp /home/kpark/os/shell.sh `pwd`  shell.sh is copied to the working directory $ cp /home/kpark/os/shell.sh  compare this with above command Examples $ echo “Today is `date`” $ echo $HOME grep command with ps command to find out a process $ ps aux | grep elasticsearch  the process you want to search for $ kill PID_of_the_process

14 Shell Built in Variables
$# : number of command line arguments $* : all arguments to shell : same as above $- : options supplied to shell $$ : PID of shell $! : PID of last started background process (started with &)

15 Example Script with Shell Built in Variables
$ vi demo #!/bin/sh # # Script that demos, command line arguments echo “Total number of command line argument : $#” echo “$0 is script name” echo “$1 is first argument” echo “$2 is second argument” echo “All of them are: $* or

16 Standard I/O Redirection: <, >, >>
Syntax: command > filename $ ls > tmpfile : create new file, tmpfile, send the result of ls command to it $ history >> tmpfile : attach the result of history to tmpfile $ date >> tmpfile $ cat > months Jan Feb Mar Apr Press CTRL-D to save $ sort < months > sorted $ cat sorted

17 Pipes Connect the output of one program to the input of another program without any temporary file $ ls | more $ who | sort $ who | sort > users $ who | grep tiger

18 Process Related Command
$ ps $ kill $ ps aux, ps –ag $ ps ax | grep sshd $ top : currently running processes and information such as memory and CPU usage $ pstree

19 Decision Making if…fi Shell uses 0 for true and non-zero value for false Syntax: if condition then commands … fi e.g. if test $1 -gt 0 echo “$1 number is positive” else echo “$1 number is negative”

20 if-else-fi Syntax: e.g. if condition then commands … else fi if cat $1
echo –e “\n\nFile $1, exists”

21 Numeric Operators with test Command
Using test command -eq -ne -lt -le -gt -ge $ cat > ispositive  write a shell script using cat #!/bin/sh if test $1 –gt 0 then echo “$1 number is positive” fi $ chmod 755 ispositive $ ./ispositive 50

22 String Comparison with test Command
Using test command string1 = string2 string1 != string2 string1 -n string1 -z string1 $ cat > equals  write a shell script using cat #!/bin/sh if test $1 –eq $2 then echo “$1 equals to $2” fi $ chmod 755 equals $ ./equals julie Julia

23 File and Directory Processing with test Command
Using test command -s file -f file -d dir -w file -r file -x file $ cat > filetest  write a shell script using cat #!/bin/sh if test –f $1 then echo “File $1 exists” fi $ chmod 755 filetest $ ./filetest names

24 Logical Operators with test Command
Using test command ! expression expression1 –a expression2 expression1 –o expression2 $ cat > logictest  write a shell script using cat #!/bin/sh if test $1 –a $2 then echo “Both $1 and $2 are TRUE” fi $ chmod 755 logictest $ ./logictest 1 1

25 Argument Checking $ cat > add2numbers if [ $# -ne 2] then echo “Error : Two numbers to be added are needed.” echo “Usage : $0 number1 number2” fi ans=`expr $1 + $2` echo “Sum is $ans”  CTRL-D to save $ chmod 755 add2numbers $./add2numbers  Error notification with correct usage will be printed $./add2numbers

26 Functions Syntax: function-name () { commands… } $ SayHello() { echo “Hello $LOGNAME” return $ SayHello

27 PowerShell A task automation and configuration management framework from Microsoft Designed especially for system administrators and built on top of the .NET Framework common language runtime (CLR) and the .NET Framework Accepts and returns .NET Framework objects Components Command-line shell Scripting language (built on the .NET Framework) Interactive prompt and a scripting environment Provides full access to COM and WMI Administrators can perform administrative tasks on both local and remote Windows systems as well as remote Linux systems and network devices Currently, open-source and cross-platform (since Aug. 18, 2016)

28 History of PowerShell Microsoft’s command-line interface tool (shell)
Cmd.exe and COMMAND.COM Command line interpreter for basic commands Separate console application can be executed from the shell Script language support in the form of batch files for automating various tasks Monad (Microsoft Shell or MSH) in 2002 Renamed Windows PowerShell as a component of Windows in 2006 On Aug. 18, 2016, made PowerShell open-source and cross-platform for Windows, OS X, CentOS and Ubuntu  PowerShell Core for .NET Core

29 Cmdlets Windows PowerShell introduces the concept of a cmdlet (pronounced “command-let”) Simple, single-function command-line tool built in the PowerShell environment with more than one hundred basic core cmdlets Verb-Noun naming pattern, such as Get-ChildItem: self-descriptive Objects or collections as the output results of cmdlets  used in a pipeline Cmdlets are specialized .NET classes instantiated by the PowerShell runtime Modules: portable version of Cmdlets in PowerShell V2

30 Pipeline Piping the output of cmdlet to another cmdlet as input
Use ‘|’ operator The chain of cmdlets stages executes within the PowerShell runtime. They are structured .NET objects rather than byte streams .ToString() method to retrieve the text representation of the PowerShell objects

31 Windows PowerShell Integrated Scripting Environment (ISE)

32 Scripting PowerShell includes a dynamically typed scripting language for implementing complex operations using cmdlets Script language includes: Variables: prefixed with ‘$’, assigned any value or objects output of cmdlets Strings: enclosed in single quotes or in double quotes $args: array of all the command line arguments passed to a function from the command line $_: current object in the pipeline Functions; using function keyword, functions are created: Branching: if-then-else Loops: while, do, for, foreach Structured error/exception handling: caught using try … catch construct Closures/lambda expressions name value1 value2 name –Param1 value1 –Param2 value2 function name ($Param1, $Param2) { Instructions }

33 Running PowerShell Start Windows PowerShell by typing powershell or
Start  PowerShell  Windows PowerShell

34 PowerShell Cmdlets Commands
Get-Help Get-Service

35 Get-Service | Get-Member

36 Cmd.exe and Unix Commands available inside Windows PowerShell
cat, cd, chdir, clear, cls, copy, del, diff, dir, echo, erase, h, history, kill, lp, ls, mount, move, popd, ps, pushd, pwd, r, ren, rm, rmdir, sleep, sort, tee, type, write Get-Alias ps


Download ppt "Operating Systems Shell Commands & Script Programming"

Similar presentations


Ads by Google