bash lamp server

50
UNIX Shell-Scripting UNIX Shell-Scripting With focus on bash BINP14 Björn Canbäck

Upload: -

Post on 20-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

bash lamp server

TRANSCRIPT

Page 1: bash lamp server

UNIX Shell-ScriptingUNIX Shell-Scripting

With focus on bash

BINP14 Björn Canbäck

Page 2: bash lamp server

OutlineOutline

● What is a shell? A shell script?● Introduction to bash● Running Commands

BINP14 Björn Canbäck

Page 3: bash lamp server

What is a shell?What is a shell?

BINP14 Björn Canbäck

A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands.

Source: http://en.wikipedia.org/wiki/Unix_shell

Page 4: bash lamp server

What is a shell?What is a shell?

Input (STDIN)

shell

output (STDOUT) error (STDERR)

Page 5: bash lamp server

Common ShellsCommon Shells

● Bash (/bin/bash) Bourne again shell● C Shell (/bin/csh)● Turbo C Shell (/bin/tcsh)● Korn Shell (/bin/ksh)

BINP14 Björn Canbäck

Page 6: bash lamp server

What is bin ?What is bin ?

● /bin● /usr/bin● /usr/local/bin● /home/bjorn/bin

BINP14 Björn Canbäck

Page 7: bash lamp server

What is a shell script?What is a shell script?

● A text file● With instructions● Executable

BINP14 Björn Canbäck

Page 8: bash lamp server

What is a Shell Script?What is a Shell Script?

% cat > hello.sh <<HERE

#!/bin/sh

echo 'Hello world!'

HERE

% chmod +x hello.sh

% ./hello.sh

Hello world!

Page 9: bash lamp server

What is a Shell Script? What is a Shell Script? A Text FileA Text File

% cat > hello.sh <<HERE

#!/bin/sh

echo 'Hello world!'

HERE

% chmod +x hello.sh

% ./hello.sh

Hello world!

BINP14 Björn Canbäck

Page 10: bash lamp server

What is a Shell Script? What is a Shell Script? How To RunHow To Run

% cat > hello.sh <<HERE

#!/bin/sh

echo 'Hello world!'

HERE

% chmod +x hello.sh

% ./hello.sh

Hello world!

BINP14 Björn Canbäck

Page 11: bash lamp server

What is a Shell Script? What is a Shell Script? What To DoWhat To Do

% cat > hello.sh <<HERE

#!/bin/sh

echo 'Hello world!'

HERE

% chmod +x hello.sh

% ./hello.sh

Hello world!

BINP14 Björn Canbäck

Page 12: bash lamp server

What is a Shell Script? What is a Shell Script? ExecutableExecutable

% cat > hello.sh <<HERE

#!/bin/sh

echo 'Hello world!'

HERE

% chmod +x hello.sh

% ./hello.sh

Hello world!

BINP14 Björn Canbäck

Page 13: bash lamp server

What is a Shell Script? What is a Shell Script? Running itRunning it

% cat > hello.sh <<HERE

#!/bin/sh

echo 'Hello world'

HERE

% chmod +x hello.sh

% ./hello.sh

Hello world!

BINP14 Björn Canbäck

Page 14: bash lamp server

Finding the program: PATHFinding the program: PATH

● % ./hello.sh● % echo $PATH/bin:/usr/bin:/usr/local/bin:/home/bjorn/bin

● % which echo/usr/bin/echo

BINP14 Björn Canbäck

Page 15: bash lamp server

Variables and the environmentVariables and the environment

% hello.sh

bash: hello.sh: Command not found

% PATH=“$PATH:.”

% hello.sh

Hello, world

BINP14 Björn Canbäck

Page 16: bash lamp server

RedirectionRedirection

echo hej > test.txt

echo “ hej” >> test.txt

Expert users only: cat < test.txt

cat <<INPUTSome inputINPUT

test.sh 2> myError

text.sh> myErrorAndOut 2>&1

BINP14 Björn Canbäck

Expert users only:

error 2

input 0

program

output 1

Page 17: bash lamp server

QuotingQuoting

% echo '$USER'$USER% echo “$USER”bjorn% echo $USERbjorn% echo \””% echo \>>

BINP14 Björn Canbäck

Page 18: bash lamp server

How to learnHow to learn

● man● man bash● man cat● man man

● Learning the Bash Shell, 2nd Ed.● “Bash Reference” Cards

Page 19: bash lamp server

Continuing lines: \Continuing lines: \

% echo This \Is \ A \Very \Long \ Command LineThis Is A Very Long Command Line%

BINP14 Björn Canbäck

Page 20: bash lamp server

Make Your Life EasierMake Your Life Easier

● TAB completion● Control+R● Control+S

Page 21: bash lamp server

PipesPipes

● Lots of Little Tools

echo “Hello” | \

wc -c

INPUT

echo

OUTPUT ERROR

0

1 2

INPUT

wc

OUTPUT ERROR

0

1 2

A Pipe!

Page 22: bash lamp server

Following is only if you want to learn more

Page 23: bash lamp server

Exit status (expert users)Exit status (expert users)

● $?● 0 is True

% ls /does/not/exist% echo $?1% echo $?0

BINP14 Björn Canbäck

Page 24: bash lamp server

Exit status: (expert users)Exit status: (expert users)

% cat > test.sh <<_TEST_

exit 3

_TEST_

% chmod +x test.sh

% ./test.sh

% echo $?

3

Page 25: bash lamp server

Logic: test (expert users)Logic: test (expert users)

% test 1 -lt 10

% echo $?

0

% test 1 == 10

% echo $?

1

Page 26: bash lamp server

Logic: test (expert users)Logic: test (expert users)

● test● [ ]

● [ 1 –lt 10 ] ● [[ ]]

● [[ “this string” =~ “this” ]]● (( ))

● (( 1 < 10 ))

Page 27: bash lamp server

Logic: test (expert users)Logic: test (expert users)

● [ -f /etc/passwd ]● [ ! –f /etc/passwd ]● [ -f /etc/passwd –a –f /etc/shadow ]● [ -f /etc/passwd –o –f /etc/shadow ]

Page 28: bash lamp server

An aside: An aside: $(( ))$(( )) for Math (expert users) for Math (expert users)

% echo $(( 1 + 2 ))

3

% echo $(( 2 * 3 ))

6

% echo $(( 1 / 3 ))

0

Page 29: bash lamp server

Logic: if (expert users)Logic: if (expert users)

if somethingthen :# “elif” a contraction of “else if”:elif something-elsethen :elsethen :fi

Page 30: bash lamp server

Logic: if (expert users)Logic: if (expert users)

if [ $USER –eq “borwicjh” ]then :# “elif” a contraction of “else if”:elif ls /etc/oratabthen :elsethen :fi

Page 31: bash lamp server

Logic: if (expert users)Logic: if (expert users)

# see if a file exists

if [ -e /etc/passwd ]

then

echo “/etc/passwd exists”

else

echo “/etc/passwd not found!”

fi

Page 32: bash lamp server

Logic: for (expert users)Logic: for (expert users)

for i in 1 2 3

do

echo $i

done

Page 33: bash lamp server

Logic: for (expert users)Logic: for (expert users)

for i in /*

do

echo “Listing $i:”

ls -l $i

read

done

Page 34: bash lamp server

Logic: for (expert users)Logic: for (expert users)

for i in /*

do

echo “Listing $i:”

ls -l $i

read

done

Page 35: bash lamp server

Logic: for (expert users)Logic: for (expert users)

for i in /*

do

echo “Listing $i:”

ls -l $i

read

done

Page 36: bash lamp server

Logic: C-style for (expert users)Logic: C-style for (expert users)

for (( expr1 ;

expr2 ;

expr3 ))

do

list

done

Page 37: bash lamp server

Logic: C-style for (expert users)Logic: C-style for (expert users)

LIMIT=10

for (( a=1 ;

a<=LIMIT ;

a++ ))

do

echo –n “$a ”

done

Page 38: bash lamp server

Logic: whileLogic: while

while something

do

:

done

Page 39: bash lamp server

Logic: whileLogic: while

a=0; LIMIT=10

while [ "$a" -lt "$LIMIT" ]

do

echo -n "$a ”

a=$(( a + 1 ))

done

Page 40: bash lamp server

CountersCounters

COUNTER=0

while [ -e “$FILE.COUNTER” ]

do

COUNTER=$(( COUNTER + 1))

done

● Note: race condition

Page 41: bash lamp server

Reusing Code: “Sourcing”Reusing Code: “Sourcing”

% cat > /path/to/my/passwords <<_PW_FTP_USER=“sct”_PW_% echo $FTP_USER

% . /path/to/my/passwords% echo $FTP_USERsct%

Page 42: bash lamp server

Variable ManipulationVariable Manipulation

% FILEPATH=/path/to/my/output.lis% echo $FILEPATH/path/to/my/output.lis% echo ${FILEPATH%.lis}/path/to/my/output% echo ${FILEPATH#*/}path/to/my/output.lis% echo ${FILEPATH##*/}output.lis

Page 43: bash lamp server

Running ProgramsRunning Programs

Page 44: bash lamp server

Reasons for Running ProgramsReasons for Running Programs

● Check Return Code● $?

● Get Job Output● OUTPUT=`echo “Hello”`● OUTPUT=$(echo “Hello”)

● Send Output Somewhere● Redirection: <, >● Pipes

Page 45: bash lamp server

Email NotificationEmail Notification

% echo “Message” | \

mail –s “Here’s your message” \

[email protected]

Page 46: bash lamp server

DatesDates

% DATESTRING=`date +%Y%m%d`

% echo $DATESTRING

20060125

% man date

Page 47: bash lamp server

FTP the Hard WayFTP the Hard Way

ftp –n –u server.wfu.edu <<_FTP_

user username password

put FILE

_FTP_

Page 48: bash lamp server

FTP with FTP with wgetwget

● wget \ftp://user:[email protected]/file

● wget –r \ftp://user:[email protected]/dir/

Page 49: bash lamp server

FTP with FTP with curlcurl

curl –T upload-file \

-u username:password \

ftp://server.wfu.edu/dir/file

Page 50: bash lamp server

Searching: Searching: findfind

% find /home/borwicjh \

-name ‘*.lis’

[all files matching *.lis]

% find /home/borwicjh \

-mtime -1 –name ‘*.lis’

[*.lis, if modified within 24h]

% man find