The Saigon CTT
The Linux Shell and
BASH Scripting
The Saigon CTT
Objectives
Objectives
Identify differrent Linux shell environments
Understand the redirection of input
Understand and utilize command substitution
Write and configure BASH script using
variables, flow controls interactive input,
functions, arithmetic and arrays
The Saigon CTT
Example of Script
Example of Script
$ more example_script
#!/bin/bash
/usr/bin/nmbd -D
/usr/bin/smbd -D
The Saigon CTT
The Linux Shell
The Linux Shell
Shells : Bourne (sh), Bourne Again
(bash), Korn (ksh), C shell (csh, tcsh)
Programs start from command line have
separate environments : parameters,
variables , functions.
The Saigon CTT
The Linux Shell
The Linux Shell
Shells : Bourne (sh), Bourne Again
(bash), Korn (ksh), C shell (csh, tcsh)
Programs start from command line have
separate environments : parameters,
variables , functions.
The Saigon CTT
Shell Environment Customize
Shell Environment Customize
bash config files :
•
/etc/profile
•
~/.bash_profile, ~/.bash_login,
~/.profile, ..
Default environment variables : PS1, PS2,
HOME, LOGNAME, SHELL, PATH,
PAGER, LPDEST, PWD, DISPLAY,
MAIL, ..
set, unset, export, … commands
The Saigon CTT
Using the
Using the
bash
bash
shell
shell
Alt+Fn
gpm : mouse server deamon
up and down keys (~/.bash_history )
Ctrl+Z, Ctrl+C, *, ?, …
The Saigon CTT
Redirecting Input and Output
Redirecting Input and Output
Input (<) or (<0)
#mail < file_name
Output (>) or (1>)
# ls –l > file_name
( set –C or set –o noclobber : prevent
overwrite )
Append (>>)
Error (2>)
The Saigon CTT
Redirecting Input and Output
Redirecting Input and Output
Pipe (|)
#ls –l *.JPG | tee file_name
#ls –al /root | grep error >error_file
Back ticks (`) or “$()”
# which passwd
/usr/bin/passwd
# ls –l `which passwd`
-r-sr-xr-x 1 root root 13476 Aug 7 /usr/bin/passwd
The Saigon CTT
Redirecting Input and Output
Redirecting Input and Output
Sometimes the output of a command is a
list (ex: ls) and we wish to execute
another command on each of the entries,
use xargs
#ls | xargs grep README
Do Not use :
#ls –al | xarg grep README
The Saigon CTT
Background jobs
Background jobs
Job
#ls –l *.JPG | tee file_name
#ls –al /root | grep error >error_file
Backround jobs :
- Append with (&)
or
- Ctrl+Z and #bg %job_id
The Saigon CTT
Variables
Variables
Naming : not begin with a digit
Assigning : VAR=value
VAR=$(command)
VAR=`command`
Note : not SPACES around “=”
Ex: # VAR=“Hello World”