免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1880 | 回复: 2
打印 上一主题 下一主题

shell 函数处理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-13 18:57 |只看该作者 |倒序浏览
1.表达式求值

(())
一种是使用双括号,实际上是(())双括号里面是用c的语法来做,例如
for (( i=0;i<10;++i )); do
...
done

expr
因为shell中的内容都是当成字符串来处理,所以对于数字值,要处理起来,就比较麻烦了,这时候,用expr就比较好办

expr计算参数值,另外它还有常用的运算符
\|    如果它飞null 或者非0, 返回第一个表达式,否则返回第二个
\&    如果非null或者非0, 返回第一个表达式,否则返回0
= \> \>= \< \<= !=
+ - \* / %

2. shell的退出状态
使用:
exit 整数
来返回给其父级进程一个退出状态,其中整数的范围是0-255. 若成功执行脚本,则返回0,否则返回非0值。
一般来说,用程序员可以自己约定退出状态,方便在自己程序中调用

测条件测试命令的值
[ ] 表示空,一定是false,其他非零值包括0(当成字符串)也都是true
0 1 -1 都是真,因为是字符串,非空的
直接写$xyz,是false,因为没有赋值过,所有为null


3.定义函数

function fun() {
local i=""
命令列表
; #注意这里一定会有个分号!
}

其中local 是用来声明本地变量的。与之相对的是去全局变量,在shell脚本的规矩中,全局变量使用后,如果没用,必须unset

shell的函数没有参数,但是可以有返回值,使用return来返回值。

之后可以将函数当成内部命令使用

例如

function get(){
#function get(){
function get(){
echo "1";
}
get;
get;

论坛徽章:
0
2 [报告]
发表于 2011-01-13 18:58 |只看该作者
本帖最后由 liyihongcug 于 2011-01-13 19:00 编辑

函数必须定义在前面的.否则  line 1: get: command not found

Linux shell 脚本实例2011-01-07 19:54
1. 写一个脚本,利用循环计算10的阶乘#!/bin/sh
factorial=1
for a in `seq 1 10`
do
       factorial=`expr $factorial \* $a`
done
echo "10! = $factorial"


2. 写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。#!/bin/sh
unset var

while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done
//////////////////////////////////////////
#!/bin/sh
unset var
while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done


3. 写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和


4.一个函数,利用shift计算所有参数乘积,假设参数均为整数


#! /bin/sh
result=1
while [ $# -gt 0 ]
do
     result=`expr $result * $1`
     shift
done
echo $result

5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。#!/bin/shcase ${1##*.tar.} in     bz2)         tar jxvf $1         ;;     gz)         tar zxvf $1         ;;     *)         echo "wrong file type"esac6.写一个脚本以方便用户查询rpm的相关信息。这个脚本首先提示用户选择查询依据,比如文件名,包名,全部等。然后提示用户选择查询信息,比如包名,包里所包含的所有文件,包的信息等。然后询问是否继续查询,是则循环刚才的过程,否则退出。


   


  


#!/bin/bash
# favourite OS.       samli          2004.4.19
echo "What is your favourite OS?"
select var in "Linux" "UNIX" "Windows" "Other"; do
echo "You have selected $var."
#break
done



# !/bin/bash
# list a content summary of a number of RPM packages           samli         2004.4.19
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/Thizlinux/RPMS/*.rpm

for rpmpackage in $*;
do
if [ -r "$rpmpackage" ];
then
echo "=============== $rpmpackage =============="
/bin/rpm -qip $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
fi
done

#!/bin/bash
if [ $# -le 0 ]
then
echo "Not enough parameters"
exit 1
fi
#string="2 3 4 5 6"
#set string
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum

#! /bin/bash
echo "*******************************"
echo "Please select your operation:"
echo " 1 Copy"
echo " 2 Delete"
echo " 3 Backup"
echo "*******************************"
read op
case $op in
     C)
       echo "your selection is Copy"
     ;;
     D)
       echo "your selection is Delete"
     ;;
     B)
       echo "your selection is Backup"
     ;;
     *)
       echo "invalid selection"
esac
#! /bin/sh
while true
do
   echo "*******************************"
   echo "Please select your operation:"
   echo " 1 Copy"
   echo " 2 Delete"
   echo " 3 Backup"
   echo " 4 Quit"
   echo "*******************************"
   read op
   case $op in
        C)
          echo "your selection is Copy"
           ;;
        D)
          echo "your selection is Delete"
          ;;
        B)
         echo "your selection is Backup"
           ;;
        Q)
          echo "Exit ..."
               break
          ;;
        *)
          echo "invalid selection,please try again"
   esac
done
#! /bin/sh
a=10
b=20
c=30
value1=`expr $a + $b + $c`
echo "The value of value1 is $value1"
value2=`expr $c / $b`
echo "The value of value2 is $value2"
value3=`expr $c * $b`
echo "The value of value3 is $value3"
value4=`expr $a + $c / $b`
echo "The value of value4 is $value4"
var4=`expr $value4 - $value2`
echo $var4


#! /bin/sh
sum=0
for i in $*
do
    sum=`expr $sum + $i`
done
echo $sum
abc=123
echo $abc


###定义函数

example1()

{

abc=456

}

###调用函数

example1

echo $abc

abc=234234

example1

echo $abc

###定义函数,使用参数

example2()

{

echo $1

echo $2

}


###调用函数,向它传递参数

example2 abc bbb

example2 dksdfsdfsfaa bbb

#!/bin/bash
echo "please input a file name:"
read file_name
if [ -d $file_name ]
then
echo "$file_name is a directory"
elif [ -f $file_name ]
then
echo "$file_name is a regular file"
elif [ -c $file_name -o -b $file_name ]
then
echo "$file_name is a device file"
else
echo "$file_name is an unkonwn file"
fi


#! /bin/sh
read first second third
echo "the first parameter is $first"
echo "the second parameter is $second"
echo "the third parameter is $third"


#! /bin/sh
if [ $# -ne 2 ]; then
   echo "Not enough parameters"
   exit 0
fi

if [ $1 -eq $2 ]; then
   echo "$1 equals $2"
elif [ $1 -lt $2 ]; then
   echo "$1 less than $2"
elif [ $1 -gt $2 ]; then
   echo "$1 greater than $2"
fi

#clear
#:trap "" 2 18 20 24
#e cho "***********************************************************************"
#e cho " HI! You changed my root password"
#e cho " This is a little punishment for you then you can use the shell"
#e cho " You must answer my three questions and type "yes" or "no" to answer"
#
#answer="no"
#
#e cho "***********************************************************************"
#e cho "Please press "Enter" key to continue"
#read
#while [ $answer = "no" ]
#do
#     echo
#     echo "1.Are you a boy ? (yes/no)"
#     read answer
#done
#
#answer="no"
#
#until [ $answer = "yes" ]
#do
#     echo
#     echo "2.Are you foolish ? (yes/no)"
#     read answer
#done
#
#answer="no"
#
#until [ $answer = "yes" ]
#do
#     echo
#     echo "3.Do you love me ? (yes/no)"
#     read answer
#done
#
#e cho "***********************************************************************"
#e cho
#e cho "Now ! I know you are a foolish girl and you love me."
#e cho
#e cho "So you can continue"
#e cho
#e cho "**************************************************************"
#e cho
#sleep 3
#e cho "    (haha ! It is just a joke)"
i=1
echo "this time i is not equal to 4"
while [ $i -le 8 ]
do
       ((i=i+1))
       if [ $i -eq 4 ]
       then
              echo "this time i is equal to 4"
              continue
       fi
       echo "this time i is not equal to 4"
done
echo "The command is $0"
echo "The first argument is $1, the second argument is $2"
echo "The entire command is $0 $1 $2"
echo "And there are $# arguments"
echo "The end of testing"


#! /bin/sh
if [ $# -gt 1 ]
then
echo "Too many parameters"
exit 1
fi

if [ $# -eq 0 ]
then
echo "Too few parameters"
exit 100
fi

if [ ! -d $1 ]
then
echo "Usage : $0 directory"
exit 1
fi

#for i in $1/*
#do
# if [ -x $i -a ! -d $i ]
#    then
#     ls $i
# fi
#done


#!/bin/sh

………………

1. 写一个脚本,利用循环计算10的阶乘#!/bin/sh
factorial=1
for a in `seq 1 10`
do
       factorial=`expr $factorial \* $a`
done
echo "10! = $factorial"


2. 写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。#!/bin/sh
unset var

while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done
//////////////////////////////////////////
#!/bin/sh
unset var
while [ "$var" != "end" ]
do
     echo -n "please input a number: "
     read var
     if [ "$var" = "end" ]
     then
         break
     fi
     echo "var is $var"
done


3. 写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和


4.一个函数,利用shift计算所有参数乘积,假设参数均为整数


#! /bin/sh
result=1
while [ $# -gt 0 ]
do
     result=`expr $result * $1`
     shift
done
echo $result

5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。#!/bin/shcase ${1##*.tar.} in     bz2)         tar jxvf $1         ;;     gz)         tar zxvf $1         ;;     *)         echo "wrong file type"esac6.写一个脚本以方便用户查询rpm的相关信息。这个脚本首先提示用户选择查询依据,比如文件名,包名,全部等。然后提示用户选择查询信息,比如包名,包里所包含的所有文件,包的信息等。然后询问是否继续查询,是则循环刚才的过程,否则退出。


   


  


#!/bin/bash
# favourite OS.       samli          2004.4.19
echo "What is your favourite OS?"
select var in "Linux" "UNIX" "Windows" "Other"; do
echo "You have selected $var."
#break
done



# !/bin/bash
# list a content summary of a number of RPM packages           samli         2004.4.19
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/Thizlinux/RPMS/*.rpm

for rpmpackage in $*;
do
if [ -r "$rpmpackage" ];
then
echo "=============== $rpmpackage =============="
/bin/rpm -qip $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
fi
done

#!/bin/bash
if [ $# -le 0 ]
then
echo "Not enough parameters"
exit 1
fi
#string="2 3 4 5 6"
#set string
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum

#! /bin/bash
echo "*******************************"
echo "Please select your operation:"
echo " 1 Copy"
echo " 2 Delete"
echo " 3 Backup"
echo "*******************************"
read op
case $op in
     C)
       echo "your selection is Copy"
     ;;
     D)
       echo "your selection is Delete"
     ;;
     B)
       echo "your selection is Backup"
     ;;
     *)
       echo "invalid selection"
esac
#! /bin/sh
while true
do
   echo "*******************************"
   echo "Please select your operation:"
   echo " 1 Copy"
   echo " 2 Delete"
   echo " 3 Backup"
   echo " 4 Quit"
   echo "*******************************"
   read op
   case $op in
        C)
          echo "your selection is Copy"
           ;;
        D)
          echo "your selection is Delete"
          ;;
        B)
         echo "your selection is Backup"
           ;;
        Q)
          echo "Exit ..."
               break
          ;;
        *)
          echo "invalid selection,please try again"
   esac
done
#! /bin/sh
a=10
b=20
c=30
value1=`expr $a + $b + $c`
echo "The value of value1 is $value1"
value2=`expr $c / $b`
echo "The value of value2 is $value2"
value3=`expr $c * $b`
echo "The value of value3 is $value3"
value4=`expr $a + $c / $b`
echo "The value of value4 is $value4"
var4=`expr $value4 - $value2`
echo $var4


#! /bin/sh
sum=0
for i in $*
do
    sum=`expr $sum + $i`
done
echo $sum
abc=123
echo $abc


###定义函数

example1()

{

abc=456

}

###调用函数

example1

echo $abc

abc=234234

example1

echo $abc

###定义函数,使用参数

example2()

{

echo $1

echo $2

}


###调用函数,向它传递参数

example2 abc bbb

example2 dksdfsdfsfaa bbb

#!/bin/bash
echo "please input a file name:"
read file_name
if [ -d $file_name ]
then
echo "$file_name is a directory"
elif [ -f $file_name ]
then
echo "$file_name is a regular file"
elif [ -c $file_name -o -b $file_name ]
then
echo "$file_name is a device file"
else
echo "$file_name is an unkonwn file"
fi


#! /bin/sh
read first second third
echo "the first parameter is $first"
echo "the second parameter is $second"
echo "the third parameter is $third"


#! /bin/sh
if [ $# -ne 2 ]; then
   echo "Not enough parameters"
   exit 0
fi

if [ $1 -eq $2 ]; then
   echo "$1 equals $2"
elif [ $1 -lt $2 ]; then
   echo "$1 less than $2"
elif [ $1 -gt $2 ]; then
   echo "$1 greater than $2"
fi

#clear
#:trap "" 2 18 20 24
#e cho "***********************************************************************"
#e cho " HI! You changed my root password"
#e cho " This is a little punishment for you then you can use the shell"
#e cho " You must answer my three questions and type "yes" or "no" to answer"
#
#answer="no"
#
#e cho "***********************************************************************"
#e cho "Please press "Enter" key to continue"
#read
#while [ $answer = "no" ]
#do
#     echo
#     echo "1.Are you a boy ? (yes/no)"
#     read answer
#done
#
#answer="no"
#
#until [ $answer = "yes" ]
#do
#     echo
#     echo "2.Are you foolish ? (yes/no)"
#     read answer
#done
#
#answer="no"
#
#until [ $answer = "yes" ]
#do
#     echo
#     echo "3.Do you love me ? (yes/no)"
#     read answer
#done
#
#e cho "***********************************************************************"
#e cho
#e cho "Now ! I know you are a foolish girl and you love me."
#e cho
#e cho "So you can continue"
#e cho
#e cho "**************************************************************"
#e cho
#sleep 3
#e cho "    (haha ! It is just a joke)"
i=1
echo "this time i is not equal to 4"
while [ $i -le 8 ]
do
       ((i=i+1))
       if [ $i -eq 4 ]
       then
              echo "this time i is equal to 4"
              continue
       fi
       echo "this time i is not equal to 4"
done
echo "The command is $0"
echo "The first argument is $1, the second argument is $2"
echo "The entire command is $0 $1 $2"
echo "And there are $# arguments"
echo "The end of testing"


#! /bin/sh
if [ $# -gt 1 ]
then
echo "Too many parameters"
exit 1
fi

if [ $# -eq 0 ]
then
echo "Too few parameters"
exit 100
fi

if [ ! -d $1 ]
then
echo "Usage : $0 directory"
exit 1
fi

#for i in $1/*
#do
# if [ -x $i -a ! -d $i ]
#    then
#     ls $i
# fi
#done


#!/bin/sh

………………




http://hi.baidu.com/briony545/bl ... ec4fd87acbe170.html

论坛徽章:
0
3 [报告]
发表于 2011-04-28 13:18 |只看该作者
$ more tt.sh
fun(){
        echo $1
}
fun aa
echo $1
$
$ bash tt.sh hello
aa
hello

  1 #!/bin/bash
  2
  3 Display()
  4 {
  5         echo $1
  6         return 1
  7 }
  8
  9 Display xxx-server
10
11 if [ $? != 0 ] ; then
12         echo "true"
13 else
14         echo "false"
15 fi
16
17 Display "xxx-server"
18 echo $?
19 echo $1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP