Ȼ/07

Top / Ȼ / 07

What are shell scripts?: 륹ץȤȤ

JNorth_arrow-right-sm.png ץȥեΥޥɲ ѤơĤΥޥɤѤΤǤ롥
Unix Dz褦ʺȤ򤹤Ȥʤɤˤϡ˰Ϥȯ롥



֤ϥ륹ץȤ
ޥɤˤƤɤɤΨ褯

ʸˡ˴ؤƤϡsh ϥ csh ϥǷ빽㤦

  • csh Ϥʹ֤ˤȤäľŪ.
  • bsh ϤȤƤ ( warning.png "ͭ csh ץߥ" ȤʸwebǸƤߤ褦)



bsh ϤdzؽƤ.



Preparation and rough explanation: ȰϢή

Preparation:

  1. ץȥե¢Ƥǥ쥯ȥѰա bin Ȥ̾Ѥ뤳Ȥ¿

    cd␣~

    mkdir␣bin


  2. Ѱդǥ쥯ȥ path ̤.profile ꤹΤɤ Ūˤϡ~/.profile ΤäݤȤ
    1. if [ -d $HOME/bin ]
    2.   then
    3.   PATH=$PATH:$HOME/bin; export PATH
    4. fi
    ʤɤȽ񤭹ǤСꤳ줬ͭˤʤ롥
    warning.png [ ] äƤ륹ڡʤȡ ϰʸޥɤʤΤ
    warning.png ѹͭˤ뤿ˤäȤƤƥ󤷤Ƥ褤

    source␣~/.profile

    ϤȤξ.profileեȤȿǤΤǤǤ褤

notes.png ½: ν 1, 2 򤷤Ƥ δĶǤϺƥ󤷤 .profile ƤȿǤʤ⤷ʤ ξϡ󤷤sourceȤˡ path ()ꤷ褦

Rough explanation: 륹ץѤΰϢή

륹ץȤäơƻȤˤϤĤʲʳƧǤɤ

  1. 륹ץȤꡤ1 Ѱդǥ쥯ȥ֤.

  2. ץȥե˼¹ԵĤͿ롥
    㤨Сץȥե뤬 dummy Ȥ̾ʤ

    chmod␣u+x␣~/bin/dummy

    Ȥɤ



Basic structure of shell scripts: 륹ץȤδܹ¤(bash)

JNorth_arrow-right-sm.png ץȥեΥޥɲ Ѥƥ륹ץȤϺ롥 ץȤƬԤˤɤ񤯤Ǥ뤬linux ϤǤ

#!/bin/sh

Ƚ񤱤ɤ



warning.png ץȤƤ¹ԤʤǡʸˡåԤϡ

#!/bin/sh␣-n

ƬԤ˽񤱤Ф褤 եκʤɡʺȤԤץȤϤǥåɤ



warning.png ¹Ի˥ץȤƤɽϡ

#!/bin/sh␣-v

ƬԤ˽񤱤Ф褤 ץȤư˾ߤΤΤȰ㤦Ϥǥåɤ



notes.png ½

  1. Hello Ȥե̾ǡΤ褦ȤΥץȤ.
    1. #!/bin/sh
    2. echo "Hello, world!"

  2. JNorth_arrow-right-sm.png Ϣή 1,2 Ԥ

  3. ɤΥǥ쥯ȥ꤫Ǥ褤Τǡ

    Hello

    Ǥǡ륹ץȤư뤳Ȥǧ褦



Continuous execution of commands: ޥɤϢ³¹

ޥɤϢ³¹ˤĤ JNorth_arrow-right-sm.png Ϣ³¹ ʬdzؽǾɲäƤ

ޥ1␣;␣ޥ2
ޥ1 ¹Ը塤ޥ2 ¹Ԥ롥
( ޥ1␣;␣ޥ2 )
ޥ1 ¹Ը塤ޥ2 ¹Ԥ Τ ĤΥޥɤ֥֥פΤȤǼ¹Ԥ(롼ײ) ֥ǹԤäĶѹϤȤΥ˱ƶʤ äơ֤ŪѹƺȤȤ

Gnome-Preferences.png 롼ײ.
㤨С

(cd␣/tmp;␣ls␣-a)

Ȥȡ/tmp ǺȤԤ뤬äȤ˥ǥ쥯ȥѹƤʤ

ޤ롼ײޥɤɸϡֹ蘆äơ׽ФƤ롥 줬ʤȤ¿

Gnome-Preferences.png 롼ײ2.
㤨С

(\ls␣/tmp;␣\ls␣/var)␣|␣less

Ȥȡ/tmp /var 2ĤΥǥ쥯ȥˤեΥꥹȤ٤˱Ǥ롥
ޥ1␣&&␣ޥ2
AND ¹ԡ ޥ1 ¹ԤƤߤơޥ1 ʤ(顼ˤʤʤ) ޥ2 ¹Ԥ롥
ޥ1␣¦¦␣ޥ2
OR ¹ԡ ޥ1 ¹ԤƤߤơޥ1 Ԥʤ(顼ˤʤä) ޥ2 ¹Ԥ롥



Grammer of shell scripts: 륹ץȤʸˡ

Comment out: ȥ

륹ץ # Ȥʸȡ ޤǤȤȤƼ갷(ʸϽ)

Variables: ѿ

JNorth_arrow-right-sm.png ѿ dzؤȤˡ륹ץȤѿȤ롥

warning.png ѿΤȤѿ̾ȡ=פδ֤˥ڡʡ


warning.png ¤ϡѿȤκݤ

${ѿ̾}

Ȥ



String: ʸ

ʸ quotation ɽ
λ '(󥰥륯) ǰϤΤȡ "(֥륯) ǰϤΤǤۤʤΤդɬפ

'(󥰥륯) ǰϤʸ
񤫤ƤƤñʤʸȤư

"(֥륯) ǰϤʸ
$ ` \ 3Ĥüʸ򤳤ޤdzؤüʰ̣ǰ. Ĥޤꡤ"(֥륯)ǰϤޤ줿ʸѿλȤϹԤ. "(֥륯) ΰϤǾ3ĤüʸñʤʸȤưС \ ˤĤ \$ \` \\ Ȥ롥



notes.png ½

Hello ץȤ򼡤Τ褦˲¤褦.

  1. #!/bin/sh
  2. # Greeting Script
  3. g='Hello, world!'
  4. d="today is `date +%m/%d`"
  5. echo $g, $d.

ơɤʤäƤΤ򤷤褦



Arguments:

ȤϡץưȤˡͿǤ롥 㤨 ls␣-a␣/tmp ȥץ¹Ԥ顤(̣) -a, /tmp ĤǤ롥
ΰϰʲΤ褦˰롥

sh ϥ륹ץȤǤΰΰ
ѿ̣
$#θĿ
$nn ܤΰ
0ܤΰ $0 ̤ǡ륹ץȼȤ̾ˤʤ롥

ޤn > 9 Ǥ ${12} ʤɤȡ楫åǰϤޤʤȻȤʤ
$*ơ
() $argv{m-n}m ܤ n ܤޤǤΰλ. shϤǤͭǤϤʤ褦 bash ʤɤǤϡʣʽ getopts ǹԤΤɤ

Argument : shift ޥ

shift Ȥޥɤ¹Ԥȡ ܤΥץƤõ졤ܰʹߤˤ롥 ȡ $1 $2 Ƥꡤ $2 $3 Ƥ äưˤʤ롥 ޤη̤Ȥƥץο $# 1 롥



notes.png ½

Welcome Ȥե̾ǥץȤ򼡤Τ褦˺.

  1. #!/bin/sh
  2. # Welcome Script
  3. name=`whoami`
  4. g="($1 says) Thank you, $name"
  5. echo $g.

ơΥץȤ򼡤Τ褦˼¹ԤƤߤ褦.

Welcome␣Smith

¹ԤƤߤ补 ơץȤƤɤ򤻤.



I/O:

륹ץȤ¹ϤԤˤϡ˼ΥޥɤѤ롥

read␣ѿ
ϡ
桼(ܡ)ϤԤη̤ѿˤ롥
echo␣Ϥ
echo␣-n␣Ϥ
ϡϸ˲Ԥʤ

Gnome-Preferences.png ¹㡥

  1. #!/bin/sh
  2. echo -n 'Pls input something : '
  3. read a
  4. echo ' You input is ' $a

¹ԤȤɤʤ



notes.png ½

Hello ץȤ򤵤˲¤ƼΤ褦ˤ褦.

  1. #!/bin/sh
  2. # Greeting Script
  3. g='Hello'
  4. i='Please tell me your name: '
  5. o="Oh, nice to meet you"
  6. echo $g.
  7. echo -n $i
  8. read ans
  9. echo $o, $ans.

ơΥץȤ¹ԤƤߤȤȤˡɤʤäƤΤ򤻤.



Calculations: ׻

bash ϤȤ⤫ ꥸʥ sh Ϸ׻ǽۤȤɻäƤʤ
ǡ륹ץǷ׻ԤȤϸߴդߤ JNorth_arrow-right-sm.png ɤɾ `` ѤƳޥɤƤӽФʤɤˡΤ뤳Ȥ¿
Ūˤϡñ׻Ǥ褱 expr ʣʼ¿׻Ԥ bc ʤɤȤäƼΤ褦ˤ롥

Gnome-Preferences.png expr Ȥä

  1. #!/bin/sh  
  2. a=5
  3. b=`expr $a + 10` ; echo $b
  4. c=`expr $b \* 2` ; echo $c

warning.png ǸιԤ ` ` ǡ* Ÿƥե̾ʤɤˤʤäƤޤͤʤΤǡ\* Ȥ()ñʤʸȤƤ롥

notes.png ½

ΥץȤƤߤ.



Gnome-Preferences.png bc Ȥä

bc ϡɸϤ׻äƷ̤ɸϤ˽ФΤǡ㤨 echo ǼϤɤ

  1. #!/bin/sh  
  2. a=`echo "4*a(1.0)" | bc -l` ; echo $a
  3. b=`echo "c($a)" | bc -l` ; echo $b

ʤߤˡ2ܤ Ф3ܤ cos() ׻Ƥ롥

notes.png ½

ΥץȤƤߤ.



Functions of shell: δؿ

sh ϥǤϡʣΥޥɤޤȤ̾ĤΡפʬǺäƻȤ롥 ϥץߥ󥰤Ǥ ؿ ȤФ롥

ؿˤϡ



ؿ̾ () {
    ޥɡ
    }

ȤʸѤ롥
ʤߤˡ{ } ޤǤδ֤ ؿФ n ܤΰ $n ȤƻȤǤ.

ޤδؿǻȤѿȤϡ local ƬˤĤѿ򤹤Ф褤


warning.png Ǥ sh alias ѿ򤦤ޤȤʤΤǡʪȤƴؿΩġ



Gnome-Preferences.png

  1. #!/bin/sh
  2.  
  3. mawaru () {
  4.   local i=1
  5.   while [ $i -le 10 ]
  6.     do
  7.     echo $i
  8.     i=`expr $i + 1`
  9.     done
  10. }
  11.  
  12. mawaru

Ȥ륹ץȤϤɤư򤹤뤫ͤưƤߤ褦 ʤwhile ʸΰ̣ʸˡϤΤdzؤ֡ ˤʤͤϤɤǤäƤ褦

notes.png ½

Hello ץȤ򤵤˲¤ƼΤ褦ˤ褦.

  1. #!/bin/sh
  2. # Greeting Script
  3. g='Hello'
  4. d=`date +%d`
  5. dm=`expr $d - 1`
  6. t=`date +%H`
  7. m=`date +%M`
  8.  
  9. d2min () {
  10. min=`echo "(($1 * 24) + $2) * 60 + $3" | bc -l`
  11. }
  12.  
  13. d2min $dm $t $m
  14. echo $g, "about $min minutes have passed this month."

ơΥץȤ¹ԤƤߤȤȤˡɤʤäƤΤ򤻤.



Flow control of shell: 湽¤

֤⤷ĤʤСפȤ֡Ĥ򷫤֤ơġפȤưϼΤ褦ˤ롥



Conditional branch: if (ñ)ʬ

˱(Ĥ)ʬƤ Ūʹʸϰʲ̤ꡥ

if␣1
   then␣ޥ1

   elif␣2
     then␣ޥ2
   elif␣3
      then␣ޥ3

   (ʲ, elif then 򹥤ʤ֤)

   else␣ޥN
fi

warning.png νˤĤƤϡΤ JNorth_arrow-right-sm.png PӾPΥå dzؤܤ

warning.png elif ʲ else ʲ¸ߤʤƤɤ



ιʸǤήϰʲ̤ꡥ

ǽ 1 ¹ & å졤
1 Ω(= ¹Ԥ) ޥ1 ¹Ԥ if ʸϽλ롥
1 Ωʤ elif ء

elif 褿硥2 å졤
2 Ω ޥ2 ¹Ԥ if ʸϽλ롥
2 Ωʤ elif ء

( ʲ֤ )

else 褿硥̵ ޥN ¹Ԥ if ʸϽλ롥



notes.png ½

Τ褦ƤΥץȤꡤ¹Ԥ򤻤. if Τ [␣-e␣$fn␣] ȤʬˤĤƤϸҤΤϸ󤷤Ǥ褤.

  1. #!/bin/sh
  2.  
  3. fn=~/.bashrc
  4.  
  5. if [ -e $fn ]
  6.   then
  7.     cat $fn
  8.   else
  9.     echo "You has no $fn."
  10. fi



Pattern match: case (ѥޥå)ʬ

ʸפѰդѥȥޥå뤫ɤʬ롥 ñξʬ ѥ "¦" Ƕڤäɽ(or ̣)뤳ȤǤ롥

Ūʹʸʲ̤ꡥ

case␣ʸ␣in
    ѥ1)␣ޥ1 ;;
    ѥ2)␣ޥ2 ;;

( ʲ, ѥk)␣ޥk;; 򹥤ʤ֤ )

    *)␣ޥN
esac



warning.png *) ¸ߤʤƤɤ



ήϰʲ̤ꡥ



ǽ ʸ ɾơ
ʸ󤬥ѥ1 ȥޥå ޥ1 ¹Ԥ case ʸϽλ롥
ޥåʤ Υѥȥޥå뤫(ʲ֤)

* ) 褿硥̵ ޥN ¹Ԥ case ʸϽλ롥



notes.png ½



Τ褦ƤΥץȤꡤ¹Ԥ򤻤.

  1. #!/bin/sh
  2.  
  3. echo -n "Please input file name: "
  4. read fn
  5.  
  6. case $fn in
  7.   *.txt )
  8.     echo "$fn is text file.";;
  9.   *.c )
  10.     echo "$fn is C program file.";;
  11.   * )
  12.     echo "Hey, what is $fn ? You know it?";;
  13. esac



for ƤϤޤѿη֤

Ūʹʸưήϰʲ̤ꡥ



for␣ѿ␣in␣ѥorꥹ
do
    ޥɡ
done



ޤѥorꥹȤŸơꥹȤ롥
ΥꥹȤǤ֤ ѿ ơ ΤӤ˥ޥɡ ¹Ԥ롥



notes.png ½



Τ褦ƤΥץȤꡤ¹Ԥ򤻤.

  1. #!/bin/sh
  2.  
  3. for x in 0 1 2 3 4 5 6 7 8 9 10
  4. do
  5.   v=`echo "$x * 0.31416" | bc -l`
  6.   echo "sin($v) = " `echo "s($v)" | bc -l`.
  7. done



Loop: while, until åĤǤη֤

Ūʹʸưήϰʲ̤ꡥ



while␣
do
    ޥɡ
done



warning.png until ιʸƱ



ޤ郎ΩĤå
[ while ξ] ΩĤʤСޥɡ ¹ԡ
[ until ξ] ΩʤʤСޥɡ ¹ԡ

Ƥӡ郎ΩĤå ʲ֤



notes.png ½



Τ褦ƤΥץȤꡤ¹Ԥ򤻤. while Τ [␣$x␣-le␣10␣] ȤʬˤĤƤϸҤΤϸ󤷤Ǥ褤.

  1. #!/bin/sh
  2.  
  3. x=0
  4.  
  5. while [ $x -le 10 ]
  6. do
  7.   v=`echo "$x * 0.31416" | bc -l`
  8.   echo "sin($v) = " `echo "s($v)" | bc -l`.
  9.   x=`expr $x + 1`
  10. done



Break-out: break ĥ롼æС

Υޥɤ¹Ԥȡforwhile, until Υ롼פæФ롥



Termination: exit λ

륹ץȤνλ



Conditional statement: PӾPΥå

sh ϥǤϡϴŪ

[␣P␣]

Ȥǽ񤫤롥

warning.png [ ȾPδ֤ˤϥڡ␣ɬס

# ܤȡ[ ϾPå롤ȤǽĥޥɤǤ롥

PϡŪˡñ黻ҡפ黻ҡפȹܤȤ߹碌

statement for files: ե˴ؤP

-e␣̾
̾Υե뤬¸ߤʤп
-d␣̾
̾Υǥ쥯ȥ꤬¸ߤʤп
-f␣̾
̾̾ե뤬¸ߤʤп
-r␣̾
̾ɤ߹߲ǽʥե뤬¸ߤʤп
-w␣̾
̾ν񤭹߲ǽʥե뤬¸ߤʤп
-x␣̾
̾μ¹Բǽʥե뤬¸ߤʤп
-s␣̾
̾Υ 0 礭ʥե뤬¸ߤʤп

notes.png ½

湽¤ if ΤȤμ½ʤɤ򻲹ͤˤơξʤ٤ץȤƤߤ.

statement for string: ʸ˴ؤP

-z␣ʸ
ʸĹʤп
-n␣ʸ
ʸĹǤʤп
ʸ1␣==␣ʸ2
ʸп(ˤ=פĤĤʤäƤ)
ʸ1␣!=␣ʸ2
ʸ󤬰ۤʤп

notes.png ½

湽¤ if ΤȤμ½ʤɤ򻲹ͤˤơξʤ٤ץȤƤߤ.

statement for number: ͤ˴ؤP

1␣-eq␣2
ͤп
1␣-ne␣2
ͤۤʤп
1␣-gt␣2
1 > 2 ʤп
1␣-ge␣2
1 2 ʤп
1␣-lt␣2
1 < 2 ʤп
1␣-le␣2
1 2 ʤп

warning.png PȽˤȤʤ

notes.png ½

湽¤ if ΤȤμ½ʤɤ򻲹ͤˤơξʤ٤ץȤƤߤ.

other statement: ¾ P

!P
NOTɽ. PΩʤп
P1␣-a␣P2
ANDɽ. ξξPΩƤп
P1␣-o␣P2
ORɽ. ɤ餫ξPΩƤп
( P )
P򥰥롼ײ롥 ʣʾP񤯤Ȥˤϡ տޤȰۤʤ̤ˤʤʤˤѶŪ˻Ȥ٤

notes.png ½

湽¤ if ΤȤμ½ʤɤ򻲹ͤˤơξʤ٤ץȤƤߤ.



Report: ݡ

ʲβˤĤǽ¤긭ĴȹͻԤ
AppliedMath7-Report-07
Ȥ̾Ĥ e-mail ˤƶ˥ݡȤȤФ. ʤݡȤ e-mail TeX Ǻ̤ˤФƤ褤.

Exercises:

  1. 1 ˡֶۡפ򡤰2 ˡפȡ ζۤǤڤꤿǽŪˤˤʤ뤫׻륷륹ץȤ补 10 1 (ʣ)Ȥ롥
    warning.png read ޥɤʤɤȤ鷺륹ץȤΰ򤭤Ƚ뤳ȡ

  2. եõ륷륹ץȤ补 εǽäƤ褦ˤ补
    1. եΥХååץե뤬뤫ɤĴ٤ơ
      • ̵ 롥
      • ο3İʲ ˥Хååץե롥
      • ο3Ĥ¿ ָŤХååץեõƤ顤 ˥Хååץե롥
    2. ʾκȤԤäƤ顤եõ롥

  3. ޤǤμȲǡ륹ץȤѤƲǤ꤬ФԤ



about Icons, ClipArts

For details, see JNorth_arrow-right-sm.png this.