Cis 216 – shell scripting

12
Remembering the basics – Week 1 Dan Morrill April 01 2013 CIS 216 – Shell Scripting

description

A small primer for my CIS 216 class on shell scripting

Transcript of Cis 216 – shell scripting

Page 1: Cis 216 – shell scripting

Remembering the basics – Week 1

Dan Morrill April 01 2013

CIS 216 – Shell Scripting

Page 2: Cis 216 – shell scripting

RMDIR – removes a directory only if it is empty

RMDIR –r – removes a directory regardless if it is empty or not

CHMOD 777 –r /user/dan – recursively changes the permissions of the directory /user/dan to RWXRWXRWX

grep – pattern matching for example ps –ef |grep apache2 will look for all processes running under the name apache2egrep – used in regular expressions

Remembering the basic Unix Commands

Page 3: Cis 216 – shell scripting

find – find the location of a file (or files) on a hard drive at the command line

echo – writes data to the screen (standard output)The TTY – Screen is always standard output

(STDOUT)more – displays the contents of a file one

page at a time (hard for long files like syslog)clear – clears the screen of text and provides

the command prompt chown – change the owner of a file or

directoryrm – remove a file

Remembering the basic Unix Commands

Page 4: Cis 216 – shell scripting

mv – move a file to a new place and or new namechgrp – change the group ownership of a filemkdir – make a directoryls – list the files in a directory, ls –la show the

long form (including permissions and owners) of the files in a directory

cat – an alternative to showing the data in a file all at once

pwd – display the current directorypasswd – change the password of the currently

logged in user

Remembering the basic Unix Commands

Page 5: Cis 216 – shell scripting

>> - append data to the END of a file> - overwrites the file and any data in it| - pipe character – strings commands together|| - logical OR – in that A || B in a loop, condition A or

condition B & - executes your process in the background – so you

can still use the command line ./script.sh & will run your process in the background

&& - Logical AND A && B if A succeeds, run Bsleep – stop execution of the script for a specified

number of seconds sleep 20 means sleep for 20 seconds

Manipulating data output

Page 6: Cis 216 – shell scripting

diff – compares two files (this is great for seeing what changes were made in a script against the original and modified)

sdiff – only works with the 132 character display, but compares files side by side

lp – prints a file to the configured printerlpstat – shows the user what is in the printer

queue( ) – runs the enclosed command in a sub-

shell(( )) – evaluates and assigns a value to a

variable and does math in a sub-shell

Manipulating data output

Page 7: Cis 216 – shell scripting

$(( )) – evaluates the enclosed expression[ ] – test an expression to see if it is true or

false< > - used to compare values or strings$( ) – command substitution – great when

doing logical OR’s or AND’s

Remembering the basic Unix Commands

Page 8: Cis 216 – shell scripting

++ - auto increment a value for I=1 ++ (takes I up by 1 for every loop)

-- - auto decrement a value for I=100 -- (takes I down by 1 for every loop)

<= - less than or equal to>= - greater than or equal to

Using math in a script

Page 9: Cis 216 – shell scripting

I will expect to see this in the header of every Unix Script you will turn in:

#!/bin/sh (tells the computer to use a bourne shell)## Script: Name of script (what you named this file)# Author: Your Name (so I know who wrote it)#Date: (the date you wrote it)#Revision: 1.1.A(how many times it changed, and what

state it is in(A, B, D, T or P) All scripts should also have A, B, D, T or P at the end of the

revision statement so I know if it is Alpha, Beta, Dev, Test, or Production. All scripts turned in should be production scripts

The header of every Unix Script

Page 10: Cis 216 – shell scripting

How to get your script to do more useful stuffIf. Thenif [test]

Thenstuff I want the computer to do

fiIf. Then. Else

if [test]Then

stuff I want the computer to doElse

Stuff I want the computer to do if the first condition didn’t happen

fi

Control Structures

Page 11: Cis 216 – shell scripting

forfor A=1

dostuff I want the computer to do

done

WhileWhile A=1

dostuff I want the computer to do

done

Control Structures

Page 12: Cis 216 – shell scripting

Until until A=1

dostuff I want the computer to do

done

Casecase A=1

stuff I want the computer to docase A != 1

stuff I want the computer to doeasc

Control Structures