Improving repetitive tasks in function


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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  
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:
# 4  
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:
Login or Register to Reply

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

More UNIX and Linux Forum Topics You Might Find Helpful
Repetitive ending of script
jefferj54
Hi, I am quit satisfied with this scrtipt and really don't want to change anything but the end. I get a repetitve option when I want to "quit" and don't know why. What am I missing? When I press q to quit with this script, I get an EXTRA "Enter " How do I correct this so that when I press...... Shell Programming and Scripting
1
Shell Programming and Scripting
Improving this validate function
pyscho
Hi guys, I use this function which was provided to me by someone at this site. It works perfectly for validating a users input option against allowed options.. example: validateInput "1" "1 3 4 5" would return 0 (success) function validateInput { input=$1 allowedInput=$2 for...... Shell Programming and Scripting
4
Shell Programming and Scripting
Count if numbers are not repetitive
Raynon
Hi All, I have an input below and i would want to do a count on all the term "aaa" and count only once if the number in first column is the same. For eg, if i use a "grep -c aaa input" command, the count will be "8". However, i would want the count to be "6" instead since 2 numbers in the 1st...... Shell Programming and Scripting
7
Shell Programming and Scripting
Repetitive Tasks: using if..then inside a loop
garak
I thought I was getting pretty good with Bash but this problem is stumping me. Any suggestions would be greatly appreciated. I've written a firewall script that uses a lot of functions. The variables are read into the script from another .conf file earlier on in the code. Most of these...... Shell Programming and Scripting
2
Shell Programming and Scripting
Repetitive Tasks
JairGuerra
Could someone tell me how I can simplify the script that follows!!! I know that there must be a way how to grep Average from sar01.................. sar02 ....................... sar03....................... sar04...... UNIX for Dummies Questions & Answers
3
UNIX for Dummies Questions & Answers