Home > database >  Currently my script's logic is **A or B then C**, but I want my script's logic to be **A o
Currently my script's logic is **A or B then C**, but I want my script's logic to be **A o

Time:10-01

Based on Multiple logical operators, ((A || B) && C), and "syntax error near unexpected token" I tried this...

sh -c [["wmctrl -a 'To Do List - Google Sheets' || (google-chrome --new-window https://docs.google.com/spreadsheets/d/1Oh7XWRTWoy5izkz4eV6G7vZKgdwNeZa_NW-VCdR5GUk/edit#gid=0 && xdotool key F11")]]

but it didn't work.

The URL below is associated with "To Do List - Google Sheets"

sh -c "wmctrl -a 'To Do List - Google Sheets' || "(google-chrome --new-window https://docs.google.com/spreadsheets/d/1Oh7XWRTWoy5izkz4eV6G7vRKgdwNeZa_NW-VCdR5GUk/edit#gid=0 && xdotool key F11)"

Because google-chrome --start-fullscreen does not work on my machine which is running Linux Mint 20.3, I appended xdotool key F11 to cause Google Chrome to open full screen.

That works properly the first time I run the script, but subsequently xdotool key F11 toggles full screen. I understand what is happening, but I don't know how to cause xdotool key F11 to only run in case part B were to run (assuming my script were considered to be A || B) but not part A were to run.

In other words, currently my script's logic is A or B then C, but I want my script's logic to be A or (B then C).

CodePudding user response:

I'd suggest you save yourself some grief by writing it legibly

export url=https://...
sh -c '
    if ! wmctrl -a "To Do List - Google Sheets"; then 
        google-chrome --new-window "$url" && xdotool key F11
    fi
'

For a one-liner, just remove the newlines and add a semicolon before "fi"

sh -c 'if ! wmctrl -a "To Do List - Google Sheets"; then google-chrome --new-window '"$url"' && xdotool key F11; fi'

Or, using the boolean control operators, and braces to group the right-hand side of || (note the semicolon before the close brace is required)

sh -c 'wmctrl -a "To Do List - Google Sheets" || { google-chrome --new-window '"$url"' && xdotool key F11; }'

CodePudding user response:

  • '[[' ']]' is for bash, not sh. So, sh -c "[[" would not work anyway

  • You need a space after '[[' and before ']]'

  • '[[' ']]' are logic operator. Not something to start commands in it. So, I don't think you should be using them at all any way. [[ "firefox" ]] doesn't start firefox at all, and has nothing to do with firefox command. It is just true (meaning that [[ built in returns a 0 exit code) because "firefox" string is not empty. As would be [[ "wqyupfwirarav" ]] or anything. ```

So, from what you said, I surmise that what you are looking for is

wmctrl -a 'To Do List - Google Sheets' || ( google-chrome --new-window "https://docs.google.com/spreadsheets/d/1Oh7XWRTWoy5izkz4eV6G7vZKgdwNeZa_NW-VCdR5GUk/edit#gid=0"   && xdotool key F11" )

Or, even better, since there is no need for subshell (( ... ) starts a new shell in which it executes ... ; so ( bla || foo ) is a little bit like bash -c "bla || foo" )

wmctrl -a 'To Do List - Google Sheets' || { google-chrome --new-window "https://docs.google.com/spreadsheets/d/1Oh7XWRTWoy5izkz4eV6G7vZKgdwNeZa_NW-VCdR5GUk/edit#gid=0"  && xdotool key F11 ; }

Note the compulsory ; at the end of the compound grouping ({ ... }). This doesn't start a subshell. It just executes wmctrl ... command and, if it fails, executes google-chrome command and then the xdotool command.

Note that I also enclosed the google-chrome url in double quote. At first glance it is not necessary here (not even sure tho), but generally speaking url could contain some & or things like that, that could require quoting in order not to be interpreted by bash.

  •  Tags:  
  • bash
  • Related