samedi 24 avril 2021

BASH - Problem with generating a different random string in each curl command

I did a thorough research everywhere I could and yet came up short (partial solutions) on solving the problem described in the Title.

I tried adapting my script by using this solution found here - https://stackoverflow.com/a/32484733/7238741

chars=abcd1234ABCD
for i in {1..8} ; do
    echo -n "${chars:RANDOM%${#chars}:1}"
done
echo

PROBLEM: It will generate a random string for each curl command in my bash script, the problem is when it generates a, let's say, string of 5 characters, it will use 'neighbor' characters i.e. 45678 or defgh. So perhaps we should leave this solution aside completely, and focus on the next one.

Next, I used this solution in my script which seems would've done the job 100% - https://gist.github.com/earthgecko/3089509 - but... here's a practical example what I'd like to achieve - each of the two curl commands should end up posting a Random and Different comment which, in this case, consists of 5 alpha-numeric characters:

Here's v1 of the bash script:

#!/bin/bash

NEW_UUID=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1)

curl "https://mywebsite.com/comment... ...&text=$NEW_UUID"; sleep 60;
curl "https://mywebsite.com/comment... ...&text=$NEW_UUID"; sleep 60;

Here's v2 of the bash script (I will use this one, I posted the more simple v1 just in case it's easier for someone to provide a solution)

#!/bin/bash

NEW_UUID=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1)

for i in {1..2}; do curl "https://mywebsite.com/comment... ...&text=$NEW_UUID"; sleep 60; done

PROBLEM: In both versions, the script will generate a different random string each time it's executed, but that one string will be used for both curl commands, resulting in always posting the exact same comment both times ( ex: TuJ1a ), and the goal is to generate a different and random string for each curl command.

In the end, the first few answers to this Question which is somewhat the same as mine, do provide a solution how to "generate a different string in each line" - https://unix.stackexchange.com/questions/428737/how-can-i-add-random-string-for-each-line - it's just Their answers are adapted to OP's personal script, and unfortunately for me I do not have the proper knowledge to adapt those solutions to my 'v2 script'.

Thank you for any assistance / hint you may provide <3




Aucun commentaire:

Enregistrer un commentaire