Bash If...then syntax driving me nuts!

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
fmen
Posts: 51
Joined: Mon 07 May 2018, 12:22

Bash If...then syntax driving me nuts!

#1 Post by fmen »

Been working on this for hours, googling and googling but no pleasure.

Code: Select all

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

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

Re: Bash If...then syntax driving me nuts!

#2 Post by fredx181 »

fmen wrote:Been working on this for hours, googling and googling but no pleasure.

Code: Select all

#!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: Select all

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: Select all

#!/bin/bash

url1=$(xdotool search --name "yahoo.com")
echo $url1
if [[ $url1 == "39845889" ]]; then
xdotool windowactivate 39845889
fi
Fred

fmen
Posts: 51
Joined: Mon 07 May 2018, 12:22

#3 Post by fmen »

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.

fmen
Posts: 51
Joined: Mon 07 May 2018, 12:22

#4 Post by fmen »

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: Select all

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.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#5 Post by fredx181 »

fmen wrote:

Code: Select all

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: Select all

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

fmen
Posts: 51
Joined: Mon 07 May 2018, 12:22

#6 Post by fmen »

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: Select all

#!/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.
.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#7 Post by fredx181 »

Does e.g. xdotool search --name "yahoo.com" give any output for you ?

fmen
Posts: 51
Joined: Mon 07 May 2018, 12:22

#8 Post by fmen »

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

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#9 Post by misko_2083 »

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: Select all

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: Select all

if true; then
   echo "true"
fi
if you run this, it will always echo "true"

command 'false' always has a return status 1

Code: Select all

if false; then
   echo "true"
fi
if you run this, it will never echo "true"

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#10 Post by MochiMoppel »

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: Select all

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, 07:55, edited 1 time in total.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

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 [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#12 Post by fredx181 »

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: Select all

xdotool search --name "murga-linux.com"
There's no output, but this does give me the window ID:

Code: Select all

xdotool search --name "Puppy Linux Discussion Forum :: View topic - Bash If...then syntax driving me nuts!"
Or just:

Code: Select all

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

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#13 Post by misko_2083 »

fredx181 wrote: For example, having this thread open and I do:

Code: Select all

xdotool search --name "murga-linux.com"
There's no output, but this does give me the window ID:

Code: Select all

xdotool search --name "Puppy Linux Discussion Forum :: View topic - Bash If...then syntax driving me nuts!"
Or just:

Code: Select all

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: Select all

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, 14:48, edited 1 time in total.

fmen
Posts: 51
Joined: Mon 07 May 2018, 12:22

#14 Post by fmen »


Try

Code: Select all

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.

WIckedWitch
Posts: 276
Joined: Thu 29 Mar 2018, 22:13
Location: West Wales bandit country

#15 Post by WIckedWitch »

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 :?

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#16 Post by jafadmin »

WIckedWitch wrote:Potentially controversial suggestion: Use Tcl instead of bash.
No. Linux ships with bash. It's ubiquitous. Why write scripts in a language that doesn't ship with the OS? That is the true definition of insanity.
It's like saying you invented a really superior PC that works great with 71HZ AC ..

WIckedWitch
Posts: 276
Joined: Thu 29 Mar 2018, 22:13
Location: West Wales bandit country

#17 Post by WIckedWitch »

jafadmin wrote:
WIckedWitch wrote:Potentially controversial suggestion: Use Tcl instead of bash.
No. Linux ships with bash. It's ubiquitous. Why write scripts in a language that doesn't ship with the OS? That is the true definition of insanity.
It's like saying you invented a really superior PC that works great with 71HZ AC ..
My reason for not using something that ships with the OS is that scripts written in an OS-specific language are not portable across platforms. By using Tcl, I have to be familiar with just one scripting shell across all platforms that I use: Windows, MacOS, Linux and various *NiXes.

And by using Tk and other Tcl extensions such as SQlite and Expect, I can do more in Tcl more easily than I could using OS-specific shell scripts. Indeed I routinely prototype applications in Tcl, then, if they need to be in a native-compiled language, simply re-implement in C using the Tcl C API.

It's not without reason that Tcl is sometimes called the "Swiss Army knife of programming languages", or sometimes "the best kept secret in software engineering"..

I'll admit, however, that my inclination to write things portably has in the past been regarded by some colleagues as "anally obsessive". That's almost right because I'm actually autistically obsessive.
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 :?

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#18 Post by jafadmin »

WIckedWitch wrote:My reason for not using something that ships with the OS is that scripts written in an OS-specific language are not portable across platforms. By using Tcl, I have to be familiar with just one scripting shell across all platforms that I use: Windows, MacOS, Linux and various *NiXes.
That's great when you are only concerned about your own needs. It does absolutely nothing for others though.

If you peruse this forum you will find hundreds of examples of Linux users helping others with scripts. That is only possible because all have a common shell.

Don't hold your breath waiting for everyone to install tcl just so they can run your scripts.

The cool thing about Computer Science is you learn to program in all languages.

WIckedWitch
Posts: 276
Joined: Thu 29 Mar 2018, 22:13
Location: West Wales bandit country

#19 Post by WIckedWitch »

jafadmin wrote:
WIckedWitch wrote:My reason for not using something that ships with the OS is that scripts written in an OS-specific language are not portable across platforms. By using Tcl, I have to be familiar with just one scripting shell across all platforms that I use: Windows, MacOS, Linux and various *NiXes.
That's great when you are only concerned about your own needs. It does absolutely nothing for others though.

If you peruse this forum you will find hundreds of examples of Linux users helping others with scripts. That is only possible because all have a common shell.

Don't hold your breath waiting for everyone to install tcl just so they can run your scripts.

The cool thing about Computer Science is you learn to program in all languages.
That's fine. I did say earlier on that recommending Tcl was a little provocative. Basically It's down to my own dilapidations. I've always found sh and bash scripts impossible to read, so I never use them except in tiny trivial cases.

Whenever I set up a new OS on a machine, the first three things I install are gcc, Tcl/Tk and Python. Thereafter, I never go near sh and bash unless I need to execute the very occasional sh or bash command from within a Tcl script.

I agree with you about using lots of programming languages but there are just some that I avoid like the plague because I find them disagreeable to use. Over the years I have used: Algol60, Algol68, Ada, BASIC, BCPL, C, C++, CCS, COBOL, CPN, CSP, Eiffel, Erlang, Estelle, FORTH, FORTRAN, Java, Javascript, LISP, LOTOS, Lua, Matlab, ML, OCAML, Octave, Pascal, Perl, Prolog, Python, R, Ruby, SCADE, Smalltalk, Tcl, Visual Basic, Z, various shell scripting languages, assembler, and maybe one or two others that escape me. (Not all of these are PLs - some are formal description languages.)

After all that lot, one develops favourites and things one won't touch with a barge pole. I viscerally loathe: C++, Estelle, Java, Javascript. LISP, Perl, Visual Basic and the sh and bash shell languages. Nowadays, at 65, I'm a lot slower at learning things, so I stick with the set of languages that I feel comfortable with - and generally, the more formally defined, the better.

My language prejudices are undoubtedly strongly coloured by having worked for 25 years in safety-, security- and mission-critical systems.

Sorry if I came on a bit strong as regards Tcl. I didn't mean to sound dismissive. It's just me becoming a grumpy old woman :-)
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 :?

Post Reply