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