Improving repetitive tasks in function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Improving repetitive tasks in function
# 1  
Old 12-05-2018
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 12-05-2018
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:
# 3  
Old 12-05-2018
thanks for your reply, I'll give it a try.
# 4  
Old 12-05-2018
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:
# 5  
Old 12-05-2018
thanks for both suggestions. works great!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I need help to removing repetitive lines

Hello SuperUsers! First i wanna say my english sucks.. Don't hate me for that. :rolleyes: I need to make a Bash for removing smilar lines from an output file. My output file always same. Line 1 & 2 Stays. And others similar to this lines needs to be delete. </UsageData><?xml version="1.0"... (4 Replies)
Discussion started by: morphin
4 Replies

2. Shell Programming and Scripting

Repetitive ending of script

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... (1 Reply)
Discussion started by: jefferj54
1 Replies

3. UNIX for Dummies Questions & Answers

Repetitive scripts within a bash shell

I have a bash shell that even though it does not look pretty is working very well. Some of the steps are repetitive, something like this: muscle -in ${e}.4 > $e.5 read -t1 sed ':a /^>/!N;s/\n\(\)/\1/;ta' $e.5 > $e.6 read -t2 awk '/>/{fr=$3;getline;n=split ($0,a,""); for (i=1;i<=n;i++)... (2 Replies)
Discussion started by: Xterra
2 Replies

4. Shell Programming and Scripting

Print lines between two repetitive patterns

Hi users I have one file which has number of occurrence of one pattern examples Adjustmenttype,11 xyz 10 dwe 9 abd 13 def 14 Adjustmenttype,11 xyz 24 dwe 34 abd 35 def 11 nmb 12 Adjustmenttype, not eleven .... ... ... (2 Replies)
Discussion started by: eranmoh
2 Replies

5. Shell Programming and Scripting

Improving this validate function

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... (4 Replies)
Discussion started by: pyscho
4 Replies

6. Shell Programming and Scripting

Unix Remove repetitive alphabets

Hi, I am trying to write a script that will take 2 or more instances of repetitive alphabets (ZZ) to be removed from a field. This should only happen from beginning and end of a field. For Example : Input File a) ZZZIBM Corporation b) ZZZIBM Corporation ZZZZZ b) IBM ZZZ... (26 Replies)
Discussion started by: msalam65
26 Replies

7. Shell Programming and Scripting

Perl Repetitive Pattern Matching

Problem: GIVEN ======= my $sql="INSERT INTO table_nm(a, b, b, d, e, f , g) VALUES (2046, TODAY, 'Change Subscription Name', '00000000000002000000000000000000000000000000000000', '00000000000001000000000000000000000000000000000000', '00000000000000000000000000000000000000000000000000', 1);... (2 Replies)
Discussion started by: Niroj
2 Replies

8. Shell Programming and Scripting

Count if numbers are not repetitive

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... (7 Replies)
Discussion started by: Raynon
7 Replies

9. Shell Programming and Scripting

Repetitive Tasks: using if..then inside a loop

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... (2 Replies)
Discussion started by: garak
2 Replies

10. UNIX for Dummies Questions & Answers

Repetitive Tasks

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... (3 Replies)
Discussion started by: JairGuerra
3 Replies
Login or Register to Ask a Question