开发者

Functions vs Scripts - Unix Bash Programming

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.

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.

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

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.

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.

Edit: 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜