linuxbashloopsfor-loopbatch-file

Conversion of Web Dictionary tool script from batch to bash to make it work by any creative method


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"

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.


Solution

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

    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:

    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:

    Testing results:

    enter image description here


    NOTES: