Linux shell script-1

1,533 views

Published on

Lecture documentation for Linux/shell-script.

Published in: Education
  • Be the first to comment

Linux shell script-1

  1. 1. introduction ofthe shell scriptbasic sh and shell script usage2013 usagi@WonderRabbitProject.net CC0
  2. 2. whats the shell script?● its a language ○ script language // like a C style ■ procedural model ■ function is available ■ variable is available ■ type system is not available // so text based ■ can call an external routine // system call● its as same the shell-self ○ APIs are commands on the system
  3. 3. if you learned a C style language● control statements ○ loop ■ `for` ■ `while` // and `until` ○ conditional branching ■ `if` ■ `case` // equiv switch● function and variable ○ `f(){}` // equiv `void f(void){}` ■ how to use input params and a return? --> next! ○ `ABC=hoge` // equiv `char* ABC = "hoge"` ■ and more --> next!!
  4. 4. how to use function● how to get input params? ○ f(){ echo $1; echo $2; echo $3; } ■ its equiv in C style: void f(char* a, char* b, char* c) { puts(a); puts(b); puts(c); }● how to put return value? ○ n/a // shell script has not C style return value! ■ but, it has return code as same the system call● how call function? ○ f hoge fuga piyo ■ its equiv in C style f("hoge", "fuga", "piyo")
  5. 5. return value of function call● n/a● but, can use static variables and a trick ○ trick # define // its line comment use the char of `#` f(){ echo "its return value!"; } # call and get return value trick with backquote ` RETURN_VALUE = `f` # and print echo $RETURN_VALUE ■ whats `$` token? ● --> its a variable to next!
  6. 6. how to use a variable?● define a variable ○ HOGE=hogehogehoge FUGA=fugafugafuga num_of_my_awesome_defition=123456● reference a variable ○ echo $HOGE echo "$HOGE-$FUGA" echo $num_of_my_awesome_definition >> $hoge
  7. 7. variable tips-1● a variable is only string type ○ abc=1234 # quiv to abc="1234" # its not a number● writing rules ○ standard definition ■ abc=hogehoge # its ok ■ abc = fugafuga # ERROR! its a function call # $1==, $2=fugafuga; hint is white-space. ○ explicit value ■ def=#abc $abc @abc # equiv: #abc $abc @abc ■ def="#abc $abc @abc # equiv: #abc fugafuga @abc
  8. 8. variable tips-2● implicit defined special values ○ $1 $2 $3 ... its params by a function call or run a script with params. # f(){ echo $1; } // int main(int ac, char** params) { } ○ $# its number of params # f(){ echo $#; } // int main(int ac, char** params) { } ○ $? its a last result code (success or else) # echo $?
  9. 9. lets run a shell script> pwd/home/myname> # its comment line> my_awesome_variable=1234> echo $my_awesome_variable1234> my_awesome_function(){ echo $1; }> my_awesome_function hogehoge
  10. 10. save to file a shell script> echo echo $1 > my_awesome.sh> cat my_awesome.shecho $1you can write with the Vim or the other texteditor as you like if you want to use.> vim my_aersome2.sh
  11. 11. run a shell script from a file● run the other process on the process > sh my_awesome.sh hoge hoge > bash my_awesome.sh fuga fuga● run in the process > source my_awesome.sh piyo piyo
  12. 12. environment variable● variable ○ shell-variable its only visible from the shell process. > abc=hoge > sh <-- run a child process >> echo $abc <-- its put the empty string >> exit > echo $abc <-- its puts hoge ○ environment-varialbe its visible from the shell and child processes. > export abc=hoge > sh >> echo $abc <-- its puts hoge
  13. 13. shebang and executable-file● shebang ○ > vim hoge.sh #!/bin/sh <-- its shebang liine echo $1● executable-file ○ set the executable flag > chmod +x hoge.sh ○ run > ./hoge.sh <-- dont forget path before filename! ■ hint: see the $PATH environment variable. its search path for a command on your system. and think with a security policy.
  14. 14. shebang for any script-files● shell script #!/bin/sh● node.js script #!/usr/bin/node● python script #!/usr/bin/python● php script #!/usr/bin/php● haskell script #!/usr/bin/runhaskell hint: > file hoge.sh
  15. 15. `for` statementand an array like variable# its like an array separated by the space charvalues=aaa bbb ccc# like the foreach in Java or for-in in JSfor value in $valuesdo echo $valuedone
  16. 16. `seq` command> seq 812345678
  17. 17. `...` <-- its the back-quote pair,not a single-quote pair(...)# if use the single-quote pair> echo seq 512 64 768seq 512 64 768# if use the back-quote pair> echo `seq 512 64 768`512 576 640 704 768 <-- its like an array stringthe back-quote pattern is like an eval() in JS.its run a text as a script code. be cautious!
  18. 18. `for` statementwith `seq` command and ` patternits give a loop with a numeric sequence.for value in `seq 0 15`do echo $valuedone
  19. 19. `while` and `until` statements● while <-- if true then loop while true do echo \(^o^)/ done● until <-- if false then loop until false do echo /(^o^)\ done
  20. 20. `test` command for comparison> test 3 -eq 4 <-- equiv (3 == 4)> echo $?1 <-- so false in shell-script> test 3 -ne 4 <-- equiv (3 != 4)> echo $?0 <-- so true in shell-script> test 3 -gt 4 <-- equiv (3 > 4)> echo $?1 <-- so false in shell-script
  21. 21. `test` command for file check> test -f /proc/cpuinfo <-- is the file available> echo $?0> test -d /boot <-- is the directory available> echo $?0
  22. 22. `expr` commandfor calc a number from a string# its ng, not calc> echo 1 + 2 + 4# its ok, calc> echo `expr 1 + 2 + 4`7> expr 5 + ( 5 - 5 ) * 5 / 5 % 55# note: ( ) * <-- need escape-sequence.
  23. 23. `until` statementwith `test`, `expr` and ` patternits give a loop with a numeric counter.counter=15until `test $counter -eq 0`do echo $counter counter=`expr $counter - 1`done
  24. 24. `if` statement# if.sh > source if.shf(){ >f0 if test $1 -lt 0 then zero echo negative >f1 elif test $1 -gt 0 positive then echo positive > f -1 else negative echo zero fi}
  25. 25. `case` statement# case.sh > source case.shf(){ >f0 case $1 in 0) zerp echo zero;; >f5 1) two-nine echo one;; [2-9]) > f -1 echo two-nine;; ???? default) echo ???? esac}
  26. 26. where are more information?all of the information isavailable on the manpages. see the man pages.man page is the bestunified answerer for allGNU/Linux users.> man sh

×