LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script need help with writing (https://www.linuxquestions.org/questions/programming-9/bash-script-need-help-with-writing-4175610985/)

Wi1done 07-31-2017 02:04 PM

Bash script need help with writing
 
This is my first post and first script. Just finished my first Linux class and I'm trying to put it all into practice. Accepting all criticism and help, TIA!

I am writing a script that would

1) Open my web browser

2) Search a random word using bing search, selected randomly from the dictionary

3) Wait 5 - 10 seconds then search another random word, repeating roughly 20 - 30 times

4) Close the web browser

5) Repeat once a day

So far this is the code I have
Code:

#! /bin/bash


a=( $(shuf -n1 /usr/share/dict/american-english) )
for i in ${a}

do
xdg-open https://www.bing.com/search?q=$i


done
exit 0

So I cannot make it repeat a specific number of times, of course I can just put the same code multiple times but Im trying to do it right.

Also when the web browser is already open it gives the error
Code:

[7259:7294:0731/114659.197592:ERROR:browser_gpu_channel_host_factory.cc(103)] Failed to launch GPU process.
Created new window in existing browser session.

Which is fine because it just opens another browser but I wonder if I can make it do the search in the same tab/page.

Lastly, I cannot figure out how to make it close the browser when it has finished its task. The uuid is different each time it loads.

Thanks for any help!

Jjanel 07-31-2017 05:40 PM

Try changing: for i in ${a} to: for i in ${a[*]}
(a simpler test could use: echo $i vs xdg-open; maybe seq vs shuf)

Do you have to use xdg-open? Or could you use wget?
'run daily' makes me think of cron (but who will SEE browser?)
pkill <browser_name> maybe
I don't see a way to make xdg-open re-use SAME browser-window.
(oh, is this like: mid-day pop-up flash-cards, on your screen?)

TMI:jawa: hints: let n++; if [ $n -gt 23 ] ; then break ; fi ;sleep 9

Welcome to LQ! GREAT A+ job on 1st post! Best wishes! Let us know...
(post test-case with just echo vs xdg, and just 2-3 iterations!)

Hopefully, some other LQ'er will address the xdg/gui issues:eek:
(while you're tackling crontab with my echo_a_few test-version;))

p.s. You didn't web-search that error [?] :tisk: IF proven guilty, A+ -> A :D

Beryllos 07-31-2017 07:30 PM

Quote:

Originally Posted by Wi1done (Post 5742249)
Lastly, I cannot figure out how to make it close the browser when it has finished its task. The uuid is different each time it loads.

Run the command in background (for example, with &). Then $! reports its pid. You can assign that pid to a variable and later use it to kill the process.

For example:
Code:

#!/bin/bash
sleep 3600 &  # sleep 1 hour in background
pid=$!        # get pid of most recent background process
kill $pid      # kill process


Wi1done 08-03-2017 11:49 AM

Im writing a script that will deploy automatically and do searches due to the fact that my browser gives me points per search. Id like to utilize the max daily amount of points.

For the life of me I still cannot make it loop a specific number of times. ive put together loops on their own and tried to implement the script into it and i continually get errors. Im gonna mess with a few more days and ill let everyone know how it turns out.

Appreciate all the help. Sorry about accidentally posting twice, Im a noob

michaelk 08-03-2017 12:12 PM

Code:

a=( $(shuf -n 1 /usr/share/dict/american-english) )
for i in ${a[*]}
do
  xdg-open https://www.bing.com/search?q=$i
done

-n 1 will only output one line. Change 1 to the number of words to be searched.

Wi1done 08-03-2017 12:31 PM

I cant believe I missed that! Thanks Michaelk, much appreciated!

grail 08-03-2017 09:21 PM

I have not played with browsers much so will leave that to someone else, but I would change your process to feed a while loop:
Code:

while read -r word
do
  <your browser stuff here>

  sleep 5
done< <(shuf -n 30 /usr/share/dict/american-english)

Please note there is a space between < and <(...).

The above script can then called in cron to launch once a day.

Wi1done 08-05-2017 09:17 PM

So I was able to get the script to do exactly what I want, thanks to all of your help. I appreciate it.

Now the last step is to set the script to a crontab and let it do its business at the desired time. cron cannot open a web browser, ive tried wget as well as xdg-open.

With some google searching I have found that some people suggest that you put the command directly into the cron tab to open the web browser. I have done that as well with no luck.

The only thing I can think of now is to find another way to schedule a job once a day... any suggestions. TIA

NevemTeve 08-05-2017 11:45 PM

When using cron/at, forget GUI and GUI based browser, use wget or curl.

michaelk 08-06-2017 05:27 AM

Try this
Code:

xx yy * * * export DISPlAY=:0.0 && /path/to/my_script
Replace xx yy with actual values.

Wi1done 08-06-2017 04:23 PM

That did it.

Code:

export DISPLAY=:0 &&
My code is complete. Thank you guys for all the help

Sefyir 08-06-2017 04:27 PM

Quote:

With some google searching I have found that some people suggest that you put the command directly into the cron tab to open the web browser. I have done that as well with no luck.
Cron can be difficult to work with. It has a unusual syntax. I like to use this post to help me figure it out
https://stackoverflow.com/questions/...19183#18919183.

However using cron to open a web-browser is not recommended


All times are GMT -5. The time now is 06:14 PM.