You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ScriptingIntro.md
+27-17Lines changed: 27 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,11 @@ When I began my career the operating systems of the time where UNIX and Dos (Win
5
5
6
6
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.
7
7
8
+
---
9
+
---
10
+
8
11
## 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:
10
13
- in the shell's language
11
14
- executables built-in to the kernel
12
15
- other programming languages (Python, C#, Perl)
@@ -21,12 +24,14 @@ While in most \*nix systems we'd use:
21
24
22
25
tidy -xml -iq test.xml
23
26
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
+
---
25
30
26
31
## Basic Shell Scripting Commands
27
32
On Linux you can get more infomation using ***\<command \-\-help\>*** and in Powershell using ***Get-Help \<command\>***. The basic shell scripting commands are:
28
33
29
-
####Echo / Write-Host commands: Used for console display
34
+
### Echo / Write-Host commands: Used for console display
30
35
#bash
31
36
echo "Hello World"
32
37
@@ -44,12 +49,12 @@ Used by itself it just prints a blank line and it can be used with a variable (n
44
49
Write-Host "$test World"
45
50
Write-Host
46
51
47
-
####Cat: Show contents of a file
52
+
### Cat: Show contents of a file
48
53
# This is the same in bash/Powershell (aliased to Get-Content)
49
54
cat filename.txt
50
55
Frequently, used with pipes to pass contents of file between cmdlets/executables or as quick check of file.
51
56
52
-
####Grep / Select-String: Search text for pattern
57
+
### Grep / Select-String: Search text for pattern
53
58
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).
54
59
55
60
#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"
77
82
78
83
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).
79
84
80
-
####ls: List folder and files in directory
85
+
### ls: List folder and files in directory
81
86
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.
82
87
83
-
####pwd & cd: Find Path and Change Directory
88
+
### pwd & cd: Find Path and Change Directory
84
89
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:
85
90
86
91
# Assuming in /Users
@@ -89,15 +94,15 @@ So these are ran together often enough. cd changes your directory (**. = current
89
94
cd Users/Sam #change to this directory/sub-directory
90
95
pwd # would print /Users/Sam
91
96
92
-
####Mkdir: Makes a directory
97
+
### Mkdir: Makes a directory
93
98
Same on both again: makes a directory with default permissions (and current user as owner).
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).
102
107
103
108
# both systems
@@ -117,30 +122,30 @@ As we are using a command-line: copy, move, and rename are a bit different. Copy
117
122
118
123
## Intermediate Commands
119
124
120
-
####sudo / su
125
+
### sudo / su
121
126
No real powershell equivilent (can use *-RunAs* flag but its not the same).
122
127
123
-
####Ifconfig: Change to IP?
128
+
### Ifconfig: Change to IP?
124
129
Tuning and debuging of network....add IP
125
130
126
-
####Netstat: Displays general network information
131
+
### Netstat: Displays general network information
127
132
Can be used to display:
128
133
- routing tables
129
134
- network connections
130
135
- masquerade connections
131
136
- interface statistics
132
137
- even more
133
138
134
-
####Nslookup: Displays the information of servers.
139
+
### Nslookup: Displays the information of servers.
135
140
Queries DNS and fetches information (ip address).
136
141
137
-
####Uptime / Get-Uptime: Gives the uptime of server
142
+
### Uptime / Get-Uptime: Gives the uptime of server
138
143
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.
139
144
140
145
## Piping and Redirects
141
146
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**.
142
147
143
-
####redirecting output
148
+
### redirecting output
144
149
`>` is used to **redirect output** and ***write****it to a file*.
145
150
146
151
`>>` 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
155
160
In Powershell, you cannot redirect pure strings (literally `Write-Output or "Hello World"`). See Piping for how to perform this.
156
161
157
162
<!--
158
-
#### redirecting input
163
+
### redirecting input
159
164
`<` is used to **redirect input**.
160
165
161
166
$ cat < file.txt
@@ -201,7 +206,7 @@ Output:
201
206
Add this later
202
207
-->
203
208
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.
205
210
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:
206
211
207
212
`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
215
220
# -i = ignore case, -o = only words; xargs = make array into arguments
216
221
# ./ = path to script (assumes shebang and executable permissions set)
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