Unix Shell Scripting Basics
Unix Shell Scripting Basics — Presentation Transcript
- UNIX Shell-Scripting Basics
- Agenda
- What is a shell? A shell script?
- Applied Shell Programming
- What is a shell?
▌
- What is a shell?
- What is a shell?
- What is a shell? INPUT shell OUTPUT ERROR
- What is a shell?
- But there are a few popular shells…
- Bourne Shells
- /bin/bash “Bourne-Again Shell”
Steve Bourne
- Other Common Shells
- Turbo C Shell ( /bin/tcsh )
- An aside: What do I mean by /bin ?
- Turbo C Shell ( /bin/tcsh )
- An aside: What do I mean by /bin ?
- /bin, /usr/bin, /usr/local/bin
- /sbin, /usr/sbin, /usr/local/sbin
- What is a Shell Script?
- What is a Shell Script?
- % cat > hello.sh <<MY_PROGRAM
- What is a Shell Script? A Text File
- % cat > hello.sh <<MY_PROGRAM
- An aside: Redirection
- cat <<INPUT Some input INPUT
0 1 2 INPUT env OUTPUT ERROR
- What is a Shell Script? How To Run
- % cat > hello.sh <<MY_PROGRAM
- What is a Shell Script? What To Do
- % cat > hello.sh <<MY_PROGRAM
- What is a Shell Script? Executable
- % cat > hello.sh <<MY_PROGRAM
- What is a Shell Script? Running it
- % cat > hello.sh <<MY_PROGRAM
- Finding the program: PATH
- % echo $PATH /bin:/usr/bin:/usr/local/bin: /home/borwicjh/bin
- % which echo /usr/bin/echo
- Variables and the Environment
- bash: hello.sh: Command not found
- An aside: Quoting
- Variables and the Environment
- […variables passed to sub-programs…]
- Welcome to Shell Scripting! Shebang! The Environment PATH Input, Output, and Error chmod
- How to Learn
- Learning the Bash Shell , 2 nd Ed.
- http://www.tldp.org/LDP/abs/html/
- Introduction to bash
- Continuing Lines:
- This Is A Very Long Command Line
- Exit Status
- Exit Status: exit
- Logic: test
- Logic: test
- [[ “this string” =~ “this” ]]
- Logic: test
- [ -f /etc/passwd –a –f /etc/shadow ]
- [ -f /etc/passwd –o –f /etc/shadow ]
- An aside: $(( )) for Math
- Logic: if
- # “elif” a contraction of “else if”:
- Logic: if
- if [ $USER –eq “borwicjh” ]
- # “elif” a contraction of “else if”:
- Logic: if
- echo “/etc/passwd exists”
- echo “/etc/passwd not found!”
- Logic: for
- Logic: for
- Logic: for
- Logic: for
- Logic: C-style for
- Logic: C-style for
- Logic: while
- Logic: while
- while [ "$a" -lt "$LIMIT" ]
- Counters
- while [ -e “$FILE.COUNTER” ]
- COUNTER=$(( COUNTER + 1))
- Reusing Code: “Sourcing”
- % cat > /path/to/my/passwords <<_PW_
- % . /path/to/my/passwords
- Variable Manipulation
- % FILEPATH=/path/to/my/output.lis
- % echo ${FILEPATH %.lis }
- It takes a long time to become a bash guru…
- Running Programs
- Reasons for Running Programs
- Pipes
INPUT echo OUTPUT ERROR 0 1 2 INPUT wc OUTPUT ERROR 0 1 2 A Pipe!
- Email Notification
- mail –s “Here’s your message”
- Dates
- % DATESTRING=`date +%Y%m%d`
- FTP the Hard Way
- ftp –n –u server.wfu.edu <<_FTP_
- FTP with wget
- wget ftp://user:pass@server.wfu.edu/file
- wget –r ftp://user:pass@server.wfu.edu/dir/
- FTP with curl
- ftp://server.wfu.edu/dir/file
- Searching: grep
- Searching: find
- [all files matching *.lis]
- [*.lis, if modified within 24h]
- Searching: locate
- [files with .lis in path]
- [also finds “/var/log/messages”]
- Applied Shell Programming
- Make Your Life Easier
- pushd/popd
- Monitoring processes
- “DOS” Mode Files
- FTP transfer in ASCII, or
- dos2unix infile > outfile
- sqlplus
- sqlplus $BANNER_USER/$BANNER_PW << _EOF_
- set sqlprompt ""
- EXECUTE WF_SATURN.FZ_Get_Parameters('$JOB', '$PARAMS', '$PARAMS_USER');
- sqlplus
- sqlplus $USER/$PASS @$FILE_SQL
- if [ -e /file/sql/should/create ]
- Passing Arguments
- echo “Your name is $1 $2 ”
- % ./test.sh John Borwick ignore-this
- Your name is John Borwick
- INB Job Submission Template
- % /path/to/your/script $UI $PW
- Scheduling Jobs
- 0 0 * * * daily-midnight-job.sh
- * * * * * every-minute.sh
- 0 1 * * 0 1AM-on-sunday.sh
- It's Over!
- Other Questions?
- bash and Banner in Practice
No comments:
Post a Comment