Shell脚本编程基础

分类:shell编程 阅读:10363 次

Shell简介

Shell简单的说就是命令解析器,将用户输入的指令转换为相应的机器能够执行的程序。

Shell脚本是一个包含一系列命令序列的文本文件(批处理)。当运行这个脚本文件时,文件中包含的命令序列将得到执行。

HelloWorld

Shell脚本的第一行必须是如下格式: #!/bin/bash

符号#!用来指定该脚本文件的解析程序。例中使用bash,也可以使用其他shell。如#!/bin/sh。

当编辑好脚本后,必须使其具有可执行属性。 chmod +x filename

Shell脚本hello.sh

view plaincopy to clipboardprint?
#!/bin/bash
echo "hello world!"
mkdir ./helloworld
#!/bin/bash
echo "hello world!"
mkdir ./helloworld

Shell中的变量

在Shell编程中,所有的变量都由字符串组成,并不需要预先对变量声明。

Shell脚本s1.sh

view plaincopy to clipboardprint?
#!/bin/sh
#号后面是注释
#set variable a
a="hello"
#print a
echo "A is: $a"
#!/bin/sh
#号后面是注释
#set variable a
a="hello"
#print a
echo "A is: $a"

命令行参数传递

$#:传入脚本的命令行参数个数

$*:所有的命令行参数,各参数值之间留有空格

$0:命令本身(shell文件名)

$1:第一个命令行参数

$2:第二个命令行参数

shell脚本s2.sh

view plaincopy to clipboardprint?
#!/bin/sh
echo "numer of vars:"$#
echo "values of vars:"$*
echo "value of var1:"$1
echo "value of var2:"$2
echo "value of var3:"$3
echo "value of var4:"$4
#!/bin/sh
echo "numer of vars:"$#
echo "values of vars:"$*
echo "value of var1:"$1
echo "value of var2:"$2
echo "value of var3:"$3
echo "value of var4:"$4

运行 ./s2.sh a b c d

输出结果:

numer of vars:4
values of vars:a b c d
value of var1:a
value of var2:b

value of var3:c

value of var4:d

Shell中的局部变量

shell脚本s3.sh

view plaincopy to clipboardprint?
#!/bin/bash
hello="var1"
echo $hello
function funcl
{
local hello="var2"
echo $hello
}
funcl
echo $hello
#!/bin/bash
hello="var1"
echo $hello
function funcl
{
local hello="var2"
echo $hello
}
funcl
echo $hello

在变量首次被赋值时加上local关键字可以声明一个局部变量

注意:(1)变量赋值时,“=”左右两边都不能有空格

(2)BASH中的语句结尾不需要分号

Shell中的控制结构

if语句

if[expression]

then

#code block

if

if[expression]

then

#code block

else

#code block

fi

比较运算符

比较操作 整数操作 字符串操作 比较操作 整数操作 字符串操作
相同 -eq = 大于或等于 -ge
不同 -ne != 小于或等于 -le
大于 -gt > 为空 -z
小于 -lt < 不为空 -n


使用实例:

比较整数a是否大于整数b: if[ $a -gt $b ] 判断字符串a是否为空: if[ -z $a ]

注意:

(1)在"["和"]"符号的左右都留有空格 (2)"="左右都有空格

shell脚本s4.sh

view plaincopy to clipboardprint?
#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
fi
if [ $a -eq $b ] ; then
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a<number b"
fi
fi
#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
fi
if [ $a -eq $b ] ; then
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a<number b"
fi
fi

判断

-e 文件已经存在 -f 文件是普通文件 -s 文件大小不为零 -d 文件是一个目录

-r 文件对当前用户可以读取 -w 文件对当前用户可以写入 -x 文件对当前用户可以执行

shell脚本s5.sh

view plaincopy to clipboardprint?
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder"
[ -f "$folder" ] || echo "this is not file"
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder"
[ -f "$folder" ] || echo "this is not file"

shell脚本s6.sh

view plaincopy to clipboardprint?
#!/bin/bash
DIR=$1
#if the string empty
if [ "$DIR" = " " ]
then
echo "usage: `basename $0` directory to create" >&2
exit 1
fi
echo "dir" $DIR
if [ -d $DIR ]
then
echo "The directory already exist"
exit 0
else
echo "The directory does exist"
echo -n "Create is now? [Y/N]:"
read create
if [ "$create" = "y" ] || [ "$create" = "Y" ]
then
echo "creating now"
if [ mkdir $DIR ]
DIR=" "
fi

if [ "$DIR" = " " ]
then
echo "create directory sucess"
else
echo "create directory error"
fi
elif [ "$create" = "n" ] || [ "$create" = "N" ]
then
echo "does not create directory"
exit 0
else
echo "Errors order"
exit 1
fi
fi
#!/bin/bash
DIR=$1
#if the string empty
if [ "$DIR" = " " ]
then
echo "usage: `basename $0` directory to create" >&2
exit 1
fi
echo "dir" $DIR
if [ -d $DIR ]
then
echo "The directory already exist"
exit 0
else
echo "The directory does exist"
echo -n "Create is now? [Y/N]:"
read create
if [ "$create" = "y" ] || [ "$create" = "Y" ]
then
echo "creating now"
if [ mkdir $DIR ]
DIR=" "
fi

if [ "$DIR" = " " ]
then
echo "create directory sucess"
else
echo "create directory error"
fi
elif [ "$create" = "n" ] || [ "$create" = "N" ]
then
echo "does not create directory"
exit 0
else
echo "Errors order"
exit 1
fi
fi

for循环

for循环结构与C语言中有所不同,在bash中for循环的基本结构式

for var in [list]

do

#code lock

done

其中$var是循环控制变量,[list]是var遍历的一个集合,do/done对包含了循环体。

另外,如果for和do写在同一行,必须在do前面加";"。

shell脚本s7.sh

view plaincopy to clipboardprint?
#!/bin/bash
for day in Sun Mon Tue Wed Thu Fri Sat
do
echo $day
done
#!/bin/bash
for day in Sun Mon Tue Wed Thu Fri Sat
do
echo $day
done

shell脚本统计当前目录下的文件数

view plaincopy to clipboardprint?
#!/bin/bash

counter=0
for files in *
do
counter=`expr $counter + 1`
done
echo "There are $counter files in `pwd` we need to process"
#!/bin/bash

counter=0
for files in *
do
counter=`expr $counter + 1`
done
echo "There are $counter files in `pwd` we need to process"

shell脚本将用户输入的数字按倒序的方式输出

view plaincopy to clipboardprint?
#!/bin/bash
echo -n "Pleasw enter number : "
read n
sd=0
rev=""
on=$n
echo "$n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
rev=$( echo $rev$sd)
done
echo "$on in a reverse order $rev"
#!/bin/bash
echo -n "Pleasw enter number : "
read n
sd=0
rev=""
on=$n
echo "$n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
rev=$( echo $rev$sd)
done
echo "$on in a reverse order $rev"

until循环

until循环的基本结构

until [condition]

do

#code block

done

while和until的区别在于while是为真时执行,until是为假时执行。

shell脚本 移动一个文件,如果目标存在,监视该文件,直到文件被删除才移动文件。

view plaincopy to clipboardprint?
#!/bin/bash
if [ "$1" = "" ] || [ "$2" = "" ]
then
echo "Please enter file name"
exit 1
fi
if [ -e $2 ]
then
echo "The file already exists"
until [ ! -f $2 ]
do
sleep 1
done
fi
if [ ! `mv $1 $2` ]
then
echo "mv sucessful"
else
echo "mv error"
fi
#!/bin/bash
if [ "$1" = "" ] || [ "$2" = "" ]
then
echo "Please enter file name"
exit 1
fi
if [ -e $2 ]
then
echo "The file already exists"
until [ ! -f $2 ]
do
sleep 1
done
fi
if [ ! `mv $1 $2` ]
then
echo "mv sucessful"
else
echo "mv error"
fi


case语句

case语句结构

case "$var" in

condition1)

;;

condion2)

;;

*)

default statments;;

esac

shell脚本 判断键盘输入大小写

view plaincopy to clipboardprint?
#!/bin/bash
echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
[A-Z] ) echo "Uppercase letter";;
[a-z] ) echo "Lowercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac
#!/bin/bash
echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
[A-Z] ) echo "Uppercase letter";;
[a-z] ) echo "Lowercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac