LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2019, 06:25 PM   #16
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,137

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392

Never noticed so many replies. Here is what I ended up with including sample. For the fun of it I tried making a single master script for all my machines in one. So much for the "do one thing and do it well" philosphy. 775 lines. Relevant part below. Everything is wrapped in a function with case statements inside as needed.

Code:
SCRIPTCALL=$(basename "$0")
SCRIPTEXELINKS=/usr/local/bin

bin_link_create() { # creates symlinks necessary for script to function
	if [[ ! -L "$SCRIPTEXELINKS"/"$1" ]] ; then
		if [[ ! -d "$SCRIPTEXELINKS" ]] ; then
			mkdir -p "$SCRIPTEXELINKS"
		fi
		if dir | grep $(basename "$0") > /dev/null ; then
			ln -s $(pwd)/$(basename "$0") "$SCRIPTEXELINKS"/"$1"
		else
			ln -s "$0" "$SCRIPTEXELINKS"/"$1"
		fi
	fi
}

case "$SCRIPTCALL" in
	# minecraft servers
	stop|restart|backup|start|runtest) #MAINCOMMAND#
		minecraft_controller "$SCRIPTCALL" "$@"
		;;
	kodistop|kodinewusr|kodiplaylists|kodimedia|kodistart) #MAINCOMMAND#
		kodi_controller "$SCRIPTCALL"
		;;
	poweroff|reboot|pxewake) #MAINCOMMAND#
		power_manager "$SCRIPTCALL" "$@"
		;;
	mysqlbackup) #MAINCOMMAND#
		mysql_backup "$1" "$2"
		;;
	repowatch) #MAINCOMMAND#
		repo_watcher
		;;
	dnscheck|servicecheck|status) #MAINCOMMAND#
		services_manager "$SCRIPTCALL" "$@"
		;;
	upgrade|full-upgrade) #MAINCOMMAND#
		package_upgrades "$SCRIPTCALL" "$1"
		;;
	mergerfspool|smartstatus) #MAINCOMMAND#
		storage_manager "$SCRIPTCALL" "$@"
		;;
	mcramdisk|userload) #MAINCOMMAND#
		minecraft_ramdisk "$SCRIPTCALL"
		;;
	touchpad) #MAINCOMMAND#
		touchpad_toggle
		;;
	mute|raise|lower) #MAINCOMMAND#
		volume_control "$SCRIPTCALL"
		;;
	servercheck) #MAINCOMMAND#
		server__check
		;;
	dvdrip) #MAINCOMMAND#
		dvd_ripper "$1"
		;;
	battery) #MAINCOMMAND#
		battery_warning
		;;
	*)
		if [[ "$USER" == root ]] ; then
			for line in $(grep "#MAINCOMMAND#" "$0" | grep -v command | sed 's/#MAINCOMMAND#//g') ; do
				if echo "$line" | grep "|" > /dev/null ; then
					for command in $(echo "$line" | awk -F\| '{OFS="\n"; print $1, $2, $3, $4, $5, $6, $7, $8}' | cut -d\) -f 1) ; do
						bin_link_create "$command"
					done
				else
					for command in $(echo "$line" | cut -d\) -f 1) ; do
						bin_link_create "$command"
					done
				fi
			done
		fi
		;;
esac
Very hacky. Not sure who Rube Goldberg is but I get an idea from the statement. It works though.

Last edited by jmgibson1981; 02-15-2019 at 06:27 PM.
 
Old 02-16-2019, 01:26 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Whilst it makes more sense now, I am not a fan of self processing scripts and see no reason why a simple array is added to each time you create a new entry in the case statement,
but of course, each to their own
 
Old 02-16-2019, 01:24 PM   #18
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,137

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Seemed a nice feature to have with the design of the script. Making the symlinks for each option could be a long process. That plus in theory this will only need to run when first time or when you add to it. Not a regularly running part. Is why I didn't think much of it looking like this. I agree though, not the most optimal thing. But it does work that much I know. I couldn't think of any other way to accomplish.

Last edited by jmgibson1981; 02-16-2019 at 05:45 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
A better way to make a bash script run on boot helptonewbie Linux - General 6 09-13-2006 06:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration