Shell scripting - By Vu Duy Tu from eXo Platform SEA

  • 1,248 views
Uploaded on

This presentation made by Vu Duy Tu from eXo Platform SEA

This presentation made by Vu Duy Tu from eXo Platform SEA

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,248
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
51
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. www.exoplatform.com Copyright 2011 eXo Platform Linux Shell Scripting Exo Shell Scripting Vu Duy Tu – CT Team Link to file in exo site
  • 2. www.exoplatform.com Copyright 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.com Copyright 2011 eXo Platform What is shell ?
    • Shell is
      • Command Interpreter that turns text that you type (at the command line) in to actions.
      • User Interface: take the command from user.
    • Programming Shell can do
      • Customization of a Unix session
      • Scripting
  • 4. www.exoplatform.com Copyright 2011 eXo Platform What is shell ? Unix Architecture
  • 5. www.exoplatform.com Copyright 2011 eXo Platform What is shell ? Popular Shells Shell Name Developed by Where Remark BASH ( Bourne-Again SHell ) Brian Fox and Chet Ramey Free Software Foundation Most common shell in Linux. It's Freeware shell. 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 enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).
  • 6. www.exoplatform.com Copyright 2011 eXo Platform What is shell ? Families of Shells
  • 7. www.exoplatform.com Copyright 2011 eXo Platform What is shell ? Flavors of Unix Shells
    • Two main flavors of Unix Shells
      • Bourne (or Standard Shell): sh, ksh, bash, zsh
        • Fast
        • $ for command prompt
      • C shell : csh, tcsh
        • better for user customization and scripting
        • %, > for command prompt
    • To check shell:
      • % echo $SHELL (shell is a pre-defined variable)
    • To switch shell:
      • % exec shellname (e.g., % exec bash)
  • 8. www.exoplatform.com Copyright 2011 eXo Platform What is shell ? Login shell
    • sh,ksh:
    • /etc/profile (out-of-the-box login shell settings) /etc/profile.local (addtnl. local system settings) ~/.profile (addtnl. user customized settings) ~/.kcshrc (non-login shell user customization)
    • bash: /etc/profile (out-of-the-box login shell settings) /etc/bash.bashrc (out-of-box non-login settings) /etc/bash.bashrc.local (global non-login settings)
    • ~/.bash_profile (login shell user customization)
    • ~/.bashrc (non-login shell user customization)
    • ~/.bash_logout (user exits from interactive login shell)
    • csh/tcsh: /etc/login (out-of-the-box login shell settings) /etc/csh.login (non-login shell customizations) /etc/csh.login.local (global non-login settings)
      • ~/.login: (login shell user customizations)
      • ~/.cshrc: (non-login shell user customizations)
      • ~/.cshrc.logout: (non-login shells at logout)
      • ~/.logout: (read by login shells at logout)
  • 9. www.exoplatform.com Copyright 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.com Copyright 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.com Copyright 2011 eXo Platform What is Shell Script ? Why to Write Shell Script ? Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. To automate some task of day today life. System Administration part can be also automated.
  • 12. www.exoplatform.com Copyright 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.com Copyright 2011 eXo Platform Shell Scripting Manual
    • Manual Basic
    - Operators Operators Example = a=$b; a=&quot;abc&quot;; a=&quot;a=$b&quot;; a=$(ls); a=`ls *.java`; a=$((a+1)); +,-,*,/,% Only use in (()) or use command expr: echo &quot;1 + 2 = $((1+2)) or 1 + 2 = `expr 1+ 2`&quot;; echo &quot;3 * 5 = `3 * 5`&quot;; ==, !=, >, < &&, || if [ $a == $b ], if [ $a != $b ], if [ $((a > 5)) ], if [ $a -gt 5 ], if [ $a -le 5 ] if [ -n &quot;$a&quot; ] && [ -e &quot;$b&quot; ]; ..., if [ -n &quot;$a&quot; ] || [ -n &quot;$b&quot; ]; ... - Different &quot; &quot;, ` ` and ' '
    • &quot;Double Quotes&quot; : The normal string, but use (except and $).
    • `Back quote` - To execute command
    • 'Single quotes' - Enclosed in single quotes remains unchanged.
  • 14. www.exoplatform.com Copyright 2011 eXo Platform Shell Scripting Manual -Parameters + It is their position on the command line - Operators Operators Example if... else if [ $a == $b ]; then .... fi if [ $a == $b ]; then .... elif [ $a == &quot;xxx&quot; ]; then ... fi if [ $a == $b ]; then .... elif [ $a == &quot;xxx&quot; ]; 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 Detail $0 Name of the calling program $1 - $9 Command-line Arguments
  • 15. www.exoplatform.com Copyright 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 &quot;${arr[@]}&quot;; do ... done
  • 16. www.exoplatform.com Copyright 2011 eXo Platform Shell Scripting Example - Base shell
  • 17. www.exoplatform.com Copyright 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.com Copyright 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.com Copyright 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.com Copyright 2011 eXo Platform Document reference 1/ The book attachment in wiki page. 2/ Link: http://javarevisited.blogspot.com http://tldp.org/LDP/abs/html/ http://www.freeos.com/guides/lsst/ (main) http://www.grymoire.com/Unix/Sed.html
  • 21. www.exoplatform.com Copyright 2011 eXo Platform Discussion Thank you!
  • 22. www.exoplatform.com Copyright 2011 eXo Platform Linux Shell Scripting Exo Shell Scripting Vu Duy Tu – CT Team Link to file in exo site