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 Files\Mozilla 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"
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.
Setup:
$ cat lst-pl_wrds.txt
łódka dwa trzy cztery piec
rok jutro
NOTES:
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:
e
variable only ever contains a single string, but from the code (for o in $e
) it appears OP is expecting multiple stringsfor e in $(cat lst-pl_wrds.txt)
effectively becomes for e in łódka dwa trzy cztery piec rok jutro
(all words placed on a single line with a single space as delimiter)url
variable keeps appending each new word on the end of the previous url
valueurl="${url}${o}&end"
; OP is using the same url
variable for the 'base' url as well as the 'new' urlModifying 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:
ar[]
is (re)populated with the words from a single line from our input fileurl
is no longer appending multiple words togetherRolling 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:
>nul
is meant to discard/ignore stdout from the firefox
call; in this case the unix/linux equivalent is >/dev/null
firefox
command line options does not include anything related to 'timeouts' so I'm assuming OP's intention is to wait 1 second between each firefox
invocation; if this assumption is wrong then OP can modify the firefox
call as needed (and remove the sleep 1
?)
the &
on the end of the firefox
call will place the call in the backgroundTesting results:
brave-browser
so I replaced the firefox ...
line with brave-browser "${url}" > /dev/null &
brave-browser
window (ie, the brave-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:
piec
and jutro
in this example) then it may be an issue of the file containing windows/dos line endings (\r\n
)od -c lst-pl_wrds.txt
and if the output contains \r
entries then this confirms the windows/dos line endings exist in the filedos2unix lst-pl_wrds.txt
; this only needs to be run once as it will edit the file and remove the \r
characters