Red Hat System Administration III - Unit 3 - Bash Scripting and Tools
Bash Overview
- Start scripts with
#!/bin/bash - Make it executable with
chmod - Put it in a directory in
$PATH~/bin- for user's private programs/usr/local/bin- locally developed scripts by other uses on the system/usr/local/sbin- locally developed scripts used by root
- Variable syntax:
$my_var${my_var}
- Command substitution:
today=$(date +%Y-%m-%d); echo $today
- Quoting & escaping:
echo "Today is $X"- interpolateecho 'Today is $X'- literal
-
Repetition can be achieved with:
for var in commands; do command; donefor i in {1..9}; do echo "This is $i"; done -
Conditional branching:
- utilise non-zero value returns for false
- Syntax:
test EXPRESSIONortest [ EXPRESSION ] - Use
$?to get last return code - Non-zero or zero-length string operators:
test -{n|z} STRING - String comparison:
==,!= - Numeric comparison:
-eq,-ne - File status operations:
test -{e|f|d|r|w|x|s} FILE-s- check for empty files
- Logical operators:
-o- OR-a- AND!- NOT
man testto get info on Bash operators
- Use
envto get list of environment variables - Positional params:
script.sh TEST DEMO-$0 $1 $2$@- all positional params$#- count of params
- Running commands on remote machine:
- Save file locally:
ssh user@host 'command1; command2' > log.local - Save file remotely:
ssh user@host 'command1; command2 > log.remote'
- Save file locally:
Text Processing Tools
diff
- Used to compare two files
- Args:
-c- display surrounding lines for context-u- unified output format-r- perform a recursive comparison of files
patch
- Changes files from patches created with
diff -u:$ diff -u samsreport.sh samsreport-2.sh >> my_patch
- Patch file using the
patchcommand
$ patch samsreport.sh < my_patch
patching file samsreport.sh
- Use
-boption to specify a backup before patching. Will create a file with an.origsuffix:> samsreport.sh.orig
grep
- Search for patterns of text
- Args:
-iperform case-insensitive search-nprecede return lines with line of numbers-rperform a recursive search of lines, starting with the named directory-cdisplay a count of lines matchingvreturn lines that don't match-llist names of files with one line containing the pattern
cut
- Get first column delimitered by
::> cat /etc/passwd | cut -d : -f 1
wc
-ldisplays number of lines-wdisplays number of words-cdisplays number of bytes-mdisplays nubmer of characters
sort
-n- sort numerically instead of char-k- set the sort field-t- specify a field separator
tr
Translate command
> echo "HELLO" | tr 'A-Z' 'a-z'
hello
sed
- Edits a stream of textual data
- Use
gto make it global, usedto delete lines
- Use
> echo "dogs and cats" | sed '/ddogs/d'
dogs and cats
> echo "dogs and cats" | sed '/dogs/d'
Regular Expression
^- line begins$- line ends.- any single character[z]- a single character that is x, y or z[^xyz]- opposite
Password Aging
Use change to change password information for a user
Defaults stored in /etc/login.defs:
-m- min days-M- max days-W- warn days
Defaults stored in /etc/default/useradd:
-I- inactive days-E- expiry