LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scipt help elif (https://www.linuxquestions.org/questions/linux-newbie-8/scipt-help-elif-4175435358/)

bilbonix 11-02-2012 03:53 PM

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

bilbonix 11-03-2012 09:23 AM

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")

shivaa 11-03-2012 09:27 AM

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 ?

bilbonix 11-03-2012 09:47 AM

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.

shivaa 11-03-2012 09:53 AM

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
.....
.......

David the H. 11-03-2012 03:35 PM

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.

bilbonix 11-03-2012 03:47 PM

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?


All times are GMT -5. The time now is 03:16 AM.