功能VS脚本 - 的Unix bash编程 [英] Functions vs Scripts - Unix Bash Programming

查看:101
本文介绍了功能VS脚本 - 的Unix bash编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的编程(约一年 - 用java),非常新来砸(大约六周)。我写了一个小程序,做了几件事情,这完全consits脚本。没有一个功能。我知道我可以有一个文件,所有功能,但为什么和/或当我应该。

I am fairly new to programming (about one year - with java) and extremely new to bash (about six weeks). I wrote a small application that does a few things, and it consits of entirely scripts. Not one function. I know I can have one file with all functions but why and/or when should I.

例如我甲肝叫的getID脚本调用validateID脚本,以确保该ID是有效的(正好3个数字长的前827 395 148),然后检查为特定的ID,并返回一个IDS的文件如果ID进行验证,发现真实。

For example I hav a script called getID that calls a "validateID" script to make sure the id is valid (exactly 3 numbers long ex. 827 395 148), then checks an "ids" file for the specific id and returns true if the id is validated and found.

然后,我有叫getID2的验证ID,如果找不到该ID返回true另一个脚本。

Then I have another script called getID2 that validate the id and returns true if the id is not found.

我最近发现了getopt的命令,我可以说的getID -f返回true,如果发现的getID -n如果没有发现返回true。或者,我可以写一个函数的getID()如果发现1如果没有找到,返回0。

I recently found the getopt command where I can say getID -f to return true if found and getID -n to return true if not found. Or I can write a function getID() that returns 0 if found and 1 if not found.

目前我正在试图超过更好的逻辑编写应用程序。我会接受该计划的任何部分的意见,但我真的想知道我什么时候应该使用功能和/或何时应使用脚本。该方案的细节如下。先谢谢了!

I am currently trying to write the application over with better logic. I'll accept any advice for any part of the program, but I would really like to know when should I use functions and/or when should I use scripts. Specifics for the program are below. Thanks in advanced!

具体细节 -
该应用程序是一个小型的报告制度在那里我有身份证,姓名和分数的列表。我可以根据自己的身份证查看,编辑,更新或删除的人。

Specifics - The application is a small reporting system where i have a list of ids, names and scores. I can view, edit, update, or delete someone based on their id.

推荐答案

作为一般规则,你应该只创建的东西一个全新的脚本时有显著独立的实用。如果其存在的唯一原因是为了通过主脚本调用,它应该在主脚本的功能。

As a general rule, you should only create a whole new script for something when it has significant standalone utility. If the only reason for its existence is to be called by the main script, it should be a function in the main script.

编辑:如果一个特定的功能(或功能集合)将被从多个独立的脚本调用,您可以把该功能作为一个函数到一个单独的文件和源它在它的使用各种脚本

If a particular function (or set of functions) is going to be called from multiple standalone scripts, you can put that function as a function into a separate file and source it in the various scripts where it's used.

这篇关于功能VS脚本 - 的Unix bash编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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