Bash 4

32
Bash 4 Linux Open Administration Days Pieter Colpaert _ _

description

My presentation on Bash on loadays

Transcript of Bash 4

Page 1: Bash 4

Bash 4Linux Open Administration Days

Pieter Colpaert__

Page 2: Bash 4

#Understanding Bash

>A little history

>Reinventing UNIX

#Speaking Bash

#Bash 4

#_

Page 3: Bash 4

Understanding Bash_

Page 4: Bash 4

A little history (1970)

Page 5: Bash 4

And some UNIX today (2010)

CC BY-SA – Larry Miller

Page 6: Bash 4

But also

#Routers

#Mainframes

#Servers

#Smartphones

#PDA's

#Toasters

#..._

Page 7: Bash 4

Let's reinvent UNIX

/rules/1# Writing software is one thing. Making it communicate is another_

Page 8: Bash 4

Let's reinvent UNIX

#Communication

>Systemcalls to hardware through kernel

>Systemcalls to user through a scriptinglanguage_

Page 9: Bash 4

Why Bash?

#Not much knowledge required

#Combines C-shell, Bourne Shell, …

#Free Software

#_

Page 10: Bash 4

What the hell is wrong with a GUI?

#Nothing

>For once in a life-time configurations

>Living today is pretty awesome

#Everything

>When you have to configure 20 PC's the same way

>When you don't want to install a demo-version of some proprietary tool

>When you want to do things fast_

Page 11: Bash 4

Speaking Bash_

Page 12: Bash 4

&, (( )), ., :, [, [[, alias, bg, bind, break, builtin, caller, case, cd, command, compgen, complete,

compopt, continue, coproc, declare, dirs, disown, echo, enable, eval, exec, exit, export, false, fc, fg, for, for, function, getopts, hash, history, if, jobs, kill, let, local, logout, mapfile, bind, printf, pushd, pwd, read, readarray, readonly, return, set, select, shift,

shopt, source, suspend, test, time, times, true, typeset, ulimit, umask, unalias, unset, until,

variables, wait, while, { cmd; }

Built-in commands

Page 13: Bash 4

External commands

#Compiled programs

#Bash-scripts

#Alias-commands

#Perl-scripts

#Functions

#_

Page 14: Bash 4

man-pages

#If you don't know how a command works

>man command

>info command

Page 15: Bash 4

Hello World!

On stdout:

#echo hello world!

On stderr:

#echo hello world! >&2

To a file:

#echo hello world! > HELLOWORLD

To the end of a file:

#echo hello world! >> HELLOWORLD

Page 16: Bash 4

Input

###################In script

#read a b c

#set -

Set your $IFS!

###################From CLI

#Script.sh < file

#Here document:

#script.sh <<EOF

Page 17: Bash 4

Scripting Basics

#Magic - #!/bin/bash

#Add -x to debug

#Comments - #

#; = EOL

#\ escapes EOL

#chmod +x

#echo $PATH

Page 18: Bash 4

Expansion

#VARIABLE=value

>All-caps not needed, just recommended

#echo ${VARIABLE}

>{} not always needed, but stops problems

>$ is necessary

#declare -i VARIABLE=value

>Specifies variable is an integer

Page 19: Bash 4

Expansion

#Command Substitution

>$(command)

>`command`

#Tilde

>~ = your home directory

>~user = user's home directory

#Wildcard globs

>man 7 glob

Page 20: Bash 4

Expansion

#Prevention

>\ escapes a single special character

>“” escapes all but $, ` and \

>'' escapes all special characters

Page 21: Bash 4

Loops

#for VAR in items in list; do commands; done

#exit status - $?

#test - [ condition ]

#while [ test ]; do commands; done

#until [ test ]; do commands; done

Page 22: Bash 4

Conditional Statements

#Operators

>&&

>||

#if [ test ]; then commands

#elif [ test ]; then commands

#else commands

#fi

Page 23: Bash 4

Conditional Statements

#case VAR in

>option)

>commands

>;;

>*)

>commands

>;;

>esac

Page 24: Bash 4

I/O Redirection

#Combining 1 and 2

>&>

>&>>

>2>&1

>>&2

#Channel 0 – STDIN

><

>-

Page 25: Bash 4

User Input

#Arguments

>$1, $2, ${10}, etc.

>$* or $@

>$#

Page 26: Bash 4

Bash 4_

Page 27: Bash 4

Bash4

#Associative arrays!

#Expansions ~ perl

#Coproc

#..._

Page 28: Bash 4

Associative Arrays

#Declare -A array

#array[element]=hello

#echo ${array[element]}

Page 29: Bash 4

New expansions

Substring

#${#:0:1}

Case modification

#${var^^}

#${var^}

#${var,}

#${var,,}

Page 30: Bash 4

Chopping strings

Get the extension of the current script

#${0##*.}

#${0#*.} (not correct)

Get the filename without extension

#${0%.*}

#${0%%.*} (not correct)

Page 31: Bash 4

Examples

Page 32: Bash 4

Questions

#CC BY-SA