DAWNING %暖光 科技计算表来 Shell编程 陈伟
Shell 编程 陈伟
DAWNING 多曙光 Index 件技计算 The shell of Linux Bourne Shell Programming find.and.regular.expression 。text manipulation Shell编程 2
Index • The shell of Linux • Bourne Shell Programming • find.and.regular.expression • text manipulation Shell 编程 2
DAWNING 乡暖光 The shell of Linux 科技计算素来 ·Bourne shell(sh), 。C shell(csh), ·Korn shell(ksh), 。TC shell(tcsh), Bourne Again shell (bash) Shell编程 3
The shell of Linux • Bourne shell (sh), • C shell (csh), • Korn shell (ksh), • TC shell (tcsh), • Bourne Again shell (bash) Shell 编程 3
DAWNING %曙光 DAWNING Difference between programming and计”来 scripting languages Programming languages are generally a lot more powerful and a lot faster than scripting languages.Programming languages generally start from source code and are compiled into an executable.This executable is not easily ported into different operating systems. A scripting language also starts from source code,but is not compiled into an executable.Rather,an interpreter reads the instructions in the source file and executes each instruction.Interpreted programs are generally slower than compiled programs.The main advantage is that you can easily port the source file to any operating system. Shell编程 4
Difference between programming and scripting languages – Programming languages are generally a lot more powerful and a lot faster than scripting languages. Programming languages generally start from source code and are compiled into an executable. This executable is not easily ported into different operating systems. – A scripting language also starts from source code, but is not compiled into an executable. Rather, an interpreter reads the instructions in the source file and executes each instruction. Interpreted programs are generally slower than compiled programs. The main advantage is that you can easily port the source file to any operating system. Shell 编程 4
DAWNING 乡曙光 料技计算未来 The first bash program We must know how to use a text editor.There are two major text editors in Linux: -vi,emacs (or xemacs). So fire up a text editor;for example: -$vi& and type the following inside it: -#!/bin/bash echo "Hello World" The first line tells Linux to use the bash interpreter to run this script. We call it hello.sh.Then,make the script executable: -chmod 700 hello.sh -$1s-1 -rwx------hello.sh Shell编程 5
The first bash program • We must know how to use a text editor. There are two major text editors in Linux: – vi, emacs (or xemacs). • So fire up a text editor; for example: – $ vi & and type the following inside it: – #!/bin/bash echo “Hello World” • The first line tells Linux to use the bash interpreter to run this script. We call it hello.sh. Then, make the script executable: – $ chmod 700 hello.sh – $ ls –l -rwx------ hello.sh Shell 编程 5
DAWNING 多曙光 The first bash program To execute the program: -hello.sh -bash:hello.sh:command not found The home directory (where the command hello.sh is located) is not in the variable PATH echo SPATH bin:/usr/bin:... We must specify the path of hello.sh -$/home/srinaldi/Scripts/hello.sh -$./hello.sh Shell编程 6
The first bash program • To execute the program: – $ hello.sh -bash: hello.sh: command not found The home directory (where the command hello.sh is located) is not in the variable PATH – echo $PATH :bin:/usr/bin:… We must specify the path of hello.sh – $/home/srinaldi/Scripts/hello.sh – $./hello.sh Shell 编程 6
DAWNING 乡曙光 科技计算素米 The second bash program We write a program that copies all files into a directory,and then deletes the directory along with its contents.This can be done with the following commands: -s mkdir trash cp trash rm -rf trash s mkdir trash Instead of having to type all that interactively on the shell,write a shell program instead: -cat trash #!/bin/bash this script deletes some files cp trash rm -rf trash mkdir trash echo "Deleted al1 filsell编程 7
The second bash program • We write a program that copies all files into a directory, and then deletes the directory along with its contents. This can be done with the following commands: – $ mkdir trash $ cp * trash $ rm -rf trash $ mkdir trash • Instead of having to type all that interactively on the shell, write a shell program instead: – $ cat trash #!/bin/bash # this script deletes some files cp * trash rm -rf trash mkdir trash echo “Deleted all files!” Shell 编程 7
DAWNING 乡曙光 科技计算来 ·#!/bin/bash#if1.sh echo -n "Enter a number 1<x 10: read num if["$num"-1t10]; then if "Snum"-gt 1 ]then echo "Snum*Snum=$(($num*$num))" else echo "Wrong insertion ! fi else echo "Wrong insertion ! fi
• #!/bin/bash # if1.sh echo -n “Enter a number 1 < x < 10: " read num if [ “$num” -lt 10 ]; then if [ “$num” -gt 1 ]; then echo “$num*$num=$(($num*$num))” else echo “Wrong insertion !” fi else echo “Wrong insertion !” fi
DAWNING 乡曙光 科技计算未来 Bourne Shell Programming Shell编程
• Bourne Shell Programming Shell 编程 74
DAWNING 曙光 Bourne Shell Programming 算未 ·Certainly the most popular shell is“bash”.Bash is the shell that will appear in the GNU operating system.Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh)and C shell (csh). bash is not only an excellent command line shell, but a scripting language in itself.Shell scripting allows us to use the shell's abilities and to automate a lot of tasks that would otherwise require a lot of commands. Shell编程 75
Bourne Shell Programming • Certainly the most popular shell is “bash”. Bash is the shell that will appear in the GNU operating system. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). • bash is not only an excellent command line shell, but a scripting language in itself. Shell scripting allows us to use the shell's abilities and to automate a lot of tasks that would otherwise require a lot of commands. Shell 编程 75