linux shell service,linux shell编程中$符号问题

一下是linux下的service脚本,有几个地方有点点疑问,请各位高手指点下。

1.第15行的echo $"${USAGE}",如果是我写的话,直接写echo $USAGE,请问这前者有什么优点呢?

2.第32行cd ${SERVICEDIR},如果是我写的话,直接写cd $SERVICEDIR,请问前者有什么优点?

3.第66行echo $"${SERVICE}: unrecognized service", 如果是我写的话,我直接写

echo "$SERVICE: unrecognized service",请问前者有什么优点?

1#!/bin/sh

2

3# Set up a default search path.

4PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"

5export PATH

6

7VERSION="`basename $0` ver. 0.91"

8USAGE="Usage: `basename $0`  | --status-all |

9[ service_name [ command | --full-restart ] ]"

10SERVICE=

11SERVICEDIR="/etc/init.d"

12OPTIONS=

13

14if [ $# -eq 0 ]; then

15   echo $"${USAGE}" >&2

16   exit 1

17fi

18

19cd /

20while [ $# -gt 0 ]; do

21  case "${1}" in

22    --help | -h | --h* )

23       echo $"${USAGE}" >&2

24       exit 0

25       ;;

26    --version | -V )

27       echo $"${VERSION}" >&2

28       exit 0

29       ;;

30    *)

31       if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then

32          cd ${SERVICEDIR}

33          for SERVICE in * ; do

34            case "${SERVICE}" in

35              functions | halt | killall | single| linuxconf| kudzu |

36                  *rpmorig | *rpmnew | *rpmsave | *~ | *.orig)

37                  ;;

38              *)

39                if [ -x "${SERVICEDIR}/${SERVICE}" ]; then

40                  env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" status

41                fi

42                ;;

43            esac

44          done

45          exit 0

46       elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then

47          SERVICE="${1}"

48          if [ -x "${SERVICEDIR}/${SERVICE}" ]; then

49            env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" stop

50            env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" start

51            exit $?

52          fi

53       elif [ -z "${SERVICE}" ]; then

54         SERVICE="${1}"

55       else

56         OPTIONS="${OPTIONS} ${1}"

57       fi

58       shift

59       ;;

60   esac

61done

62

63if [ -x "${SERVICEDIR}/${SERVICE}" ]; then

64   env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${OPTIONS}

65else

66   echo $"${SERVICE}: unrecognized service" >&2

67   exit 1

68fi

|

其实就和宏定义中的()一样 防止意外的组合

|

A double-quoted string preceded by a dollar sign (‘$’) will cause

the string to be translated according to the current locale. If the

current locale is C or POSIX, the dollar sign is ignored. If the

string is translated and replaced, the replacement is double-quoted.

cd ${SERVICEDIR} 和 cd $SERVICEDIR 没啥区别,加 {} 的作用体现在

像 "${SERVICE}: unrecognized service" 这种字符串中的替换,可以进行分组,不会出现意外

|

我觉得是一个习惯的问题。

具体简单的例子:

LIBNAME=libxxx

cp ${LIBNAME}.so . #这里的{}就是必需的,不然它会把$LIBNAME.so认为${LIBNAME}

还有当你知道了shell只接收$1, $9参数之后,你就更容易理解了:

$10在shell中被解释为 ${1}0

良好的习惯还有一个好处就是在不同的shell版本之间兼容。

|

cp ${LIBNAME}.so . #这里的{}就是必需的,不然它会把$LIBNAME.so认为${LIBNAME}

更正一下:

cp ${LIBNAME}.so . #这里的{}就是必需的,不然它会把$LIBNAME.so认为${LIBNAME.so}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值