Improving repetitive tasks in function

Tags
bash, function, shell, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Improving repetitive tasks in function

Code:
stop_service ()
{
 sudo systemctl is-active --quiet video.service && sudo systemctl stop video.service && sudo rm /etc/systemd/system/video.service && echo stop video
 sudo systemctl is-active --quiet audio.service && sudo systemctl stop audio.service && sudo rm /etc/systemd/system/audio.service && echo stop audio
 sudo systemctl is-active --quiet stream.service && sudo systemctl stop stream.service && sudo rm /etc/systemd/system/stream.service && echo stop stream
 sudo systemctl is-active --quiet images.service && sudo systemctl stop images.service && sudo rm /etc/systemd/system/images.service && echo stop images
 sudo systemctl is-active --quiet browser.service && sudo systemctl stop browser.service && sudo rm /etc/systemd/system/browser.service && echo stop browser
 sudo systemctl is-active --quiet youtube.service && sudo systemctl stop youtube.service && sudo rm /etc/systemd/system/youtube.service && echo stop youtube

}

I am using this function to check if any one of these 6 services is running. I stop it, remove it and echo which service was stopped. Looking at this function though, I feel like there's a better way of doing this instead of repeating the same line 6 times. But my shell knowledge is limited so hopefully someone more experienced than me can assist.

Thanks
# 2  
Old 1 Week Ago
I don't know that this "improves" anything, but maybe it makes it more obvious that the same operations are performed for each service:
Code:
stop_service ()
{	for service in video audio stream images browser youtube
	do	sudo systemctl is-active --quiet "$service".service &&
		    sudo systemctl stop "$service".service &&
		    sudo rm /etc/systemd/system/"$service".service &&
		    echo "stop $service"
	done
}

This User Gave Thanks to Don Cragun For This Post:
rbatte1 (1 Week Ago)
# 3  
Old 1 Week Ago
thanks for your reply, I'll give it a try.
# 4  
Old 1 Week Ago
A slight modification to Don Cragun's fine proposal would make the function more versatile / helpful, stopping one to many services:


Code:
stop_service () { for service
                   do   sudo systemctl is-active --quiet "$service".service &&
                        sudo systemctl stop "$service".service &&
                        sudo rm /etc/systemd/system/"$service".service &&
                        echo "stop $service"
                   done
                }
stop_service  video audio stream images browser youtube
stop_service  video

These 2 Users Gave Thanks to RudiC For This Post:
rbatte1 (1 Week Ago), vgersh99 (1 Week Ago)
# 5  
Old 1 Week Ago
thanks for both suggestions. works great!
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count Repetitive Number in a column and renumbering using awk himanshu Shell Programming and Scripting 7 10-04-2018 12:38 PM
How to remove repetitive lines in a file with sed? baris35 Shell Programming and Scripting 6 08-26-2018 04:47 PM
Avoid running unnecessary repetitive ps command SkySmart Shell Programming and Scripting 6 01-12-2018 05:01 PM
awk regexp to print repetitive pattern yifangt Shell Programming and Scripting 10 07-18-2016 01:13 PM
I need help to removing repetitive lines morphin Shell Programming and Scripting 4 03-17-2016 06:51 PM
Repetitive ending of script jefferj54 Shell Programming and Scripting 1 08-28-2013 04:19 AM
Repetitive scripts within a bash shell Xterra UNIX for Dummies Questions & Answers 2 09-05-2011 12:10 AM
Print lines between two repetitive patterns eranmoh Shell Programming and Scripting 2 02-01-2011 04:29 AM
Improving this validate function pyscho Shell Programming and Scripting 4 10-28-2010 05:10 PM
Unix Remove repetitive alphabets msalam65 Shell Programming and Scripting 26 03-16-2010 11:54 PM
Pull specific data from repetitive text? TECK Shell Programming and Scripting 6 02-19-2010 05:58 PM
Perl Repetitive Pattern Matching Niroj Shell Programming and Scripting 2 08-05-2009 10:17 AM
Count if numbers are not repetitive Raynon Shell Programming and Scripting 7 04-26-2007 12:14 AM
Repetitive Tasks: using if..then inside a loop garak Shell Programming and Scripting 2 03-13-2006 07:36 AM
Repetitive Tasks JairGuerra UNIX for Dummies Questions & Answers 3 11-18-2005 12:36 PM
All times are GMT -4. The time now is 11:11 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.