LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2012, 03:53 PM   #1
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Rep: Reputation: Disabled
Question scipt help elif


hi can someone please tell me how to intgegrate

current_date=`date +%s`

for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`

check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
recording=1
fi
done

as an additional parameter to no suspend in to:

#!/bin/bash
sleep 30
if [ `netstat -t | grep -c ":9982"` -ge 3 ]
then
exit 1
elif [ `netstat -t | grep -c "192.168.0.:microsoft-ds ESTABLISHED"` -ne 0 ]
then
exit 1
else
echo pm-suspend
fi

Thank you

Last edited by bilbonix; 11-02-2012 at 03:59 PM.
 
Old 11-03-2012, 09:23 AM   #2
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
I tried

Code:
#!/bin/bash
sleep 30


current_date=`date +%s`

for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`

# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
 echo recording in progress
fi
done
if [ `netstat -t | grep -c ":9982"` -ge 3 ]
then
      echo live tv
elif [ `netstat -t | grep -c "192.168.0.:microsoft-ds ESTABLISHED"` -ne 0 ]
then
      echo media playing
else
      echo pm-suspend
fi
But get error

Code:
/home/billy/bin/grep.sh: line 12: $i: syntax error: operand expected (error toke                    n is "$i")
 
Old 11-03-2012, 09:27 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Do you want to combine both parts of the sript? Please mention what is your ultimate goal for which you're writing this script? I don't understand the use of definning `date +%s` as curent_date, and is it correct... current_date equal to date +%s ?
 
Old 11-03-2012, 09:47 AM   #4
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
It is a suspend script which checks for connections on port 9982 and connections to my windows share before disconnecting.

I am trying to add in a test which checks for an active recording by inserting the relevant section of the other script which does work. I just not sure of the if logic.
 
Old 11-03-2012, 09:53 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Just check this:
Quote:
#!/bin/bash
sleep 30
current_date=$(date +%s)
cmd1=$(netstat -t | grep -c ":9982")
cmd2=$(netstat -t | grep -c "192.168.0.:microsoft-ds ESTABLISHED")

for i in $( ls ); do
tmp_start=$(cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ",")
tmp_stop=$(cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ",")

# check for any current recording
if [ $tmp_stop -gt $current_date -a $tmp_start -lt $current_date ]; then
echo "recording in progress"
fi
done

if [ $cmd1 -ge 3 ];
then
echo "live tv"
elif [ cmd2 -ne 0 ];
then
echo "media playing"
else
echo "pm-suspend"
fi
Once go through "man test" for better understanding of test conditions & declarations/comparision using test.

-- ADDTION ---
Just below the #!/bin/bash line, add a line, set -xv for debugging. It will give you clear idea by showing line by line output of the script. Later you can remove or # this line.
Quote:
#!/bin/bash
set -xv
.....
.......

Last edited by shivaa; 11-03-2012 at 09:57 AM.
 
Old 11-03-2012, 03:35 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

1) Clean, consistent formatting makes code readable and more easily debuggable. Be liberal with whitespace; indent all your sub-commands evenly and separate logical sections with empty lines. Never just line everything up on the left edge. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be so a year or so down the line).

2) $(..) is highly recommended over `..`

3)
Code:
for i in $( ls ); do
Don't Read Lines With For, and don't parse ls for filenames or data.

Just use a simple globbing pattern here:

Code:
for i in * ; do
4)
Code:
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
Useless Use Of Cat, and multiple grep/cut commands when a single awk expression would probably do better.

Since the file contents are unknown to me, I can't be sure of the exact solution, but it would probably look something like this:
Code:
tmp_start=$( awk -F '[ ,]' '/"start"/ { print $3 }' "$i" )
Also, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


5)
Code:
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr

And you don't need $((..)) unless you're actually evaluating an expression, BTW.

Code:
if (( tmp_stop > -gt current_date && tmp_start < current_date ]; then
The above does assume that the variables hold simple integers, however.

Last edited by David the H.; 11-03-2012 at 03:37 PM. Reason: minor word change
 
1 members found this post helpful.
Old 11-03-2012, 03:47 PM   #7
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks for the tips!

I have it working , I had missed out part of the script, here is the working script:

Code:
#!/bin/bash
cd ~billy/.hts/tvheadend/dvr/log
current_date=`date +%s`

for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`

# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
 exit 1
fi
done
if [ `netstat -t | grep -c ":9982"` -ge 3 ]
then
      exit 1
elif [ `netstat -t | grep -c "192.168.0.:microsoft-ds ESTABLISHED"` -ne 0 ]; then
      exit 1
else
      sudo pm-suspend
fi
Here is an eg of the dvr log:

Code:
{
	"channel": "BBC TWO",
	"start": 1351884600,
	"stop": 1351886400,
	"start_extra": 2,
	"stop_extra": 2,
	"config_name": "",
	"creator": "billy",
	"filename": "/home/billy/recordings/Coast.2012-11-02.S06E01.ts",
	"title": {
		"und": "Coast"
	},
	"description": {
		"eng": "Nick Crane visits a project to build a new seaport for London, before travelling across the channel to Belgium, where he takes a ride on a tram that runs along the country's coastline. Alice Roberts learns how to be a seaside landlady in Margate, and Neil Oliver tells the story of British forces' efforts to stop Hitler's biggest battleships reaching the coast of Kent during the Second World War. Back in Belgium, Mark Horton reveals the city of Bruges's role in the history of brick-making, and Miranda Krestovnikoff goes shrimp-fishing on horseback."
	},
	"pri": "normal",
	"noresched": 0,
	"contenttype": 35,
	"broadcast": 580559,
	"container": 4
}
Should/can I still tidy it up as you specified?
 
  


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] Elif Statement help UnixNewbie91 Linux - Newbie 6 04-28-2012 10:35 AM
Python shell help (using elif) danred Linux - General 8 10-20-2009 03:55 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
Need Firewall Scipt PLease? extremebfn Linux - Networking 2 09-05-2004 06:23 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM


All times are GMT -5. The time now is 05:36 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration