关于变量,还需要知道几个与其相关的Linux命令。
env用于显示用户环境区中的变量及其取值;set用于显示本地数据区和用户环境区中的变量及其取值;unset用于删除指定变量当前的取值,该值将被指定为NULL;export命令用于将本地数据区中的变量转移到用户环境区。
下面我们来看一个更复杂的例子,结合这个例子,我们来讲述Shell Script的语法。
1 #!/bin/bash
2 # we have less than 3 arguments. Print the help text:
3 if [ $# -lt 3 ] ; then
4 cat <
5 ren -- renames a number of files using sed regular expressions
6
7 USAGE: ren 'regexp' 'replacement' files...
8 EXAMPLE: rename all *.HTM files in *.html:
9 ren 'HTM$' 'html' *.HTM
10
11 HELP
12 exit 0
13 fi
14 OLD="$1"
15 NEW="$2"
16 # The shift command removes one argument from the list of
17 # command line arguments.
18 shift
19 shift
20 # $* contains now all the files:
21 for file in $*; do
22 if [ -f "$file" ] ; then
23 newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
24 if [ -f "$newfile" ]; then
25 echo "ERROR: $newfile exists already"
26 else
27 echo "renaming $file to $newfile ..."
28 mv "$file" "$newfile"
29 fi
30 fi
31 done