Bash: Need help with grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: Need help with grep
# 1  
Old 09-13-2017
Bash: Need help with grep

Hi, I want to write a bash that can turn an application on if it is off or on if it is off. I found a script for LibreELEC, but my OS is OSMC (a Linux distribution, too). I already modiefied lines 5 and 7:
Code:
#!/bin/sh
SERVICE='hyperiond'
if ps | grep -v grep | grep $SERVICE > /dev/null
then
sudo systemctl stop hyperion.service 2>/dev/null; sudo /etc/init.d/hyperion stop 2>/dev/null ; sudo /sbin/initctl stop hyperion 2>/dev/null
else
sudo systemctl start hyperion.service 2>/dev/null ; sudo /etc/init.d/hyperion start 2>/dev/null ; sudo /sbin/initctl start hyperion 2>/dev/null
fi

Line 5 and line 7 work when I use them in Putty, so I think line 1 to 3 are the problem. If I run the script it only turnes the application on, but not off. Can you tell me what lines 1 to 3 do so I can fix them? I found a phython script that should work and do the same, just for reference (I want to use bash, as I want to expand the script once it's running).
Code:
 import subprocess
pid = subprocess.Popen('pidof hyperiond', shell=True, close_fds=True, stdout=subprocess.PIPE)
try:     
if pid.stdout.readlines():
subprocess.Popen('killall hyperiond', shell=True)
else:
subprocess.Popen('/opt/hyperion/bin/hyperiond /etc/hyperion.config.json </dev/null >/dev/null 2>&1 &', shell=True)
except Exception, e:
pass

Thanks Smilie

Last edited by Chuchu211; 09-13-2017 at 08:22 AM..
# 2  
Old 09-13-2017
Instead of
Code:
if ps | grep -v grep | grep $SERVICE > /dev/null

try
Code:
if pgrep "$SERVICE" > /dev/null

Stupid question, but why

Code:
sudo systemctl stop hyperion.service 2>/dev/null; sudo /etc/init.d/hyperion stop 2>/dev/null ; sudo /sbin/initctl stop hyperion 2>/dev/null

Each of these commands does the same thing. Wouldn't this be better?
Code:
if pgrep "$SERVICE" > /dev/null
then
   if [ -x /bin/systemctl ] 
   then sudo systemctl stop hyperion.service 2>/dev/null
   elif [ -x /sbin/initctl ] 
   then sudo /sbin/initctl stop hyperion 2>/dev/null
   else sudo /etc/init.d/hyperion stop 2>/dev/null
   fi
else
   if [ -x /bin/systemctl ] 
   then sudo systemctl start hyperion.service 2>/dev/null
   elif [ -x /sbin/initctl ] 
   then sudo /sbin/initctl start hyperion 2>/dev/null
   else sudo /etc/init.d/hyperion start 2>/dev/null
   fi
fi

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 09-13-2017
Thanks a lot Andrew, it works perfectly fine Smilie My batch knowledge is very limited, I only watched 3 hours of tutorials on Youtube, so I did not know it does the same. I got the commands from the HyperCon.jar, there you can connect to the Raspberry Pi and start/stop the Ambilight. I just grabbed the command that is used in their program.
# 4  
Old 09-13-2017
The service command is quite omnipresent in Linux (e.g. emulated by systemctl)
Code:
PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
SERVICE='hyperiond'
if service "$SERVICE" status >/dev/null 2>&1
then
  service "$SERVICE" stop
else
  service "$SERVICE" start
fi
sleep 1 # wait a sec, in case the start/stop runs in the background

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep trouble in Bash

PH=(6H 0 0 JD 9S 0 KD 0) #input from .txt file In the above array, I am trying to get the computer to tell me at what indices the non-zeros are at. I don't understand why this doesn't work... grep -v -b "0" ph.txt > position.txt Isn't grep -v supposed to show non matches in Bash?... (2 Replies)
Discussion started by: cogiz
2 Replies

2. Shell Programming and Scripting

How to use grep in a loop using a bash script?

Dear all, Please help with the following. I have a file, let's call it data.txt, that has 3 columns and approx 700,000 lines, and looks like this: rs1234 A C rs1236 T G rs2345 G T Please use code tags as required by forum rules! I have a second file, called reference.txt,... (1 Reply)
Discussion started by: aberg
1 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Grep in bash script

Hi Experts, I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script. Error message: "grep: sales.txt: No such file or directory" #!/bin/bash var=1 var1=`awk '{n++} END {print n}' sales.txt` while ] do prod=$var... (6 Replies)
Discussion started by: parpaa
6 Replies

4. Shell Programming and Scripting

Working with grep and Bash

Hi, I am currently working on a Bash shell script that - Downloads a webpage, in this case youtube.com - Extracts Number of views, Extracts Title of video, Extracts User who made it, and lastly Duration. Then I have to Out put this into columns. To me this sounds like crazyness. I'm very new... (6 Replies)
Discussion started by: Njzangel
6 Replies

5. Shell Programming and Scripting

bash: using a string variable in grep

Hi, I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg... grep 'pattern of several words' filename I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable: variable=$@ I... (2 Replies)
Discussion started by: adrian777uk
2 Replies

6. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

7. Shell Programming and Scripting

Problem using grep in bash script

When it comes to programing and UNIX, I know just enough to be really really dangerous. I have written a python script to parse through a file that contains ~1 million lines. Depending on whether a certain string is matched, the line is copied into a particular file. For the sake of brevity,... (4 Replies)
Discussion started by: errcricket
4 Replies

8. Shell Programming and Scripting

Bash script - Grep

Hi, I am very new to bash scripting and I need to write a bash script that takes two arguments, a string and a file. The output should be each line which matches the string *from the beginning of the line*. For example, given a string "ANA" the line starting with "ANABEL" will be printed, but... (9 Replies)
Discussion started by: jboy
9 Replies

9. Shell Programming and Scripting

Bash script (using find and grep)

I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n). #!/bin/bash SEARCHQUERY="searchword1 searchword2 searchword3"; for WORD in $SEARCHQUERY do ... (6 Replies)
Discussion started by: limmer
6 Replies

10. Shell Programming and Scripting

A little help using grep for anagram solving with BASH

Hi guys, I have been making a simple script for looking for anagram solutions in a word list (a file of 22k or so words). At the moment it funtions like so: User enters an 8 character string (whatever letters you want to find anagrams of, or solve rather) The script moves all the words... (2 Replies)
Discussion started by: Donthommo
2 Replies
Login or Register to Ask a Question