linux shell编程基础1

来源:互联网 发布:mac ps免费破解版下载 编辑:程序博客网 时间:2024/05/16 07:24

1.shell编程准备知识:

(1).shell编程中,很多时候用到反引号(`,就是和~一个键的那个),反引号里面的内容作为命令直接解析,很多学习shell编程的人容易把反引号看错成引号,引起程序总是出错。

(2).shell脚本的第一句一般是类似如下一句话:#!/bin/bash。表示该脚本内容使用bash命令解析,也可以是#!/bin/sh等,本文以bash为例。这并不是脚本必须的,但是推荐这么做。

(3).shell脚本中除了第一句的:#!/bin/bash外,其他语句中以”#”开头的表示注释。

2.shell编程基本步骤:

(1).编辑脚本文件并保存。

(2).使用chmod命令修改脚本文件的权限为可执行。

(3).执行脚本查看结果。

3.shell脚本的调试:

(1).echo打印输出调试。

(2).”sh –x 脚本名称方式执行脚本,同时显示脚本中所有变量的值。

(3).”sh –n 脚本名称方式执行脚本,返回脚本中所有的语法错误。

4.shellif条件判断:

(1).最简单的if条件判断:

语法格式:if 表达式

then

                     操作

                Fi

注意:”[]”是条件判断,里面的内容要合它用一个空格分隔,,同时ifelif等关键字和判断条件之间也要用一个空格分隔。

例如:

#!/bin/bash

#if statement1

if [ "10" -lt "12" ]

then

  echo "Yes,10 is lessthan 12"

fi

(2).if-else条件判断:

语法格式:if 条件判断1; then

                     操作1

               else

                     操作2

               fi

例如:

#!/bin/bash

# if statement2

if cp myfile.bak myfile; then

   echo "goodcopy"

else

   echo "`basename$0`:error could not copy the files"<&2

fi

(3).if-elseif-else条件判断:

语法格式:if 表达式1; then

                     操作1

                elif 表达式2; then

                     操作2

                ……

               else

                     操作n

               Fi

注意:如果then和条件判断语句在同一行,则中间需要用分号(;)分隔,如果不在同一行,则不需要。

例如:

#!/bin/bash

#if statement3

echo -n "Enter your name:"

read NAME

if [ -z $NAME ] || [ "$NAME" = "" ];then

 echo "You did notenter a name"

elif [ "$NAME" = "root" ];then

 echo "Hello root"

elif [ "$NAME" = $LOGNAME ];then

 echo "Hello$NAME"

else

 echo "You are not rootor $LOGNAME,but hi $NAME"

fi

5.shell中多路分支语句:

shell中使用case语句可以实现类似javaswitch的多路分支选择功能。

语法格式:case 字符串 in

                     1)

                            操作1

;;

                     2|3)

                            操作2

;;

……

*)

操作n

;;

Esac

注意:每个操作分支后的两个分号(;;)类似与java switch语句中,每个分支后的break关键字,其作用是防止case语句穿透。

例如:

#!/bin/bash

#case statement

echo -n "Enter a number from 1 to 3:"

read ANS

case $ANS in

1)

echo "You select 1"

;;

2)

echo "You select 2"

;;

3)

echo "You select 3"

;;

*)

echo "`basename $0`:this is not between 1 and3">&2

exit;

;;

esac

6.shell中的循环控制语句:

(1).for循环:

语法格式:for 变量 in 列表

                     do

                            操作

                     done

例如:

#!/bin/bash

#for-loop statement

for i in 1 2 3 4 5 6 7 8 9 10

do

 echo -n "$i"

done

(2).while循环:

语法格式:while 表达式

                     do

                            操作

                     Done

注意:当表达式条件符合时,while循环执行。

例如:

#!/bin/bash

#while-loop statement

echo "Please press <ctrl>+D exit the input"

while echo -n "Please input the movie you like best:"

read Film

do

 echo "Yeah,$Film is agood movie!"

done

(3).until循环:

语法格式:until 表达式

                     do

                            操作

                     done

注意:和while循环正好相反,当表达式条件不符合时,循环执行。

例如:

#!/bin/bash

#until-loop statement

part="/backup"

LOOK_OUT=`df|grep "$part"|awk '{print $5}'|sed 's/%//g'`

echo $LOOK_OUT

until [ "$LOOK_OUT" -gt "90" ]

do

  echo "Filesystem/backup is nearly full"

  LOOK_OUT=`df|grep"$part"|awk '{print $5}'|sed 's/%//g'`

  sleep 3600

done

7.select语句:

用于从一组不同的值中进行选择,和用户交互。

语法格式:select var in 1 2 ……; do

                     break;

                done

例如:

#!/bin/bash

#select statement

echo "What is your favourite OS?"

select var in "Linux" "Gnu Hurd" "FreeBSD" "Windows" "Others";do

            break;

done

echo "You have selected $var"

8.读取用户输入:

使用read可以读取用户的输入。

语法格式:read 变量名

注意:只能读取一个值,不能有空格。

例如:

#!/bin/bash

#The read command

echo -n "First Name:"

read firstname

echo -n "Last Name:"

read lastname

echo -e "Your First Name is:$firstname/n"

echo -e "Your Last Name is:$lastname/n"

9.位置参数:

Shell编程中位置参数很重要,用于获取用户在运行脚本时输入的多于一个以上的参数,参数之间用空格分隔。

$0:表示文件名。$1~$9:代表第1至第9个位置参数。$#:代表总的参数个数。

$*$@:代表所有的位置参数。

注意:$9以后的位置参数需要使用”{}”,如:{$10}{$11}等。

例如:

#!/bin/bash

#This shell is test the place parms

echo "this is the parm name:$0"

echo "this is the place parm first:$1"

echo "this is the place parm second:$2"

echo "this is the place parm third:$3"

echo "this is the place parm fourth:$4"

echo "this is the place parm fifth:$5"

echo "this is the place parm six:$6"

echo "this is the place parm seventh:$7"

echo "this is the place parm eigth:$8"

echo "this is the place parm ninth:$9"

echo "show the number of parms:$#"

echo "show all the parms in the script:$*"

echo "show the process id:$$"

echo "show the previous command status:$?"

shift 2

echo "this is the first:$1"

echo "this is the second:$2"

10.shift命令:

用于在循环遍历位置参数时,对位置参数进行移位,即从上一个位置参数移向下一个位置参数。

如:

#!/bin/bash

#shift position parameters

until [ -z "$1"]

do

 echo -n "$1"

  shift

done

echo

exit 0

11.shell中的函数:

Shell中使用function关键字声明函数,可以将一组功能相关的代码封装组合成一个方法,以提高代码的复用性。

语法格式:function 函数名(){

                     函数体

                     return

}

函数调用:直接函数名即可调用。注意函数必须先声明定义,然后在使用。

例如:

#!/bin/bash

#function

function hello()

{

echo "Hello,today is `date`"

}

echo "now going to the function hello"

hello

echo "back from the function"

12.shell脚本中调用其他脚本中的函数:

为了程序代码复用性的提高,shell中也可以调用其他脚本文件中的函数,在调用之前需要使用”. 脚本文件名方式将要调用函数所在的脚本文将引入即可。

例如:

#!/bin/bash

#funtion

#import source function

. function_2.sh

echo "now going to the function hello"

hello hello

echo "back from the function"

13.Linux中的正则表达式:

^:只匹配行首。         $:只匹配行尾。         *:匹配0个或多个任意字符。

[]:匹配”[]”以内的字符,可以是单字符,也可以是字符序列,可以使用”-”表示”[]”范围内的字符序列。

/:转义字符,屏蔽一个元字符的特殊含义。   .:匹配任意单个字符。

pattern/{n/}:用来匹配前面pattern出现次数,n为出现的次数。

pattern/{n,/}:含义同上,但是pattern最少出现n次。

Pattern/{n,m/}:含义同上,但是pattern的出现次数在nm之间。

原创粉丝点击