linux shell script-1

26
introduction of the shell script basic sh and shell script usage 2013 [email protected] CC0

Upload: usagi-ito

Post on 22-Nov-2014

1.114 views

Category:

Education


3 download

DESCRIPTION

Lecture documentation for Linux/shell-script.

TRANSCRIPT

Page 1: Linux shell script-1

introduction of the shell scriptbasic sh and shell script usage2013 [email protected] CC0

Page 2: Linux shell script-1

what's the shell script?

● it's 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

● it's as same the shell-self○ APIs are commands on the system

Page 3: Linux shell script-1

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!!

Page 4: Linux shell script-1

how to use function

● how to get input params?○ f(){ echo $1; echo $2; echo $3; }

■ it's 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■ it's equiv in C style

f("hoge", "fuga", "piyo")

Page 5: Linux shell script-1

return value of function call

● n/a● but, can use static variables and a trick

○ trick# define // it's line comment use the char of `#`f(){ echo "it's return value!"; }# call and get return value trick with backquote '`'RETURN_VALUE = `f`# and printecho $RETURN_VALUE■ what's `$` token?

● --> it's a variable to next!

Page 6: Linux shell script-1

how to use a variable?

● define a variable○ HOGE=hogehogehoge

FUGA=fugafugafuganum_of_my_awesome_defition=123456

● reference a variable○ echo $HOGE

echo "$HOGE-$FUGA"echo $num_of_my_awesome_definition >> $hoge

Page 7: Linux shell script-1

variable tips-1

● a variable is only string type○ abc=1234 # quiv to abc="1234"

# it's not a number● writing rules

○ standard definition■ abc=hogehoge # it's ok■ abc = fugafuga # ERROR! it's 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

Page 8: Linux shell script-1

variable tips-2

● implicit defined special values○ $1 $2 $3 ...

it's params by a function call or run a script with params.# f(){ echo $1; }// int main(int ac, char** params) { }

○ $#it's number of params# f(){ echo $#; }// int main(int ac, char** params) { }

○ $?it's a last result code (success or else)# echo $?

Page 9: Linux shell script-1

let's run a shell script

> pwd/home/myname> # it's comment line> my_awesome_variable=1234> echo $my_awesome_variable1234> my_awesome_function(){ echo $1; }> my_awesome_function hogehoge

Page 10: Linux shell script-1

save to file a shell script

> echo 'echo $1' > my_awesome.sh> cat my_awesome.shecho $1

you can write with the Vim or the other text editor as you like if you want to use.

> vim my_aersome2.sh

Page 11: Linux shell script-1

run a shell script from a file

● run the other process on the process> sh my_awesome.sh hogehoge> bash my_awesome.sh fugafuga

● run in the process> source my_awesome.sh piyopiyo

Page 12: Linux shell script-1

environment variable

● variable○ shell-variable

it's only visible from the shell process.> abc=hoge> sh <-- run a child process>> echo $abc <-- it's put the empty string>> exit> echo $abc <-- it's puts 'hoge'

○ environment-varialbeit's visible from the shell and child processes.> export abc=hoge> sh>> echo $abc <-- it's puts 'hoge'

Page 13: Linux shell script-1

shebang and executable-file

● shebang○ > vim hoge.sh

#!/bin/sh <-- it's shebang liineecho $1

● executable-file○ set the executable flag

> chmod +x hoge.sh○ run

> ./hoge.sh <-- don't forget path before filename!■ hint: see the $PATH environment variable. it's

search path for a command on your system. and think with a security policy.

Page 14: Linux shell script-1

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

Page 15: Linux shell script-1

`for` statement and an array like variable

# it's like an array separated by the space char

values='aaa bbb ccc'

# like the foreach in Java or for-in in JS

for value in $valuesdo echo $valuedone

Page 16: Linux shell script-1

`seq` command

> seq 812345678

Page 17: Linux shell script-1

`...` <-- it's the back-quote pair, not a single-quote pair('...')

# if use the single-quote pair> echo 'seq 512 64 768'seq 512 64 768# if use the back-quote pair> echo `seq 512 64 768`512 576 640 704 768 <-- it's like an array string

the back-quote pattern is like an eval() in JS. it's run a text as a script code. be cautious!

Page 18: Linux shell script-1

`for` statement with `seq` command and ` pattern

it's give a loop with a numeric sequence.

for value in `seq 0 15`do echo $valuedone

Page 19: Linux shell script-1

`while` and `until` statements

● while <-- if true then loopwhile truedo echo '\(^o^)/'done

● until <-- if false then loopuntil falsedo echo '/(^o^)\'done

Page 20: Linux shell script-1

`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

Page 21: Linux shell script-1

`test` command for file check

> test -f /proc/cpuinfo <-- is the file available> echo $?0> test -d /boot <-- is the directory available> echo $?0

Page 22: Linux shell script-1

`expr` command for calc a number from a string

# it's ng, not calc> echo 1 + 2 + 4

# it's ok, calc> echo `expr 1 + 2 + 4`7> expr 5 + \( 5 - 5 \) \* 5 / 5 % 55# note: ( ) * <-- need \ escape-sequence.

Page 23: Linux shell script-1

`until` statement with `test`, `expr` and ` pattern

it's give a loop with a numeric counter.

counter=15until `test $counter -eq 0`do echo $counter counter=`expr $counter - 1`done

Page 24: Linux shell script-1

`if` statement

# if.shf(){ if test $1 -lt 0 then echo 'negative' elif test $1 -gt 0 then echo 'positive' else echo 'zero' fi}

> source if.sh> f 0zero> f 1positive> f -1negative

Page 25: Linux shell script-1

`case` statement

# case.shf(){ case $1 in 0) echo 'zero';; 1) echo 'one';; [2-9]) echo 'two-nine';; default) echo '????' esac}

> source case.sh> f 0zerp> f 5two-nine> f -1????

Page 26: Linux shell script-1

where are more information?

all of the information is available on the man pages. see the man pages. man page is the best unified answerer for all GNU/Linux users.

> man sh