LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2017, 02:04 PM   #1
Wi1done
LQ Newbie
 
Registered: Jul 2017
Posts: 6

Rep: Reputation: Disabled
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!
 
Old 07-31-2017, 05:40 PM   #2
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 996
Blog Entries: 11

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
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 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
(while you're tackling crontab with my echo_a_few test-version)

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

Last edited by Jjanel; 07-31-2017 at 06:59 PM.
 
Old 07-31-2017, 07:30 PM   #3
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 354

Rep: Reputation: 152Reputation: 152
Quote:
Originally Posted by Wi1done View Post
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
 
1 members found this post helpful.
Old 08-03-2017, 11:49 AM   #4
Wi1done
LQ Newbie
 
Registered: Jul 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-03-2017, 12:12 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,017

Rep: Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850
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.
 
1 members found this post helpful.
Old 08-03-2017, 12:31 PM   #6
Wi1done
LQ Newbie
 
Registered: Jul 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
I cant believe I missed that! Thanks Michaelk, much appreciated!
 
Old 08-03-2017, 09:21 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
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.
 
Old 08-05-2017, 09:17 PM   #8
Wi1done
LQ Newbie
 
Registered: Jul 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-05-2017, 11:45 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,236

Rep: Reputation: 979Reputation: 979Reputation: 979Reputation: 979Reputation: 979Reputation: 979Reputation: 979Reputation: 979
When using cron/at, forget GUI and GUI based browser, use wget or curl.
 
Old 08-06-2017, 05:27 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,017

Rep: Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850Reputation: 1850
Try this
Code:
xx yy * * * export DISPlAY=:0.0 && /path/to/my_script
Replace xx yy with actual values.
 
Old 08-06-2017, 04:23 PM   #11
Wi1done
LQ Newbie
 
Registered: Jul 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
That did it.

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

Last edited by Wi1done; 08-06-2017 at 04:45 PM. Reason: adding [code]
 
Old 08-06-2017, 04:27 PM   #12
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 518

Rep: Reputation: 237Reputation: 237Reputation: 237
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
 
  


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
Need help writing a bash script stympman Linux - Newbie 2 10-18-2008 07:55 PM
looking for help in writing a bash script onepostonly Linux - General 3 10-05-2008 06:42 PM
Help Me>> Need help in writing Bash script lamak_98 Programming 6 10-04-2007 10:44 AM
Writing a bash script. lebabyg Linux - General 2 03-31-2007 11:39 AM
writing bash script ankitgdit Programming 4 08-19-2003 06:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:10 AM.

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