Hands-on Introduction to Linux Commands and Shell Scripting
This course has following modules…
This course has following modules…
What is Unix?
UNIX beginnings:
Linux’s beginnings:
Linux, use cases today:
Kernel
Linux filesystem:
Communicating with Linux System:
What is a shell?
User interface for running commands
Interactive language
Scripting language
Shell command applications:
Getting information
whoami – usernameid – user ID and group IDuname – operating system nameps – running processestop – resource usagedf – mounted file systemsman – reference manualdate – today’s dateNavigating and working with files and directories
Printing file and string contents
Compression and archiving
tar – archive a set of fileszip – compress a set of filesunzip – extract files from a compressed zip archivePerforming network operations
hostname – print hostnameping – send packets to URL and print responseifconfig – display or configure system network interfacescurl – display contents of file at a URLwget – download file from a URLMonitoring performance and status
sort — Sort lines in a file, -r will do the same in the reverseuniq — Filter out repeated linesgrep (“global regular expression print”) — Return lines in file matching patterngrep -i — makes grep search case-insensitivecut — Extracts a section from each linepaste — Merge lines from different filesWhat is a script?
Script: list of commands interpreted by a scripting language
Commands can be entered interactively or listed in a text file
Scripting languages are interpreted at runtime
Scripting is slower to run, but faster to develop
What is a script used for?
Widely used to automate processes
ETL jobs, file backups and archiving, system admin
Used for application integration, plug-in development, web apps, and many other tasks
Shell scripts and the ‘shebang’
Shell script – executable text file with an interpreter directive
Aka ‘shebang’ directive
#!interpreter [optional-arg]‘interpreter’ – path to an executable program
‘optional-arg’ – single argument string
Example – ‘shebang’ directives
Shell script directive:
#!/bin/sh
#!/bin/bashPython script directive:
#!/usr/bin/env python3Filters are shell commands, which:
Take input from standard input
Send output to standard output
Transform input data into output data
Examples are wc, cat, more, head, sort, …
Filters can be chained together
Pipe command – |
For chaining filter commands
commmand1 | command2Output of command 1 is input of command 2
Pipe stands for pipeline
Scope limited to shell
Set – list all shell variables
Defining shell variables:
var_name=valueNo spaces around =
unset var_namedeletes var_name
Extended scope
export var_nameenv — list all environment variables
# — precedes a comment; — command separator* — filename expansion wildcard? — single character wildcard in filename expansion\ — escape special character interpretation"" — interpret literally, but evaluate meta-characters'' — interpret literallyInput/Output, or I/O redirection, refers to a set of features used for redirecting
> — Redirect output to file>> — Append output to a file2> — Redirect standard error to a file2>> — Append standard error to a file< — Redirect file contents to standard inputReplace command with its output
$(command) or `command`Store output of pwd command in here:
Program arguments specified on the command line
A way to pass arguments to a shell script
./MyBashScript.sh arg1 arg2
Bath mode:
Commands run sequentially
command1; command2Concurrent mode:
Commands run in parallel
command1 & command2Job scheduling
Schedule jobs to run automatically at certain times
Cron allows you to automate such tasks
What are cron, crond, and crontab?
Cron is a service that runs jobs
Crond interprets ‘crontab files’ and submits jobs to cron
A crontab is a table of jobs and schedule data
Crontab command invokes text editor to edit a crontab file
Scheduling cron jobs with crontab
Viewing and Removing cron jobs