cazz
Posts: 223
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

Help with random bash

Mon Feb 22, 2021 6:53 am

Hi
I have this code that I have done something wrong and now it does not working.

Code: Select all

hoice = $((1 + $RANDOM % 6))
for a in /home/pi/kat$choice/* do omxplayer "$a" done
That I want to do is random play files in folder that have name "kat1,kat2,kat3,kat4,kat5,kat6"

But I get error
/home/pi/jingel.sh: line 1: =: command not found
/home/pi/jingel.sh: line 3: syntax error: unexpected end of file
/Cazz

Heater
Posts: 17441
Joined: Tue Jul 17, 2012 3:02 pm

Re: Help with random bash

Mon Feb 22, 2021 7:00 am

looks like "hoice" should be "choice" in that first line.
Memory in C++ is a leaky abstraction .

User avatar
rpdom
Posts: 18156
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Help with random bash

Mon Feb 22, 2021 7:01 am

There shouldn't be spaces either side of the "=" sign and you need some semicolons or line breaks in the "for; do ... ;done" statement.

Code: Select all

choice=$((1 + RANDOM % 6))
for a in /home/pi/kat$choice/*; do omxplayer "$a"; done
or

Code: Select all

choice=$((1 + RANDOM % 6))
for a in /home/pi/kat$choice/*
do
  omxplayer "$a"
done
Unreadable squiggle

cazz
Posts: 223
Joined: Wed Nov 14, 2012 9:01 pm
Location: Sweden

Re: Help with random bash

Mon Feb 22, 2021 7:53 am

Ahhh yes that is correct.
I was in a hurry when I did write the code and that is never good.

Thanks so much.
/Cazz

Heater
Posts: 17441
Joined: Tue Jul 17, 2012 3:02 pm

Re: Help with random bash

Mon Feb 22, 2021 10:35 am

cazz wrote:
Mon Feb 22, 2021 7:53 am
I was in a hurry when I did write the code and that is never good.
Do not hurry. As it said on the first page of the repair manual for my motorcycle years ago:

"Repair of Yamaha motorcycle require great peace of mind"

And so it is with programming.
Memory in C++ is a leaky abstraction .

sri-arjuna
Posts: 20
Joined: Thu Feb 11, 2021 7:22 am

Re: Help with random bash

Thu Mar 04, 2021 4:05 pm

Since your issue has already been solved, I just wanted to share that wrote myself a casual 'rnd' function:
You seem to want a random value between 1 and 6:

Code: Select all

choice=$(rnd 1 6)
The source (you might need to adjust some variables to their regarding commands, as that is part of the framework (cross-plattform compatibilty):

Code: Select all

~/prjs/SWARM] 0 $ . ./runtime 
...............[~/prjs/SWARM] 1 $ type rnd
rnd ist eine Funktion.
rnd () 
{ 
    doPerm=false;
    doList=false;
    PERM_NUM="";
    LIST_NUM="";
    MIN="";
    MAX="";
    c=0;
    case "$1" in 
        "-p" | "--perm")
            doPerm=true;
            PERM_NUM="$2";
            case $# in 
                1 | 2)
                    MIN=$(( $1 ));
                    MAX=$(( $1 + PERM_NUM ))
                ;;
            esac;
            shift 2
        ;;
        "-l" | "--list")
            doList=true;
            LIST_NUM="$2";
            shift 2
        ;;
    esac;
    case $# in 
        0)
            MIN=${MIN:-1};
            MAX=${MAX:-100}
        ;;
        1)
            MIN=${MIN:-1};
            MAX=${MAX:-$1}
        ;;
        2)
            MIN=${MIN:-$1};
            MAX=${MAX:-$2}
        ;;
    esac;
    function _myRND () 
    { 
        VAR=$(( RANDOM % MAX ));
        while [[ $VAR -lt $MIN ]] || [[ $VAR -gt $MAX ]]; do
            VAR=$(( RANDOM % MAX ));
            ((VAR++));
        done;
        $ECHO "$VAR"
    };
    if $doList; then
        declare -A L;
        while [[ $c -lt $LIST_NUM ]]; do
            n=$(_myRND);
            while $ECHO "${L[@]}" | $GREP -q "$n"; do
                n=$(_myRND);
            done;
            L[$c]=$n;
            ((c++));
        done;
        $ECHO "${L[@]}";
        unset L;
    else
        if $doPerm; then
            declare -A P;
            R=$RANDOM;
            for x in $(eval "$ECHO" "{${MIN}..${MAX}}");
            do
                while [[ -n "${P[$R]}" ]]; do
                    R=$RANDOM;
                done;
                P[$R]="$x";
            done;
            $ECHO "${P[@]}";
            unset P;
        else
            _myRND;
        fi;
    fi
}
'Manpage':

Code: Select all

rnd(1)                                  SWARM Manual                                  rnd(1)

NAME
       rnd - Prints a random number within range (default: 0-100)

SYNOPSYS
       rnd [ -p COUNT VALUE ] [ -l COUNT ] [MAXVAL] | [MINVAL MAXVAL]

OPTIONS
         -p COUNT VALUE    Prints COUNT permutations of VALUE
         -l COUNT          Prints COUNT random numbers

EXAMPLE
       rnd
       rnd -p 4 150
       rnd -l 5 300 600
Hope this helps & have fun :)
Last edited by sri-arjuna on Thu Mar 04, 2021 5:01 pm, edited 1 time in total.

GlowInTheDark
Posts: 1383
Joined: Sat Nov 09, 2019 12:14 pm

Re: Help with random bash

Thu Mar 04, 2021 4:16 pm

Why bother with that?

Just use the built-in RANDOM variable, as OP has done.

Note: Yours might be technically superior - perhaps it gives "better" random numbers - but I doubt it would be worth the trouble for the OP (or anyone else) to go through the bother of debugging it to make it suitable for their own use.

BTW, wasn't there another thread on this same topic some time ago? I remember giving an answer to the same question (the names kat1, kat2, etc rang a bell with me).

Edit: Oh, yeah. This thead is similar - but different:

viewtopic.php?f=34&t=293021&p=1770827&h ... 1#p1770827

Edit 2: You can use the bash "let" statement to make dealing with arithmetic a little easier.

Example:

$ let 'choice = 1 + RANDOM % 6'

The quotes are necessary, but beyond that, a lot of the extra syntax involved in doing it without "let" goes away.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Loves Linux; loves to dance.

Heater
Posts: 17441
Joined: Tue Jul 17, 2012 3:02 pm

Re: Help with random bash

Thu Mar 04, 2021 5:17 pm

GlowInTheDark wrote:
Thu Mar 04, 2021 4:16 pm
Why bother with that?

Just use the built-in RANDOM variable, as OP has done.
Because getting a random number by simply doing a modulus of 6 on RANDOM will result in very biased random numbers. Perhaps not a big deal but given that it so easy to fix then why not?
Memory in C++ is a leaky abstraction .

GlowInTheDark
Posts: 1383
Joined: Sat Nov 09, 2019 12:14 pm

Re: Help with random bash

Thu Mar 04, 2021 5:39 pm

Very unlikely that it matters, and it doesn't look at all "easy" to me.

Lots of code there. All of which needs to be debugged (aka, checked to make sure no security holes).
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Loves Linux; loves to dance.

Return to “Other programming languages”