Calling individual functions from cli

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
tadaensylvermane
Level 2
Level 2
Posts: 88
Joined: Tue Jun 25, 2013 8:50 pm

Calling individual functions from cli

Post by tadaensylvermane »

*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 )?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Calling individual functions from cli

Post by xenopeek »

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

#!/bin/bash

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;;
esac

exit 0
Image
Locked

Return to “Scripts & Bash”