Calling individual functions from cli

Forum rules
Before you post please read how to get help

Calling individual functions from cli

Postby tadaensylvermane on 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 )?
Level 2
Level 2
Posts: 76
Joined: Tue Jun 25, 2013 8:50 pm

Linux Mint is funded by ads and donations.

Re: Calling individual functions from cli

Postby xenopeek on 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
User avatar
Level 23
Level 23
Posts: 18180
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest