unix shell scripting basics l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
UNIX Shell-Scripting Basics PowerPoint Presentation
Download Presentation
UNIX Shell-Scripting Basics

Loading in 2 Seconds...

play fullscreen
1 / 71

UNIX Shell-Scripting Basics - PowerPoint PPT Presentation


  • 219 Views
  • Uploaded on

UNIX Shell-Scripting Basics. Agenda. What is a shell? A shell script? Introduction to bash Running Commands Applied Shell Programming. What is a shell?. ▌. %. What is a shell?. /bin/bash. What is a shell?. #!/bin/bash. INPUT. shell. OUTPUT. ERROR. What is a shell?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'UNIX Shell-Scripting Basics' - nola


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
unix shell scripting basics

UNIX Shell-Scripting Basics

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

agenda
Agenda
  • What is a shell? A shell script?
  • Introduction to bash
  • Running Commands
  • Applied Shell Programming

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell
What is a shell?

%

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell4
What is a shell?

/bin/bash

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell5
What is a shell?

#!/bin/bash

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell6

INPUT

shell

OUTPUT

ERROR

What is a shell?

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell7
What is a shell?
  • Any Program
  • But there are a few popular shells…

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

bourne shells
Bourne Shells
  • /bin/sh
  • /bin/bash“Bourne-Again Shell”

Steve Bourne

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

other common shells
Other Common Shells
  • C Shell (/bin/csh)
  • Turbo C Shell (/bin/tcsh)
  • Korn Shell (/bin/ksh)

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

an aside what do i mean by bin
An aside: What do I mean by /bin ?
  • C Shell (/bin/csh)
  • Turbo C Shell (/bin/tcsh)
  • Korn Shell (/bin/ksh)

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

an aside what do i mean by bin11
An aside: What do I mean by /bin ?
  • /bin, /usr/bin, /usr/local/bin
  • /sbin, /usr/sbin, /usr/local/sbin
  • /tmp
  • /dev
  • /home/borwicjh

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script
What is a Shell Script?
  • A Text File
  • With Instructions
  • Executable

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script13
What is a Shell Script?

% cat > hello.sh <<MY_PROGRAM

#!/bin/sh

echo ‘Hello, world’

MY_PROGRAM

% chmod +x hello.sh

% ./hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script a text file
What is a Shell Script? A Text File

% cat > hello.sh <<MY_PROGRAM

#!/bin/sh

echo ‘Hello, world’

MY_PROGRAM

% chmod +x hello.sh

% ./hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

an aside redirection

INPUT

OUTPUT

ERROR

env

An aside: Redirection
  • cat > /tmp/myfile
  • cat >> /tmp/myfile
  • cat 2> /tmp/myerr
  • cat < /tmp/myinput
  • cat <<INPUTSome inputINPUT
  • cat > /tmp/x 2>&1

0

1

2

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script how to run
What is a Shell Script? How To Run

% cat > hello.sh <<MY_PROGRAM

#!/bin/sh

echo ‘Hello, world’

MY_PROGRAM

% chmod +x hello.sh

% ./hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script what to do
What is a Shell Script? What To Do

% cat > hello.sh <<MY_PROGRAM

#!/bin/sh

echo ‘Hello, world’

MY_PROGRAM

% chmod +x hello.sh

% ./hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script executable
What is a Shell Script? Executable

% cat > hello.sh <<MY_PROGRAM

#!/bin/sh

echo ‘Hello, world’

MY_PROGRAM

% chmod +x hello.sh

% ./hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

what is a shell script running it
What is a Shell Script? Running it

% cat > hello.sh <<MY_PROGRAM

#!/bin/sh

echo ‘Hello, world’

MY_PROGRAM

% chmod +x hello.sh

% ./hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

finding the program path
Finding the program: PATH
  • % ./hello.sh
  • echo vs. /usr/bin/echo
  • % echo $PATH/bin:/usr/bin:/usr/local/bin:/home/borwicjh/bin
  • % which echo/usr/bin/echo

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

variables and the environment
Variables and the Environment

% hello.sh

bash: hello.sh: Command not found

% PATH=“$PATH:.”

% hello.sh

Hello, world

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

an aside quoting
An aside: Quoting

% echo ‘$USER’

$USER

% echo “$USER”

borwicjh

% echo “\””

% echo “deacnet\\sct”

deacnet\sct

% echo ‘\”’

\”

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

variables and the environment23
Variables and the Environment

% env

[…variables passed to sub-programs…]

% NEW_VAR=“Yes”

% echo $NEW_VAR

Yes

% env

[…PATH but not NEW_VAR…]

% export NEW_VAR

% env

[…PATH and NEW_VAR…]

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

welcome to shell scripting
Welcome to Shell Scripting!

Input, Output, and Error

Shebang!

The Environment

PATH

chmod

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

how to learn
How to Learn
  • man
    • man bash
    • man cat
    • man man
  • man –k
    • man –k manual
  • Learning the Bash Shell, 2nd Ed.
  • “Bash Reference” Cards
  • http://www.tldp.org/LDP/abs/html/

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

introduction to bash

Introduction to bash

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

continuing lines
Continuing Lines: \

% echo This \

Is \

A \

Very \

Long \

Command Line

This Is A Very Long Command Line

%

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

exit status
Exit Status
  • $?
  • 0 is True

% ls /does/not/exist

% echo $?

1

% echo $?

0

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

exit status exit
Exit Status: exit

% cat > test.sh <<_TEST_

exit 3

_TEST_

% chmod +x test.sh

% ./test.sh

% echo $?

3

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic test
Logic: test

% test 1 -lt 10

% echo $?

0

% test 1 == 10

% echo $?

1

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic test31
Logic: test
  • test
  • [ ]
    • [ 1 –lt 10 ]
  • [[ ]]
    • [[ “this string” =~ “this” ]]
  • (( ))
    • (( 1 < 10 ))

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic test32
Logic: test
  • [ -f /etc/passwd ]
  • [ ! –f /etc/passwd ]
  • [ -f /etc/passwd –a –f /etc/shadow ]
  • [ -f /etc/passwd –o –f /etc/shadow ]

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

an aside for math
An aside: $(( )) for Math

% echo $(( 1 + 2 ))

3

% echo $(( 2 * 3 ))

6

% echo $(( 1 / 3 ))

0

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic if
Logic: if

if something

then

:

# “elif” a contraction of “else if”:

elif something-else

then

:

else

then

:

fi

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic if35
Logic: if

if [ $USER –eq “borwicjh” ]

then

:

# “elif” a contraction of “else if”:

elif ls /etc/oratab

then

:

else

then

:

fi

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic if36
Logic: if

# see if a file exists

if [ -e /etc/passwd ]

then

echo “/etc/passwd exists”

else

echo “/etc/passwd not found!”

fi

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic for
Logic: for

for i in 1 2 3

do

echo $i

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic for38
Logic: for

for i in /*

do

echo “Listing $i:”

ls -l $i

read

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic for39
Logic: for

for i in /*

do

echo “Listing $i:”

ls -l $i

read

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic for40
Logic: for

for i in /*

do

echo “Listing $i:”

ls -l $i

read

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic c style for
Logic: C-style for

for (( expr1 ;

expr2 ;

expr3 ))

do

list

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic c style for42
Logic: C-style for

LIMIT=10

for (( a=1 ;

a<=LIMIT ;

a++ ))

do

echo –n “$a ”

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic while
Logic: while

while something

do

:

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

logic while44
Logic: while

a=0; LIMIT=10

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

do

echo -n "$a ”

a=$(( a + 1 ))

done

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

counters
Counters

COUNTER=0

while [ -e “$FILE.COUNTER” ]

do

COUNTER=$(( COUNTER + 1))

done

  • Note: race condition

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

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_USER

sct

%

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

variable manipulation
Variable 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

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

slide48
It takes a long time to become a bash guru…

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

running programs

Running Programs

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

reasons for running programs
Reasons for Running Programs
  • Check Return Code
    • $?
  • Get Job Output
    • OUTPUT=`echo “Hello”`
    • OUTPUT=$(echo “Hello”)
  • Send Output Somewhere
    • Redirection: <, >
    • Pipes

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

pipes

INPUT

OUTPUT

ERROR

ERROR

OUTPUT

INPUT

echo

wc

A Pipe!

0

0

1

1

2

2

Pipes
  • Lots of Little Tools

echo “Hello” | \

wc -c

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

email notification
Email Notification

% echo “Message” | \

mail –s “Here’s your message” \

borwicjh@wfu.edu

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

dates
Dates

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

% echo $DATESTRING

20060125

% man date

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

ftp the hard way
FTP the Hard Way

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

user username password

put FILE

_FTP_

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

ftp with wget
FTP with wget
  • wget \ftp://user:pass@server.wfu.edu/file
  • wget –r \ftp://user:pass@server.wfu.edu/dir/

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

ftp with curl
FTP with curl

curl –T upload-file \

-u username:password \

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

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

searching grep
Searching: grep

% grep rayra /etc/passwd

% grep –r rayra /etc

% grep –r RAYRA /etc

% grep –ri RAYRA /etc

% grep –rli rayra /etc

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

searching find
Searching: find

% find /home/borwicjh \

-name ‘*.lis’

[all files matching *.lis]

% find /home/borwicjh \

-mtime -1 –name ‘*.lis’

[*.lis, if modified within 24h]

% man find

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

searching locate
Searching: locate

% locate .lis

[files with .lis in path]

% locate log

[also finds “/var/log/messages”]

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

applied shell programming

Applied Shell Programming

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

make your life easier
Make Your Life Easier
  • TAB completion
  • Control+R
  • history
  • cd -
  • Study a UNIX Editor

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

pushd popd
pushd/popd

% cd /tmp

% pushd /var/log

/var/log /tmp

% cd ..

% pwd

/var

% popd

/tmp

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

monitoring processes
Monitoring processes
  • ps
  • ps –ef
  • ps –u oracle
  • ps –C sshd
  • man ps

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

dos mode files
“DOS” Mode Files
  • #!/usr/bin/bash^M
  • FTP transfer in ASCII, or
  • dos2unix infile > outfile

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

sqlplus
sqlplus

JOB=“ZZZTEST”

PARAMS=“ZZZTEST_PARAMS”

PARAMS_USER=“BORWICJH”

sqlplus $BANNER_USER/$BANNER_PW << _EOF_

set serveroutput on

set sqlprompt ""

EXECUTE WF_SATURN.FZ_Get_Parameters('$JOB', '$PARAMS', '$PARAMS_USER');

_EOF_

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

sqlplus66
sqlplus

sqlplus $USER/$PASS @$FILE_SQL \

$ARG1 $ARG2 $ARG3

if [ $? –ne 0 ]

then

exit 1

fi

if [ -e /file/sql/should/create ]

then

[…use SQL-created file…]

fi

  • Ask Amy Lamy! 

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

passing arguments
Passing Arguments

% cat > test.sh <<_TEST_

echo “Your name is \$1 \$2”

_TEST_

% chmod +x test.sh

% ./test.sh John Borwick ignore-this

Your name is John Borwick

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

inb job submission template
INB Job Submission Template

$1: user ID

$2: password

$3: one-up number

$4: process name

$5: printer name

% /path/to/your/script $UI $PW \

$ONE_UP $JOB $PRNT

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

scheduling jobs
Scheduling Jobs

% crontab -l

0 0 * * * daily-midnight-job.sh

0 * * * * hourly-job.sh

* * * * * every-minute.sh

0 1 * * 0 1AM-on-sunday.sh

% EDITOR=vi crontab –e

% man 5 crontab

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

slide70

It's Over!

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt

other questions
Other Questions?
  • Shells and Shell Scripts
  • bash
  • Running Commands
  • bash and Banner in Practice

www.wfu.edu/~borwicjh/presentations/UNIX%20Shell-Scripting%20Basics.ppt