0% found this document useful (0 votes)
26 views

Bash Programming: Andrew Vandever RHC (T, E, I, X)

This document provides an overview of Bash programming including scripting basics, expansion, loops, conditional statements, I/O redirection, user input, and System V init scripts. It covers topics such as shebang lines, variables, math, curly brace expansion, command substitution, tilde expansion, wildcards, loops, conditional tests, redirection operators, arguments, and using read to accept user input. The document also mentions using init scripts to help start and stop background processes at boot and shutdown.

Uploaded by

Andrew Vandever
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Bash Programming: Andrew Vandever RHC (T, E, I, X)

This document provides an overview of Bash programming including scripting basics, expansion, loops, conditional statements, I/O redirection, user input, and System V init scripts. It covers topics such as shebang lines, variables, math, curly brace expansion, command substitution, tilde expansion, wildcards, loops, conditional tests, redirection operators, arguments, and using read to accept user input. The document also mentions using init scripts to help start and stop background processes at boot and shutdown.

Uploaded by

Andrew Vandever
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 16

Bash Programming

Andrew Vandever RHC{T,E,I,X} [email protected] http://avcomp.net

Bash Programming
Andrew Vandever Technical Training Resources Scripting Basics Expansion Loops Conditional Statements I/O Redirection User Input System V Init Scripts

Scripting Basics

Bourne Again Shell Posix-Compliant Interactive Shell Script Automation, Repetition NOT exhaustive NOT covering external programs Check out guides on tldp.org

Scripting Basics

Magic - #!/bin/bash Add -x to debug Comments - # ; = EOL \ escapes EOL chmod +x echo $PATH

Expansion

VARIABLE=value

All-caps not needed, just recommended {} not always needed, but stops problems $ is necessary Specifies variable is an integer

echo ${VARIABLE}

declare -i VARIABLE=value

Expansion

Math

$[] $(()) {items,in,list}toexpand {a..z}range

Curly-brace expansion

Expansion

Command Substitution

$(command) `command` ~ = your home directory ~user = user's home directory man 7 glob

Tilde

Wildcard globs

Expansion

Prevention

\ escapes a single special character escapes all but $, ` and \ '' escapes all special characters

Loops

for VAR in items in list; do commands; done exit status - $? test - [ condition ] while [ test ]; do commands; done until [ test ]; do commands; done

Conditional Statements

Operators

&& ||

if [ test ]; then commands elif [ test ]; then commands else commands fi

Conditional Statements

case VAR in option)


commands ;; commands ;;

*)

esac

I/O Redirection

Channel 2 STDERR

2> 2>> > >> |

Channel 1 - STDOUT

I/O Redirection

Combining 1 and 2

&> &>> 2>&1 >&2 < -

Channel 0 STDIN

User Input

Arguments

$1, $2, ${10}, etc. $* $# $0 Force your users to give you the data you want in the form you want

read

System V Scripts

Help start and stop background processes at boot time and shutdown Necessary components:

chkconfig line to tell position, default levels case statement to accept typical service arguments start, stop, restart, status

Examples in /etc/init.d

Bash Programming

Many more possibilities! Find more info in guides at tldp.org

You might also like