Shell scripting - By Vu Duy Tu from eXo Platform SEA
-
Upload
thuydang -
Category
Technology
-
view
1.253 -
download
1
description
Transcript of Shell scripting - By Vu Duy Tu from eXo Platform SEA
www.exoplatform.comCopyright 2011 eXo Platform
Linux Shell ScriptingExo Shell Scripting
Vu Duy Tu – CT Team
Link to file in exo site
2 www.exoplatform.comCopyright 2011 eXo Platform
Agenda
What is Shell ?How to use Shell?What is Shell Script ?Why to Write Shell Script ?Shell Scripting Manual.Shell Scripting Example.Exo Shell Scripting.EXOCT (my shell scripting for eXo).
3 www.exoplatform.comCopyright 2011 eXo Platform
What is shell ?
Shell isShell is Command Interpreter that turns text that Command Interpreter that turns text that
you type (at the command line) in to actions.you type (at the command line) in to actions. User Interface: take the command from user.User Interface: take the command from user.
Programming Shell can doProgramming Shell can do Customization of a Unix sessionCustomization of a Unix session ScriptingScripting
4 www.exoplatform.comCopyright 2011 eXo Platform
What is shell ?
UnixUnix Architecture Architecture
5 www.exoplatform.comCopyright 2011 eXo Platform
What is shell ?
Popular ShellsPopular ShellsShell Name Developed
byWhere Remark
BASH ( Bourne-Again SHell )
Brian Fox and Chet Ramey
Free SoftwareFoundation
Most common shell inLinux. It's Freewareshell.
CSH (C SHell) Bill Joy University of California (For BSD
The C shell's syntax and usage are very similar to the C programming language.
KSH (Korn SHell) David Korn AT & T Bell Labs
TCSH See the man page.Type $ man tcsh
TCSH is an enhancedbut completelycompatible version ofthe Berkeley UNIX Cshell (CSH).
6 www.exoplatform.comCopyright 2011 eXo Platform
What is shell ?
Families of ShellsFamilies of Shells
7 www.exoplatform.comCopyright 2011 eXo Platform
What is shell ?
Flavors of Unix ShellsFlavors of Unix ShellsTwo main flavors of Unix ShellsTwo main flavors of Unix ShellsBourne (or Standard Shell): sh, ksh, bash, zshBourne (or Standard Shell): sh, ksh, bash, zsh
• FastFast• $ for command prompt$ for command prompt
C shell : csh, tcshC shell : csh, tcsh• better for user customization and scriptingbetter for user customization and scripting• %, > for command prompt%, > for command prompt
To check shell:To check shell:% echo $SHELL (shell is a pre-defined variable)% echo $SHELL (shell is a pre-defined variable)To switch shell:To switch shell:% exec shellname (e.g., % exec bash)% exec shellname (e.g., % exec bash)
8 www.exoplatform.comCopyright 2011 eXo Platform
What is shell ?
Login shellLogin shellsh,ksh: sh,ksh:
/etc/profile /etc/profile (out-of-the-box login shell settings)(out-of-the-box login shell settings)/etc/profile.local (addtnl. local system settings)/etc/profile.local (addtnl. local system settings)~/.profile ~/.profile (addtnl. user customized settings)(addtnl. user customized settings)~/.kcshrc~/.kcshrc (non-login shell user customization)(non-login shell user customization)
bash:bash:/etc/profile (out-of-the-box login shell settings) /etc/profile (out-of-the-box login shell settings) /etc/bash.bashrc (out-of-box non-login settings)/etc/bash.bashrc (out-of-box non-login settings)/etc/bash.bashrc.local (global non-login settings)/etc/bash.bashrc.local (global non-login settings)
~/.bash_profile~/.bash_profile (login shell user customization) (login shell user customization)
~/.bashrc~/.bashrc (non-login shell user customization)(non-login shell user customization)
~/.bash_logout (user exits from interactive login shell)~/.bash_logout (user exits from interactive login shell)
csh/tcsh:csh/tcsh:/etc/login/etc/login (out-of-the-box login shell settings) (out-of-the-box login shell settings)/etc/csh.login (non-login shell customizations)/etc/csh.login (non-login shell customizations)/etc/csh.login.local (global non-login settings)/etc/csh.login.local (global non-login settings)
~/.login: (login shell user customizations)~/.login: (login shell user customizations)
~/.cshrc: (non-login shell user customizations)~/.cshrc: (non-login shell user customizations)
~/.cshrc.logout: (non-login shells at logout)~/.cshrc.logout: (non-login shells at logout)
~/.logout: (read by login shells at logout)~/.logout: (read by login shells at logout)
9 www.exoplatform.comCopyright 2011 eXo Platform
How to use Shell ?
- To use shell (You start to use your shell as soon as you log into your system) you have to simply type commands.
- See common Linux Command for syntax and example, this can be used as quick reference while programming the shell.
10 www.exoplatform.comCopyright 2011 eXo Platform
What is Shell Script ?
Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.
11 www.exoplatform.comCopyright 2011 eXo Platform
What is Shell Script ?
Why to Write Shell Script ?Why to Write Shell Script ?
Shell script can take input from user, file and Shell script can take input from user, file and output them on screen.output them on screen.
Useful to create our own commands.Useful to create our own commands.
Save lots of time.Save lots of time.
To automate some task of day today life.To automate some task of day today life.
System Administration part can be also System Administration part can be also automated.automated.
12 www.exoplatform.comCopyright 2011 eXo Platform
Shell Scripting Manual
- Make a comment: # - make easier to read and maintain - Make a alias: alias="command/function" - Print info in to console: echo - Goto dir: cd - View files/folder in dir: ls - Move/Copy/Remove files/folder: mv/cp/rm - Create folder: mkdir - Create text file: echo "content" > file.txt - Install application : apt-get, dpkg - Editor text file: gedit, vim.. - Search files/folder: find - Search content: grep - Replace content: sed - Download file from Internet: wget ...
Manual function
13 www.exoplatform.comCopyright 2011 eXo Platform
Shell Scripting ManualManual Basic
- Operators
Operators
Example
= a=$b; a="abc"; a="a=$b"; a=$(ls); a=`ls *.java`; a=$((a+1));
+,-,*,/,% Only use in (()) or use command expr: echo "1 + 2 = $((1+2)) or 1 + 2 = `expr 1+ 2`"; echo "3 * 5 = `3 \* 5`";
==, !=,>, <
&&, ||
if [ $a == $b ], if [ $a != $b ], if [ $((a > 5)) ], if [ $a -gt 5 ], if [ $a -le 5 ] if [ -n "$a" ] && [ -e "$b" ]; ..., if [ -n "$a" ] || [ -n "$b" ]; ...
- Different " ", ` ` and ' '
"Double Quotes" : The normal string, but use (except \ and $). `Back quote` - To execute command 'Single quotes' - Enclosed in single quotes remains
unchanged.
14 www.exoplatform.comCopyright 2011 eXo Platform
Shell Scripting Manual
- OperatorsOperators Example
if... else if [ $a == $b ]; then .... fi if [ $a == $b ]; then .... elif [ $a == "xxx" ]; then ... fi if [ $a == $b ]; then .... elif [ $a == "xxx" ]; then ... else ... fi
For Loops for X in `ls *.java` do .... done for ((i = 0 ; i <= 10 ; i++ )) do ... done
Functions function abc { .... do something ... }-Parameters + It is their position on the command line
Parameters Detail
$0 Name of the calling program
$1 - $9 Command-line Arguments
15 www.exoplatform.comCopyright 2011 eXo Platform
Shell Scripting Manual
- Parameters + Value of Command-line arguments: $* and $@( not just the first nine) “$*” : treats the entire list of arguments as a single argument “$@” : produce a list of separate arguments (Only bash/ksh/sh) + Example for use:
- Array + Initialization: arr=(item1 item2 item3 ... itemn). + Use: echo arr[0]; + Loops: for X in "${arr[@]}"; do ... done
16 www.exoplatform.comCopyright 2011 eXo Platform
Shell Scripting Example
- Base shell
17 www.exoplatform.comCopyright 2011 eXo Platform
Exo Shell Scripting
I. Exo application using in terminal. - Maven : + mvn version + mvn clean + mvn eclipse:eclipse + mvn install - param - Svn : + svn checkout (co) + svn update (ud) + svn commit (ci) + svn info, log, diff, st - Git + git init + git add .... - Patch: patch file diff. - MySql - Achecker client
18 www.exoplatform.comCopyright 2011 eXo Platform
Exo Shell Scripting
II. Apply shell for Exo - Create stack for build projects (exobuild). - Create alias for quick run command. - Create function for run sequential other command. - Setup or install other application help for work in eXo - Create function for automatically run some stack (ex: jenkins) - Create function (search by rules) for checking bug in code. - Maybe we can use find/grep/sed for fix some bug :D. (ex: accessibility )
19 www.exoplatform.comCopyright 2011 eXo Platform
Exo Shell Scripting
III. EXOCT (my shell scripting for eXo)
- Quick use some function in eXo. - Quick goto produce. - Easy for update/build other produces - Easy for create patch - Easy for build quickwar/module of produce - Easy for run tomcat ... Other..
20 www.exoplatform.comCopyright 2011 eXo Platform
Document reference
1/ The book attachment in wiki 1/ The book attachment in wiki page.page. 2/ Link:2/ Link: http://javarevisited.blogspot.com http://tldp.org/LDP/abs/html/
http://www.freeos.com/guides/lsst/ (main) (main) http://www.grymoire.com/Unix/Sed.html
21 www.exoplatform.comCopyright 2011 eXo Platform
Discussion
Thank you!
22 www.exoplatform.comCopyright 2011 eXo Platform
Linux Shell ScriptingExo Shell Scripting
Vu Duy Tu – CT Team
Link to file in exo site