shell脚本通用模板 [英] Shell script common template

查看:210
本文介绍了shell脚本通用模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以百万计的开发人员编写shell脚本来解决不同类型的任务。我用shell脚本来简化部署,生命周期管理,安装或仅仅作为一种胶水语言

Millions of developers write shell scripts to solve various types of tasks. I use shell scripts to simplify deployment, life-cycle management, installation or simply as a glue language.

我已经注意到是没有人真正关心的shell脚本的风格和品质。很多球队花很多时间固定的Java,C ++,...作风问题,但完全忽略了他们的shell脚本的问题。顺便说一句,平时也实现特定项目中的shell脚本,没有标准的方法,因此,人们可以找到几十种不同的,丑陋的和马车的脚本,S $ P $垫周围codeBase的。

What I've noticed is nobody actually cares about shell scripts style and quality. A lot of teams spend many hours fixing Java, C++, ... style issues, but totally ignore issues in their shell scripts. By the way, usually there is no standard way to implement a shell script within a particular project, so the one may find dozens different, ugly and buggy scripts, spread around the codebase.

要克服我的项目,这些项目的问题,我决定创建一个shell脚本模板,普遍和不够好。我会提供我的模板,是为了使这个问题有点更加有用。开箱即用这些模板为:

To overcome that issue in my projects I decided to create a shell script template, universal and good enough. I will provide my templates as is to make this question a bit more useful. Out of the box these templates provides:


  • 命令行参数处理

  • 同步

  • 一些基本的帮助

参数处理:getopts的(最新版本: <一个href=\"https://github.com/RenatGilmanov/shell-script-template\">shell-script-template@github)

Arguments handling: getopts (latest version: shell-script-template@github)

#!/bin/bash
# ------------------------------------------------------------------
# [Author] Title
#          Description
# ------------------------------------------------------------------

VERSION=0.1.0
SUBJECT=some-unique-id
USAGE="Usage: command -ihv args"

# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
    echo $USAGE
    exit 1;
fi

while getopts ":i:vh" optname
  do
    case "$optname" in
      "v")
        echo "Version $VERSION"
        exit 0;
        ;;
      "i")
        echo "-i argument: $OPTARG"
        ;;
      "h")
        echo $USAGE
        exit 0;
        ;;
      "?")
        echo "Unknown option $OPTARG"
        exit 0;
        ;;
      ":")
        echo "No argument value for option $OPTARG"
        exit 0;
        ;;
      *)
        echo "Unknown error while processing options"
        exit 0;
        ;;
    esac
  done

shift $(($OPTIND - 1))

param1=$1
param2=$2

# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$SUBJECT.lock
if [ -f "$LOCK_FILE" ]; then
   echo "Script is already running"
   exit
fi

trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE

# --- Body --------------------------------------------------------
#  SCRIPT LOGIC GOES HERE
echo $param1
echo $param2
# -----------------------------------------------------------------

壳牌标志(shFlags)允许简化的命令行参数处理了很多,所以一些时间一刻,我决定不忽略这样的可能性。

Shell Flags (shFlags) allows to simplify command-line arguments handling a lot, so at some moment of time I decided not to ignore such possibility.

参数处理:shflags (最新版本: <一个href=\"https://github.com/RenatGilmanov/shell-script-template\">shell-script-template@github)

Arguments handling: shflags (latest version: shell-script-template@github)

#!/bin/bash
# ------------------------------------------------------------------
# [Author] Title
#          Description
#
#          This script uses shFlags -- Advanced command-line flag
#          library for Unix shell scripts.
#          http://code.google.com/p/shflags/
#
# Dependency:
#     http://shflags.googlecode.com/svn/trunk/source/1.0/src/shflags
# ------------------------------------------------------------------
VERSION=0.1.0
SUBJECT=some-unique-id
USAGE="Usage: command -hv args"

# --- Option processing --------------------------------------------
if [ $# == 0 ] ; then
    echo $USAGE
    exit 1;
fi

. ./shflags

DEFINE_string 'aparam' 'adefault' 'First parameter'
DEFINE_string 'bparam' 'bdefault' 'Second parameter'

# parse command line
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

shift $(($OPTIND - 1))

param1=$1
param2=$2

# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/${SUBJECT}.lock

if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi

trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE

# -- Body ---------------------------------------------------------
#  SCRIPT LOGIC GOES HERE
echo "Param A: $FLAGS_aparam"
echo "Param B: $FLAGS_bparam"
echo $param1
echo $param2
# -----------------------------------------------------------------

我认为这些模板可以提高到更简化开发人员的生活。

I do think these templates can be improved to simplify developer's life even more.

所以现在的问题是如何提高他们具备以下条件:


  • 内置记录

  • 更好的错误处理

  • 更好的便携

  • 更小的空间

  • 内置的执行时间跟踪

  • built-in logging
  • better error handling
  • better portability
  • smaller footprint
  • built-in execution time tracking

推荐答案

我会避开依赖于庆典作为外壳和你对<顶级解决方案建模HREF =htt​​p://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html>通过POSIX ,并使用<定义shell语法code> / bin / sh的的家当。我们有一些惊喜最近当 Ubuntu的改变 / bin / sh的破折号

I would steer clear of relying on bash as the shell and model your solution on top of shell syntax defined by POSIX and use /bin/sh on the shebang. We had a number of surprises recently when Ubuntu changed /bin/sh to dash.

在shell世界的另一大流行的退出状态codeS的普遍误解。有一个可以理解的code退出就是让其他的shell脚本编程方式应对特定的故障。不幸的是,不是很多关于这个指导超越的sysexits.h头文件

Another pandemic in the shell world is a general misunderstanding of exit status codes. Exiting with an understandable code is what lets other shell scripts programmatically react to specific failures. Unfortunately, there is not a lot of guidance on this beyond the "sysexits.h" header file.

如果您正在寻找有关良好的shell脚本做法的详细信息,专注于Korn Shell脚本资源。 KSH编程往往把重点放在真正的编程,而不是随意编写的脚本。

If you are looking for more information about good shell scripting practices, concentrate on Korn shell scripting resources. Ksh programming tends to focus on really programming as opposed to writing haphazard scripts.

就个人而言,我还没有发现壳模板多大用处。不幸的事实是,大多数的工程师会简单地复制并粘贴您的模板,并继续写同样马虎壳code。更好的方法是创建shell函数具有良好定义的语义库,然后说服别人使用它们。这种方法也可以帮助变更控制。例如,如果在模板中发现的缺陷,那么每一个基于其脚本是破碎,将需要修改。使用库,能够修复缺陷在一个地方。

Personally, I haven't found much use for shell templates. The unfortunate truth is that most engineers will simply copy and paste your template and continue to write the same sloppy shell code. A better approach is to create a library of shell functions with well-defined semantics and then convince others to use them. This approach will also help with change control. For example, if you find a defect in a template, then every script that was based on it is broken and would require modifications. Using a library makes it possible to fix defects in one place.

欢迎到shell脚本的世界。编写shell脚本是一个有点失去了艺术,这似乎是进入了一个复兴。有在90年代后期写关于这个问题的一些好书 - UNIX Shell编程的伯恩斯和亚瑟想到,虽然这本书在亚马逊的评论使它看起来可怕。恕我直言,有效的壳code拥抱UNIX哲学中 Unix编程艺术<通过埃里克雷蒙描述/ em>的

Welcome to the world of shell scripting. Writing shell scripts is a bit of a lost art that seems to be entering a renaissance. There were some good books written on the subject in the late 90's - UNIX Shell Programming by Burns and Arthur comes to mind though the Amazon reviews for the book make it seem awful. IMHO, effective shell code embraces the UNIX philosophy as described by Eric S. Raymond in The Art of Unix Programming.

这篇关于shell脚本通用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆