Calling individual functions from cli

Forum rules
Before you post please read how to get help
Level 2
Level 2
Posts: 79
Joined: Tue Jun 25, 2013 8:50 pm

Calling individual functions from cli

Postby tadaensylvermane » Sat Apr 26, 2014 3:00 pm

*EDIT* Edited subject as well as post to reflect more accurately what I am looking to do.

Title. I'd like to build a single script for all my backups but I need 2 separate functions for it. Backing my data partition to the usb hdds is 1, other is to keep a current backup of my /var/cache/apt/archives. As of right now I have them as 2 separate scripts. How can I call a specific function from the command line ( for cron entries )?

User avatar
Level 24
Level 24
Posts: 21280
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Calling individual functions from cli

Postby xenopeek » Sun Apr 27, 2014 3:13 pm

You'll need to pass a parameter to your script, based on which your script does either the one backup or the other.

Here is a skeleton example. You'd call this script as "scriptname backup1" or "scriptname backup2", and depending on the parameter passed (backup1 or backup2) it would then run the backup1 or backup2 function. Obviously this needs more polish, but I think this will get you started.

Code: Select all


print_usage_and_exit() {
   echo "Usage: $(basename $0) [backup1|backup2]"
   exit 1

backup1_function() {
   # put your code here
   echo "Running: backup1"

backup2_function() {
   # put your code here
   echo "Running: backup2"

[[ -z "$1" ]] && print_usage_and_exit

case "$1" in
   "backup1") backup1_function;;
   "backup2") backup2_function;;
   *) print_usage_and_exit;;

exit 0

Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest