Skip to content

Commit 93ecccc

Browse files
authored
Again, fixed some silly errors
1 parent 9acce9c commit 93ecccc

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

‎ScriptingIntro.md‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ When I began my career the operating systems of the time where UNIX and Dos (Win
55

66
As \*nix systems remain the top choice in servers and networking, understanding their **shell** has remained an important factor for IT, IS, and Networking professionals. Even for Windows systems (MS Servers) the *ability to automate tasks with Powershell* is considered an essiential skill. This article, part of an Intro to Networking class, we will overview the shell scripting commands most used in Powershell and bash (standard Linux Shell) - full coverage is provided in the "Scripting" course.
77

8+
---
9+
---
10+
811
## What is Scripting?
9-
Scripting is providing a set of commands to the shell for it to execute. These commands can be:
12+
Scripting is providing a set of commands to the shell for it to execute. **It is the glue that holds systems together.** These commands can be:
1013
- in the shell's language
1114
- executables built-in to the kernel
1215
- other programming languages (Python, C#, Perl)
@@ -21,12 +24,14 @@ While in most \*nix systems we'd use:
2124

2225
tidy -xml -iq test.xml
2326

24-
Either of these could be a great help to a developer trying to understand some new XML file a client provided or be **piped** into another script for further parsing and automatic uploading to a database. **Scripting is the glue that holds systems together.**
27+
Either of these could be a great help to a developer trying to understand some new XML file a client provided or be **piped** into another script for further parsing and automatic uploading to a database.
28+
29+
---
2530

2631
## Basic Shell Scripting Commands
2732
On Linux you can get more infomation using ***\<command \-\-help\>*** and in Powershell using ***Get-Help \<command\>***. The basic shell scripting commands are:
2833

29-
#### Echo / Write-Host commands: Used for console display
34+
### Echo / Write-Host commands: Used for console display
3035
#bash
3136
echo "Hello World"
3237

@@ -44,12 +49,12 @@ Used by itself it just prints a blank line and it can be used with a variable (n
4449
Write-Host "$test World"
4550
Write-Host
4651

47-
#### Cat: Show contents of a file
52+
### Cat: Show contents of a file
4853
# This is the same in bash/Powershell (aliased to Get-Content)
4954
cat filename.txt
5055
Frequently, used with pipes to pass contents of file between cmdlets/executables or as quick check of file.
5156

52-
#### Grep / Select-String: Search text for pattern
57+
### Grep / Select-String: Search text for pattern
5358
Grep (and egrep) can have their own class. Select-String is Powershell's "equiv" but its not quite equal. As a basic intro: it searches files for a provided string or pattern (Regex).
5459

5560
#Search for any instance of "test in txt files"
@@ -77,10 +82,10 @@ Grep (and egrep) can have their own class. Select-String is Powershell's "equiv"
7782

7883
For more see [Digital Ocean's guide of Grep](https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux).
7984

80-
#### ls: List folder and files in directory
85+
### ls: List folder and files in directory
8186
This command is the same in PowerShell or \*nix. It lists the folders and files present in a particular directory (either current or provided path). Powershell's **ls** is actually `ls -l` (long list: i.e. include details) in \*nix.
8287

83-
#### pwd & cd: Find Path and Change Directory
88+
### pwd & cd: Find Path and Change Directory
8489
So these are ran together often enough. cd changes your directory (**. = current directory, .. = parent directory**) and pwd prints it out. Again, these are the same in both Linux and Powershell and would look like:
8590

8691
# Assuming in /Users
@@ -89,15 +94,15 @@ So these are ran together often enough. cd changes your directory (**. = current
8994
cd Users/Sam #change to this directory/sub-directory
9095
pwd # would print /Users/Sam
9196

92-
#### Mkdir: Makes a directory
97+
### Mkdir: Makes a directory
9398
Same on both again: makes a directory with default permissions (and current user as owner).
9499

95100
cd /home
96101
mkdir Sam
97102
cd Sam
98103
pwd # would print /home/Sam
99104

100-
#### CP, MV, Rename & Rename-Item: Basic file controls
105+
### CP, MV, Rename & Rename-Item: Basic file controls
101106
As we are using a command-line: copy, move, and rename are a bit different. Copy is essentially the same (it makes a copy of the original file). However, move (think cut) deletes the original and makes a new copy of the file whereas rename just changes the file name. So many times you'll use ***mv*** over ***rename*** because you just want to recreate the file (you use rename to keep permissions and owner the same).
102107

103108
# both systems
@@ -117,30 +122,30 @@ As we are using a command-line: copy, move, and rename are a bit different. Copy
117122

118123
## Intermediate Commands
119124

120-
#### sudo / su
125+
### sudo / su
121126
No real powershell equivilent (can use *-RunAs* flag but its not the same).
122127

123-
#### Ifconfig: Change to IP?
128+
### Ifconfig: Change to IP?
124129
Tuning and debuging of network....add IP
125130

126-
#### Netstat: Displays general network information
131+
### Netstat: Displays general network information
127132
Can be used to display:
128133
- routing tables
129134
- network connections
130135
- masquerade connections
131136
- interface statistics
132137
- even more
133138

134-
#### Nslookup: Displays the information of servers.
139+
### Nslookup: Displays the information of servers.
135140
Queries DNS and fetches information (ip address).
136141

137-
#### Uptime / Get-Uptime: Gives the uptime of server
142+
### Uptime / Get-Uptime: Gives the uptime of server
138143
Whenever server is left unattended this can be queried (ran) to determine if any downtime happened. Easy to automate by just redirecting (see below) to a log which is periodically checked.
139144

140145
## Piping and Redirects
141146
When it comes to automation, the ability to redirect output (pipe it) is essential. There are several variants on piping but it can be considered as the *couts, System.out, or other file operations* dealt with in most programming languages. The following are the main **pipes**.
142147

143-
#### redirecting output
148+
### redirecting output
144149
`>` is used to **redirect output** and ***write*** *it to a file*.
145150

146151
`>>` is used to **redirect output** and ***append*** *it to the end of a file*.
@@ -155,7 +160,7 @@ When it comes to automation, the ability to redirect output (pipe it) is essenti
155160
In Powershell, you cannot redirect pure strings (literally `Write-Output or "Hello World"`). See Piping for how to perform this.
156161

157162
<!--
158-
#### redirecting input
163+
### redirecting input
159164
`<` is used to **redirect input**.
160165
161166
$ cat < file.txt
@@ -201,7 +206,7 @@ Output:
201206
Add this later
202207
-->
203208

204-
#### Piping ( | ): Pass the output received from one command to another command or script.
209+
### Piping ( | ): Pass the output received from one command to another command or script.
205210
So this is the scripting equivilent of *chaining*, that fun task where we passed information from one command to another (like a builder class: ```Build b = Build.create().withParameterA("A").withParameterB(2);```). In fact, with Powershell its the only way to redirect direct strings to files without having to deal with Array conversions using two cmdlets:
206211

207212
`Set-Content` is used to **redirect output** and ***write*** *it to a file*.
@@ -215,3 +220,8 @@ You can also use them in automation tasks and parsing, such as finding all insta
215220
# -i = ignore case, -o = only words; xargs = make array into arguments
216221
# ./ = path to script (assumes shebang and executable permissions set)
217222
grep -io "john .*" ./*.txt | xargs ./parse_users.py
223+
224+
---
225+
---
226+
# Summary
227+
Scripting is a complex subject, and includes learning several languages, so though we cannot introduce a subject complete in such a small space. This should provide a basic overview of the main scripting commands and methods we will be using in (most of) your courses.

0 commit comments

Comments
 (0)