UNIX Shell-Scripting Basics
-
Upload
guillaume-jair -
Category
Documents
-
view
46 -
download
1
description
Transcript of UNIX Shell-Scripting Basics
![Page 1: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/1.jpg)
UNIX Shell-Scripting Basics
![Page 2: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/2.jpg)
Agenda
What is a shell? A shell script?Introduction to bashRunning CommandsApplied Shell Programming
![Page 3: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/3.jpg)
What is a shell?
%▌
![Page 4: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/4.jpg)
What is a shell?
/bin/bash
![Page 5: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/5.jpg)
What is a shell?
#!/bin/bash
![Page 6: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/6.jpg)
What is a shell?
INPUT
shell
OUTPUT ERROR
![Page 7: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/7.jpg)
What is a shell?
Any ProgramBut there are a few popular shells…
![Page 8: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/8.jpg)
Bourne Shells
/bin/sh/bin/bash
“Bourne-Again Shell”
Steve Bourne
![Page 9: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/9.jpg)
Other Common Shells
C Shell (/bin/csh)Turbo C Shell (/bin/tcsh)Korn Shell (/bin/ksh)
![Page 10: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/10.jpg)
An aside: What do I mean by /bin ?
C Shell (/bin/csh)Turbo C Shell (/bin/tcsh)Korn Shell (/bin/ksh)
![Page 11: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/11.jpg)
An aside: What do I mean by /bin ?
/bin, /usr/bin, /usr/local/bin/sbin, /usr/sbin, /usr/local/sbin/tmp/dev/home/borwicjh
![Page 12: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/12.jpg)
What is a Shell Script?
A Text FileWith InstructionsExecutable
![Page 13: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/13.jpg)
What is a Shell Script?
% cat > hello.sh <<MY_PROGRAM#!/bin/shecho ‘Hello, world’MY_PROGRAM% chmod +x hello.sh% ./hello.shHello, world
![Page 14: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/14.jpg)
What is a Shell Script? A Text File
% cat > hello.sh <<MY_PROGRAM#!/bin/shecho ‘Hello, world’MY_PROGRAM% chmod +x hello.sh% ./hello.shHello, world
![Page 15: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/15.jpg)
An aside: Redirection
cat > /tmp/myfilecat >> /tmp/myfilecat 2> /tmp/myerrcat < /tmp/myinputcat <<INPUTSome inputINPUT
cat > /tmp/x 2>&1
INPUT
env
OUTPUT ERROR
0
1 2
![Page 16: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/16.jpg)
What is a Shell Script? How To Run
% cat > hello.sh <<MY_PROGRAM#!/bin/shecho ‘Hello, world’MY_PROGRAM% chmod +x hello.sh% ./hello.shHello, world
![Page 17: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/17.jpg)
What is a Shell Script? What To Do
% cat > hello.sh <<MY_PROGRAM#!/bin/shecho ‘Hello, world’MY_PROGRAM% chmod +x hello.sh% ./hello.shHello, world
![Page 18: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/18.jpg)
What is a Shell Script? Executable
% cat > hello.sh <<MY_PROGRAM#!/bin/shecho ‘Hello, world’MY_PROGRAM% chmod +x hello.sh% ./hello.shHello, world
![Page 19: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/19.jpg)
What is a Shell Script? Running it
% cat > hello.sh <<MY_PROGRAM#!/bin/shecho ‘Hello, world’MY_PROGRAM% chmod +x hello.sh% ./hello.shHello, world
![Page 20: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/20.jpg)
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
![Page 21: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/21.jpg)
Variables and the Environment
% hello.shbash: hello.sh: Command not found
% PATH=“$PATH:.”% hello.shHello, world
![Page 22: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/22.jpg)
An aside: Quoting
% echo ‘$USER’$USER% echo “$USER”borwicjh% echo “\”””% echo “deacnet\\sct”deacnet\sct% echo ‘\”’\”
![Page 23: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/23.jpg)
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…]
![Page 24: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/24.jpg)
Welcome to Shell Scripting!
![Page 25: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/25.jpg)
How to Learn
manman bashman catman man
man –kman –k manual
Learning the Bash Shell, 2nd Ed.“Bash Reference” Cardshttp://www.tldp.org/LDP/abs/html/
![Page 26: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/26.jpg)
Introduction to bash
![Page 27: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/27.jpg)
Continuing Lines: \
% echo This \Is \ A \Very \Long \ Command LineThis Is A Very Long Command Line%
![Page 28: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/28.jpg)
Exit Status
$?0 is True
% ls /does/not/exist% echo $?1% echo $?0
![Page 29: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/29.jpg)
Exit Status: exit
% cat > test.sh <<_TEST_exit 3_TEST_% chmod +x test.sh% ./test.sh% echo $?3
![Page 30: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/30.jpg)
Logic: test
% test 1 -lt 10% echo $?0% test 1 == 10% echo $?1
![Page 31: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/31.jpg)
Logic: test
test[ ]
[ 1 –lt 10 ] [[ ]]
[[ “this string” =~ “this” ]](( ))
(( 1 < 10 ))
![Page 32: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/32.jpg)
Logic: test
[ -f /etc/passwd ][ ! –f /etc/passwd ][ -f /etc/passwd –a –f /etc/shadow ][ -f /etc/passwd –o –f /etc/shadow ]
![Page 33: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/33.jpg)
An aside: $(( )) for Math
% echo $(( 1 + 2 ))3% echo $(( 2 * 3 ))6% echo $(( 1 / 3 ))0
![Page 34: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/34.jpg)
Logic: if
if somethingthen :# “elif” a contraction of “else if”:elif something-elsethen :elsethen :fi
![Page 35: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/35.jpg)
Logic: if
if [ $USER –eq “borwicjh” ]then :# “elif” a contraction of “else if”:elif ls /etc/oratabthen :elsethen :fi
![Page 36: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/36.jpg)
Logic: if
# see if a file existsif [ -e /etc/passwd ]then echo “/etc/passwd exists”else echo “/etc/passwd not found!”fi
![Page 37: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/37.jpg)
Logic: for
for i in 1 2 3do echo $idone
![Page 38: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/38.jpg)
Logic: for
for i in /*do echo “Listing $i:” ls -l $i readdone
![Page 39: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/39.jpg)
Logic: for
for i in /*do echo “Listing $i:” ls -l $i readdone
![Page 40: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/40.jpg)
Logic: for
for i in /*do echo “Listing $i:” ls -l $i readdone
![Page 41: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/41.jpg)
Logic: C-style for
for (( expr1 ; expr2 ; expr3 ))do listdone
![Page 42: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/42.jpg)
Logic: C-style for
LIMIT=10for (( a=1 ; a<=LIMIT ; a++ ))do echo –n “$a ”done
![Page 43: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/43.jpg)
Logic: while
while somethingdo :
done
![Page 44: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/44.jpg)
Logic: while
a=0; LIMIT=10while [ "$a" -lt "$LIMIT" ]do echo -n "$a ” a=$(( a + 1 ))done
![Page 45: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/45.jpg)
Counters
COUNTER=0while [ -e “$FILE.COUNTER” ]do COUNTER=$(( COUNTER + 1))done
Note: race condition
![Page 46: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/46.jpg)
Reusing Code: “Sourcing”
% cat > /path/to/my/passwords <<_PW_FTP_USER=“sct”_PW_% echo $FTP_USER
% . /path/to/my/passwords% echo $FTP_USERsct%
![Page 47: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/47.jpg)
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
![Page 48: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/48.jpg)
It takes a long time to become a bash guru…
![Page 49: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/49.jpg)
Running Programs
![Page 50: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/50.jpg)
Reasons for Running Programs
Check Return Code$?
Get Job OutputOUTPUT=`echo “Hello”`OUTPUT=$(echo “Hello”)
Send Output SomewhereRedirection: <, >Pipes
![Page 51: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/51.jpg)
Pipes
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 53: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/53.jpg)
Dates
% DATESTRING=`date +%Y%m%d`% echo $DATESTRING20060125% man date
![Page 54: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/54.jpg)
FTP the Hard Way
ftp –n –u server.wfu.edu <<_FTP_user username passwordput FILE_FTP_
![Page 56: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/56.jpg)
FTP with curl
curl –T upload-file \ -u username:password \ ftp://server.wfu.edu/dir/file
![Page 57: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/57.jpg)
Searching: grep
% grep rayra /etc/passwd% grep –r rayra /etc% grep –r RAYRA /etc% grep –ri RAYRA /etc% grep –rli rayra /etc
![Page 58: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/58.jpg)
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
![Page 59: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/59.jpg)
Searching: locate
% locate .lis[files with .lis in path]% locate log[also finds “/var/log/messages”]
![Page 60: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/60.jpg)
Applied Shell Programming
![Page 61: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/61.jpg)
Make Your Life Easier
TAB completionControl+Rhistorycd -Study a UNIX Editor
![Page 62: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/62.jpg)
pushd/popd
% cd /tmp% pushd /var/log/var/log /tmp% cd ..% pwd/var% popd/tmp
![Page 63: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/63.jpg)
Monitoring processes
psps –efps –u oracleps –C sshdman ps
![Page 64: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/64.jpg)
“DOS” Mode Files
#!/usr/bin/bash^MFTP transfer in ASCII, ordos2unix infile > outfile
![Page 65: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/65.jpg)
sqlplus
JOB=“ZZZTEST”PARAMS=“ZZZTEST_PARAMS”PARAMS_USER=“BORWICJH”sqlplus $BANNER_USER/$BANNER_PW << _EOF_set serveroutput onset sqlprompt ""
EXECUTE WF_SATURN.FZ_Get_Parameters('$JOB', '$PARAMS', '$PARAMS_USER');
_EOF_
![Page 66: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/66.jpg)
sqlplus
sqlplus $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!
![Page 67: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/67.jpg)
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
![Page 68: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/68.jpg)
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
![Page 69: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/69.jpg)
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
![Page 70: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/70.jpg)
![Page 71: UNIX Shell-Scripting Basics](https://reader035.fdocuments.us/reader035/viewer/2022070400/56813509550346895d9c5a6f/html5/thumbnails/71.jpg)
Other Questions?
Shells and Shell ScriptsbashRunning Commandsbash and Banner in Practice