I want to improve my bash script so that it opens new tabs with term expressions in Polish. I want it to work with Linux.
What have I been able to do reflectively to the bash script?
” en_dictionary.sh ” (MVE just added after the first correction is down entry, scroll down)
#!/bin/bash
url="https://www.diki.pl/slownik-angielskiego?q="
for e in $(cat lst-pl_wrds.txt)
do
for o in $e
do
url="${url}${o}&end"
firefox --new-tab "$url" & timeout 1 >nul
done
done
The example above although syntactically correct will not be able to perform the task as we imagine due to the fact that it is a puppet. It is only for the outline of functionality.
To stimulate the imagination of how it should behave I will show the batch script, which is fully operational under Windows.
” en_true_up.bat ” (working example from another OS)
@ECHO OFF
SETLOCAL
REM remember set "Encoding" to "ANSI" for "lst-pl_wrds.txt"
chcp 1250
fOR /f "delims=" %%e IN ('type "lst-pl_wrds.txt"') DO FOR %%o iN (%%e) do start "%%o" /d "C:Program FilesMozilla Firefox" firefox.exe "https://www.diki.pl/slownik-angielskiego?q=%%o&end"&timeout /t 1 >nul
GOTO :EOF
” lst-pl_wrds.txt “
łódka dwa trzy cztery piec
Based on a working batch script and dummy bash can create proper bash instructions, with the same power as on Windows?
What do I want? Working script with dictionary under Linux. Something that will be an efficient conversion and modification of the script.
The solution proposed by you can also be out of the box with unix family features unique to a particular system.
I also found some helpful materials in the development process:
xargs -a ff_url.txt firefox -new-tab "$line"
Source: https://superuser.com/questions/829117/open-a-new-tab-window-in-an-existing-firefox-instance-on-linux
Addition: https://wiki.mozilla.org/Firefox/CommandLineOptions
Have a successful interesting riddle. Thank you for any suggestions and comments.
Is there a good solution from a wider perspective?
Good luck.
MCVE just edited:
The en_dictionary.sh
script and the lst-pl_wrds.txt
file required for it, which should be saved as UTF-8
in this case, should return working tabs, which is not the case.
Execution of the mentioned sh
script returns tabs with the following URLs
and on top of that in a random order which is not the expected behavior and the string is invalid:
(1) https://www.diki.pl/slownik-angielskiego?q=%C5%82%C3%B3dka&end (?q=łódka&end)
(2) https://www.diki.pl/slownik-angielskiego?q=%C5%82%C3%B3dka&enddwa&endtrzy&endcztery&endpiec&end (?q=łódka&end&enddwa&endtrzy&endcztery&endpiec&end)
(3) https://www.diki.pl/slownik-angielskiego?q=%C5%82%C3%B3dka&enddwa&endtrzy&endcztery&end (?q=łódka&end&enddwa&endtrzy&endcztery&end)
(4) https://www.diki.pl/slownik-angielskiego?q=%C5%82%C3%B3dka&enddwa&end (?q=łódka&end&enddwa&end)
(5) https://www.diki.pl/slownik-angielskiego?q=%C5%82%C3%B3dka&enddwa&endtrzy&end (?q=łódka&end&enddwa&endtrzy&end)
I would like script en_dictionary.sh
to be fixed and improved in a way that executing it will open the following tabs in the firefox browser and preferably maintaining their order. This is the behavior I expect:
https://www.diki.pl/slownik-angielskiego?q=łódka&end
https://www.diki.pl/slownik-angielskiego?q=dwa&end
https://www.diki.pl/slownik-angielskiego?q=trzy&end
https://www.diki.pl/slownik-angielskiego?q=cztery&end
https://www.diki.pl/slownik-angielskiego?q=piec&end
The most important thing for me is the effect and correct result of the script execution. The method is not important. I expect the above action.
I would like to note two more small facts about the operation of this flawed script, which are not expected. Error in the terminal that says Try 'timeout --help' for more information.
and creates a nul
file in the destination directory, which should not be the case.
I hope the information is complete and will help create a working en_dictionary.sh
file that is the purpose of the topic.
8
Setup:
$ cat lst-pl_wrds.txt
łódka dwa trzy cztery piec
rok jutro
NOTES:
- for demonstration purposes I’ve …
- added extra spacing between words
- added 2nd line of words
Modifying OP’s script to print some debugging info:
$ cat testme
#!/bin/bash
url="https://www.diki.pl/slownik-angielskiego?q="
for e in $(cat lst-pl_wrds.txt)
do
typeset -p e
for o in $e
do
url="${url}${o}&end"
typeset -p url
# firefox --new-tab "$url" & timeout 1 >nul
done
done
Running the script generates:
$ ./testme
declare -- e="łódka"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&end"
declare -- e="dwa"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&enddwa&end"
declare -- e="trzy"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&enddwa&endtrzy&end"
declare -- e="cztery"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&enddwa&endtrzy&endcztery&end"
declare -- e="piec"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&enddwa&endtrzy&endcztery&endpiec&end"
declare -- e="rok"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&enddwa&endtrzy&endcztery&endpiec&endrok&end"
declare -- e="jutro"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&enddwa&endtrzy&endcztery&endpiec&endrok&endjutro&end"
Two things to notice from this output:
- 1) the
e
variable only ever contains a single string, but from the code (for o in $e
) it appears OP is expecting multiple strings - this happens because
for e in $(cat lst-pl_wrds.txt)
effectively becomesfor e in łódka dwa trzy cztery piec rok jutro
(all words placed on a single line with a single space as delimiter) - in this particular case this behavior is ‘ok’ as long as we are dealing with items that contain spaces (eg, compound words, phrases, etc)
- which in turn means the inner loop is not required … in this case
- in the proposed answer (below) we’ll go ahead and keep the inner loop but we’ll need to modify the outer loop to work on a single line of input at a time
- 2) the
url
variable keeps appending each new word on the end of the previousurl
value - this is occurring because that’s exactly what OP is requesting with
url="${url}${o}&end"
; OP is using the sameurl
variable for the ‘base’ url as well as the ‘new’ url - what OP probably wants is to use two variables, one for the base url and one for the word-related url
Modifying our current test script to address the above issues:
$ cat testme
#!/bin/bash
base_url="https://www.diki.pl/slownik-angielskiego?q=" # one variable for base url
while read -r -a arr # split line on white space and place words in array "arr[]"
do
typeset -p arr
for o in "${arr[@]}" # step through list of array items
do
url="${base_url}${o}&end" # different variable for word-related url
typeset -p url
# firefox --new-tab "${url}" & timeout 1 >nul
done
done < lst-pl_wrds.txt
The modified script generates:
declare -a arr=([0]="łódka" [1]="dwa" [2]="trzy" [3]="cztery" [4]="piec")
declare -- url="https://www.diki.pl/slownik-angielskiego?q=łódka&end"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=dwa&end"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=trzy&end"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=cztery&end"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=piec&end"
declare -a arr=([0]="rok" [1]="jutro")
declare -- url="https://www.diki.pl/slownik-angielskiego?q=rok&end"
declare -- url="https://www.diki.pl/slownik-angielskiego?q=jutro&end"
From this output we see:
- our array
ar[]
is (re)populated with the words from a single line from our input file url
is no longer appending multiple words together
Rolling these modifications into OP’s current script:
$ cat dictionary.sh
#!/bin/bash
base_url="https://www.diki.pl/slownik-angielskiego?q="
while read -r -a arr
do
for o in "${arr[@]}"
do
url="${base_url}${o}&end"
firefox --new-tab "${url}" >/dev/null &
sleep 1
done
done < lst-pl_wrds.txt
NOTES:
- I’m assuming OP’s use of
>nul
is meant to discard/ignore stdout from thefirefox
call; in this case the unix/linux equivalent is>/dev/null
- the link OP provides for
firefox
command line options does not include anything related to ‘timeouts’ so I’m assuming OP’s intention is to wait 1 second between eachfirefox
invocation; if this assumption is wrong then OP can modify thefirefox
call as needed (and remove thesleep 1
?)
the&
on the end of thefirefox
call will place the call in the background
Testing results:
- I use
brave-browser
so I replaced thefirefox ...
line withbrave-browser "${url}" > /dev/null &
- when I run the script it opens new tabs at 1-second intervals in the active
brave-browser
window (ie, thebrave-browser
window I’m typing this answer into); each tab represents a single word and the tabs are displayed in the same order as they are read from the file:
NOTES:
- if OP finds the script has some issues displaying the last word from each line (
piec
andjutro
in this example) then it may be an issue of the file containing windows/dos line endings (rn
) - OP can run
od -c lst-pl_wrds.txt
and if the output containsr
entries then this confirms the windows/dos line endings exist in the file - to remove the line endings OP can run
dos2unix lst-pl_wrds.txt
; this only needs to be run once as it will edit the file and remove ther
characters