UNIX Shell-Scripting Basics

71
UNIX Shell-Scripting Basics

description

jkm,

Transcript of UNIX Shell-Scripting Basics

  • UNIX Shell-Scripting Basics

  • AgendaWhat is a shell? A shell script?Introduction to bashRunning CommandsApplied Shell Programming

  • What is a shell?%

  • What is a shell?/bin/bash

  • What is a shell?#!/bin/bash

  • What is a shell?

  • What is a shell?Any ProgramBut there are a few popular shells

  • Bourne Shells/bin/sh/bin/bash Bourne-Again ShellSteve Bourne

  • Other Common ShellsC Shell (/bin/csh)Turbo C Shell (/bin/tcsh)Korn Shell (/bin/ksh)

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

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

  • What is a Shell Script?A Text FileWith InstructionsExecutable

  • What is a Shell Script?% cat > hello.sh
  • What is a Shell Script? A Text File% cat > hello.sh
  • An aside: Redirectioncat > /tmp/myfilecat >> /tmp/myfilecat 2> /tmp/myerrcat < /tmp/myinputcat &1012

  • What is a Shell Script? How To Run% cat > hello.sh
  • What is a Shell Script? What To Do% cat > hello.sh
  • What is a Shell Script? Executable% cat > hello.sh
  • What is a Shell Script? Running it% cat > hello.sh
  • Finding the program: PATH% ./hello.shecho vs. /usr/bin/echo% echo $PATH /bin:/usr/bin:/usr/local/bin: /home/borwicjh/bin% which echo /usr/bin/echo

  • Variables and the Environment% hello.shbash: hello.sh: Command not found% PATH=$PATH:.% hello.shHello, world

  • An aside: Quoting% echo $USER$USER% echo $USERborwicjh% echo \% echo deacnet\\sctdeacnet\sct% echo \\

  • Variables and the Environment% env[variables passed to sub-programs]% NEW_VAR=Yes% echo $NEW_VARYes% env[PATH but not NEW_VAR]% export NEW_VAR% env[PATH and NEW_VAR]

  • Welcome to Shell Scripting!

  • How to Learnmanman bashman catman manman kman k manualLearning the Bash Shell, 2nd Ed.Bash Reference Cardshttp://www.tldp.org/LDP/abs/html/

  • Introduction to bash

  • Continuing Lines: \% echo This \Is \ A \Very \Long \ Command LineThis Is A Very Long Command Line%

  • Exit Status$?0 is True

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

  • Exit Status: exit% cat > test.sh
  • Logic: test% test 1 -lt 10% echo $?0% test 1 == 10% echo $?1

  • Logic: testtest[ ][ 1 lt 10 ] [[ ]][[ this string =~ this ]](( ))(( 1 < 10 ))

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

  • An aside: $(( )) for Math% echo $(( 1 + 2 ))3% echo $(( 2 * 3 ))6% echo $(( 1 / 3 ))0

  • Logic: ifif somethingthen :# elif a contraction of else if:elif something-elsethen :elsethen :fi

  • Logic: ifif [ $USER eq borwicjh ]then :# elif a contraction of else if:elif ls /etc/oratabthen :elsethen :fi

  • Logic: if# see if a file existsif [ -e /etc/passwd ]then echo /etc/passwd existselse echo /etc/passwd not found!fi

  • Logic: forfor i in 1 2 3do echo $idone

  • Logic: forfor i in /*do echo Listing $i: ls -l $i readdone

  • Logic: forfor i in /*do echo Listing $i: ls -l $i readdone

  • Logic: forfor i in /*do echo Listing $i: ls -l $i readdone

  • Logic: C-style for

    for (( expr1 ; expr2 ; expr3 ))do listdone

  • Logic: C-style forLIMIT=10for (( a=1 ; a
  • Logic: while

    while somethingdo :

    done

  • Logic: whilea=0; LIMIT=10while [ "$a" -lt "$LIMIT" ]do echo -n "$a a=$(( a + 1 ))done

  • CountersCOUNTER=0while [ -e $FILE.COUNTER ]do COUNTER=$(( COUNTER + 1))done

    Note: race condition

  • Reusing Code: Sourcing% cat > /path/to/my/passwords
  • 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

  • It takes a long time to become a bash guru

  • Running Programs

  • Reasons for Running ProgramsCheck Return Code$?Get Job OutputOUTPUT=`echo Hello`OUTPUT=$(echo Hello)Send Output SomewhereRedirection: Pipes

  • PipesLots of Little Tools

    echo Hello | \ wc -c

  • Email Notification% echo Message | \mail s Heres your message \ [email protected]

  • Dates% DATESTRING=`date +%Y%m%d`% echo $DATESTRING20060125% man date

  • FTP the Hard Wayftp n u server.wfu.edu
  • FTP with wgetwget \ ftp://user:[email protected]/filewget r \ ftp://user:[email protected]/dir/

  • FTP with curlcurl T upload-file \ -u username:password \ ftp://server.wfu.edu/dir/file

  • Searching: grep% grep rayra /etc/passwd% grep r rayra /etc% grep r RAYRA /etc% grep ri RAYRA /etc% grep rli rayra /etc

  • 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

  • Searching: locate% locate .lis[files with .lis in path]% locate log[also finds /var/log/messages]

  • Applied Shell Programming

  • Make Your Life EasierTAB completionControl+Rhistorycd -Study a UNIX Editor

  • pushd/popd% cd /tmp% pushd /var/log/var/log /tmp% cd ..% pwd/var% popd/tmp

  • Monitoring processespsps efps u oracleps C sshdman ps

  • DOS Mode Files#!/usr/bin/bash^MFTP transfer in ASCII, ordos2unix infile > outfile

  • sqlplusJOB=ZZZTESTPARAMS=ZZZTEST_PARAMSPARAMS_USER=BORWICJHsqlplus $BANNER_USER/$BANNER_PW
  • sqlplussqlplus $USER/$PASS @$FILE_SQL \ $ARG1 $ARG2 $ARG3if [ $? ne 0 ]then exit 1fiif [ -e /file/sql/should/create ]then [use SQL-created file]fi

    Ask Amy Lamy!

  • Passing Arguments% cat > test.sh
  • 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

  • Scheduling Jobs% crontab -l0 0 * * * daily-midnight-job.sh0 * * * * hourly-job.sh* * * * * every-minute.sh0 1 * * 0 1AM-on-sunday.sh% EDITOR=vi crontab e% man 5 crontab

  • Other Questions?Shells and Shell ScriptsbashRunning Commandsbash and Banner in Practice

    First match winsSecurity