Author |
Message |
fmen
Joined: 07 May 2018 Posts: 51
|
Posted: Mon 21 May 2018, 10:29 Post subject:
Bash If...then syntax driving me nuts! |
|
Been working on this for hours, googling and googling but no pleasure.
Code: |
#!bin/bin/sh
url1-$(xdotool search --name "yahoo.com")
echo $url1
if [[$url1 == "39845889"]]; then
xdotool windowactivate 39845889
|
In terminal, echo shows the correct number with the following error:
"[[39845889: command not found
I've tried all the combinations possible, except the right one apparently.
Thanks you...
|
Back to top
|
|
 |
fredx181

Joined: 11 Dec 2013 Posts: 3701 Location: holland
|
Posted: Mon 21 May 2018, 11:00 Post subject:
Re: Bash If...then syntax driving me nuts! |
|
fmen wrote: | Been working on this for hours, googling and googling but no pleasure.
Code: |
#!bin/bin/sh
url1-$(xdotool search --name "yahoo.com")
echo $url1
if [[$url1 == "39845889"]]; then
xdotool windowactivate 39845889
|
In terminal, echo shows the correct number with the following error:
"[[39845889: command not found
I've tried all the combinations possible, except the right one apparently.
Thanks you... |
Hi fmen,
There has to be a space between the brackets and what's inside, like this:
Code: | if [[ $url1 == "39845889" ]]; then |
Also it has to end with fi and what's on top seems not right to me: "#!bin/bin/sh"
So maybe try this then:
Code: | #!/bin/bash
url1=$(xdotool search --name "yahoo.com")
echo $url1
if [[ $url1 == "39845889" ]]; then
xdotool windowactivate 39845889
fi |
Fred
_________________ Dog Linux website
|
Back to top
|
|
 |
fmen
Joined: 07 May 2018 Posts: 51
|
Posted: Mon 21 May 2018, 11:12 Post subject:
|
|
Awesome!!
The #!bin and fi stuff were merely typos that occurred in retyping the script in the thread. Thanks a bunch for helping this newbie.
Lesson: gotta pay close attention to spaces.
|
Back to top
|
|
 |
fmen
Joined: 07 May 2018 Posts: 51
|
Posted: Mon 21 May 2018, 15:17 Post subject:
|
|
As luck would have it the results change every time Puppy reboots, so the grand scheme of my plan goed by the wayside.
I am working on a script with the following framework:
Code: |
if yahoo.com is the activewindow then
do this
elif google.com is the activewindow then
do that
etc
fi
|
I thought I had it licked but the inconsistency of --name results changing deep-sixed that.
I'd be grateful for any suggestions.
|
Back to top
|
|
 |
fredx181

Joined: 11 Dec 2013 Posts: 3701 Location: holland
|
Posted: Mon 21 May 2018, 16:06 Post subject:
|
|
fmen wrote: |
Code: |
if yahoo.com is the activewindow then
do this
elif google.com is the activewindow then
do that
etc
fi
|
I thought I had it licked but the inconsistency of --name results changing deep-sixed that.
. |
Something like this ?
Code: | url1=$(xdotool search --name "yahoo.com")
url2=$(xdotool search --name "google.com")
if [ -n "$url1" ]; then
dothis
elif [ -n "$url2" ]; then
dothat
fi |
I'm not sure what you mean with active window. Focused ?
Fred
_________________ Dog Linux website
|
Back to top
|
|
 |
fmen
Joined: 07 May 2018 Posts: 51
|
Posted: Mon 21 May 2018, 18:51 Post subject:
|
|
Thank you.
My goal is to have a bash file that can be triggered by a keyboard shortcut or a mouse gesture and depending on which of the browser's tabs is active, will send specific keys eg name and/or pwd or other action to that particular page.
I tested the following...
Code: |
#!/bin/sh
url1=$(xdotool search --name "yahoo.com")
url2=$(xdotool search --name "google.com")
if [ -n "$url1" ]; then
xdotool type "hello world"
elif [ -n "$url2" ]; then
xdotool key shift+ctrl+t
fi
|
...unsuccessfully. Not sure why.
.
|
Back to top
|
|
 |
fredx181

Joined: 11 Dec 2013 Posts: 3701 Location: holland
|
Posted: Mon 21 May 2018, 19:30 Post subject:
|
|
Does e.g. xdotool search --name "yahoo.com" give any output for you ?
_________________ Dog Linux website
|
Back to top
|
|
 |
fmen
Joined: 07 May 2018 Posts: 51
|
Posted: Mon 21 May 2018, 20:18 Post subject:
|
|
fredx181 wrote: | Does e.g. xdotool search --name "yahoo.com" give any output for you ? |
If the browser's active tab has "yahoo.com" in its name, I get 3984588. The problem is that when I reboot Puppy and run the same command I get a different id number.
I tried using different search options like --onlyvisible and --class but do not have enough background to get the desired results.,
|
Back to top
|
|
 |
misko_2083

Joined: 08 Nov 2016 Posts: 64
|
Posted: Mon 21 May 2018, 23:05 Post subject:
|
|
fmen wrote: | fredx181 wrote: | Does e.g. xdotool search --name "yahoo.com" give any output for you ? |
If the browser's active tab has "yahoo.com" in its name, I get 3984588. The problem is that when I reboot Puppy and run the same command I get a different id number.
I tried using different search options like --onlyvisible and --class but do not have enough background to get the desired results., |
Code: | if xdotool search --name "yahoo.com" 2>&1 >/dev/null; then
xdotool type "hello world"
elif xdotool search --name "google.com" 2>&1 >/dev/null; then
xdotool key shift+ctrl+t
fi
|
Command xdotool search --name "yahoo.com" has exit status 0 if it completes successfully. If not, the exit status is 1 or higher.
2>&1 >/dev/null
2>&1 redirects stderr to stdout and then it's redirected to >/dev/null
/dev/null is like a black hole, the output will neot be printed on the screen if the command's output is redirected here.
How does if works?
if checks if the exit status of some command is equal to 0.
command 'true' always has a return status 0
Code: | if true; then
echo "true"
fi |
if you run this, it will always echo "true"
command 'false' always has a return status 1
Code: | if false; then
echo "true"
fi |
if you run this, it will never echo "true"
|
Back to top
|
|
 |
MochiMoppel

Joined: 26 Jan 2011 Posts: 1774 Location: Japan
|
Posted: Tue 22 May 2018, 00:02 Post subject:
|
|
fmen wrote: | If the browser's active tab has "yahoo.com" in its name, I get 3984588. The problem is that when I reboot Puppy and run the same command I get a different id number. | If the browser's window title contains "yahoo.com" xdotool returns the window ID , in decimal notation, of your browser window. This ID is assigned by the window manager and is different every time you (re)start your browser. You don't even have to reboot.
Try
Code: | ID=$(xdotool search --name "yahoo.com")
if [[ $ID ]];then
xdotool windowactivate $ID
fi |
meaning if variable $ID is not empty activate the window. $ID remains empty if xdotool was unable to find a window matching the search pattern.
Last edited by MochiMoppel on Tue 22 May 2018, 03:55; edited 1 time in total
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 4850 Location: Blue Springs, MO
|
Posted: Tue 22 May 2018, 02:23 Post subject:
|
|
echo $?
Gives the exit status of the previous command - useful for testing
[ A ] && B || C
Run command A, if success run command B else run command C
This is more useful if you create functions like:
GetWid(){
}
OpenInTab(){
}
OpenNew(){
}
_________________ Check out my github repositories. I may eventually get around to updating my blogspot.
|
Back to top
|
|
 |
fredx181

Joined: 11 Dec 2013 Posts: 3701 Location: holland
|
Posted: Tue 22 May 2018, 04:45 Post subject:
|
|
MochiMoppel wrote: | fmen wrote: | If the browser's active tab has "yahoo.com" in its name, I get 3984588. The problem is that when I reboot Puppy and run the same command I get a different id number. | If the browser's window title contains "yahoo.com" xdotool returns the window ID , in decimal notation, of your browser window.
|
Yes, I'm on firefox, and for me also it's the window title only that can be searched.
For example, having this thread open and I do:
Code: | xdotool search --name "murga-linux.com" |
There's no output, but this does give me the window ID:
Code: | xdotool search --name "Puppy Linux Discussion Forum :: View topic - Bash If...then syntax driving me nuts!" |
Or just:
Code: | xdotool search --name "Puppy" |
(however that will find all windows with the name "Puppy")
But maybe it's different on other browsers (e.g. having murga-linux.com in the window title)
Fred
_________________ Dog Linux website
|
Back to top
|
|
 |
misko_2083

Joined: 08 Nov 2016 Posts: 64
|
Posted: Tue 22 May 2018, 07:40 Post subject:
|
|
fredx181 wrote: |
For example, having this thread open and I do:
Code: | xdotool search --name "murga-linux.com" |
There's no output, but this does give me the window ID:
Code: | xdotool search --name "Puppy Linux Discussion Forum :: View topic - Bash If...then syntax driving me nuts!" |
Or just:
Code: | xdotool search --name "Puppy" |
(however that will find all windows with the name "Puppy")
But maybe it's different on other browsers (e.g. having murga-linux.com in the window title)
Fred |
Hi Fred
Command xdotool --name "Puppy" will list all the decimal window ID's that have a Puppy in the window name.
That includes text editors. It's more precise to search for all the window id's with the class and filter out X window properties.
Code: | for windowid in $(xdotool search --onlyvisible --class "firefox")
do
# xprop outputs window id in base sevenn
windowid_bseven=$(printf "0x%07x" "$windowid")
# most modern window managers supprort _NET_WM_NAME X window property if it's set
wmname="$(xprop -notype -id $windowid_bseven _NET_WM_NAME | cut -d'=' -f2)"
# If _NET_WM_NAME is not set window manager reverts to WM_NAME X window property
if [[ "${wmname}" == "" ]]; then
wmname="$(xprop -notype -id $windowid_bseven WM_NAME | cut -d'=' -f2)"
fi
if [[ "${wmname#*' '}" == '"Puppy Linux Discussion Forum :: View topic - Bash If...then syntax driving me nuts! - Mozilla Firefox"' ]];then
xdotool windowactivate $windowid
fi
done |
Last edited by misko_2083 on Tue 22 May 2018, 10:48; edited 1 time in total
|
Back to top
|
|
 |
fmen
Joined: 07 May 2018 Posts: 51
|
Posted: Tue 22 May 2018, 08:49 Post subject:
|
|
Quote: |
Try
Code: | ID=$(xdotool search --name "yahoo.com")
if [[ $ID ]];then
xdotool windowactivate $ID
fi |
meaning if variable $ID is not empty activate the window. $ID remains empty if xdotool was unable to find a window matching the search pattern. |
That is exactly what I was looking for. It works to a tee. Thank you, Mochi.
The rest of the contributions are definitely food for thought, as I wade through beginner's bash.
|
Back to top
|
|
 |
WIckedWitch
Joined: 29 Mar 2018 Posts: 276 Location: West Wales bandit country
|
Posted: Wed 30 May 2018, 19:03 Post subject:
|
|
Potentially controversial suggestion: Use Tcl instead of bash. As it's available cross-platform, it will drive you nuts (a lot less than less than bash) in the same way on any platform on which you use it. And it's MUCH more powerful than bash.
AFAI am concerned BASH is an acronym for "Bloody Awful SH!t. I absolutely hate it and always do things in Tcl instead of bash. Sometimes you require a few more lines but I find it much less brain-ache.
_________________ Sometimes I post mindfully, sometimes not mindfully, and sometimes both mindfully and not mindfully. It all depends on whether and when my mind goes walkies while I'm posting 
|
Back to top
|
|
 |
|