Visit Our UNIX and Linux User Community


Random variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Random variables
# 1  
Old 04-16-2012
Random variables

I need to create a random String to save in a variable.

Code:
var=`??????`

So I want to replace ?????? with random 8 chars for instance:

is96789h

only letters and numbers

Is there an easy way to do this?

Thanks as always

DV
# 2  
Old 04-16-2012
Code:
var=`perl -MList::Util=shuffle -le '@s = shuffle (a..z, 0..9); print @s[0..7]'`

This User Gave Thanks to yazu For This Post:
# 3  
Old 04-17-2012
Hi.

Here are two more solutions, both using different perl modules.

Observations: the shuffle would not return any duplicates, the Data/Random would not return duplicates until the result is quite long -- longer than the source strings (this is partly a guess and partly observation), the Data/Random/String can return duplicates as seen below.

The choice depends on the application.

Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate perl modules for string of random characters.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

var=`perl -MList::Util=shuffle -le '@s = shuffle (a..z, 0..9); print @s[0..7]'`
pl "Random list as string (shuffle): $var"

var=$(perl -e 'use Data::Random qw(:all);print join("",rand_chars(set => 'alphanumeric', size => 8)),"\n";' ) 
pl " Random list as string (Random): $var"

var=$(perl -e 'use Data::Random::String; print Data::Random::String->create_random_string(length=>8, contains=>"alphanumeric"),"\n";')
pl " Random string (String): $var"

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
perl 5.10.0

-----
Random list as string (shuffle): g2lto9kn

-----
 Random list as string (Random): s6S5A8tD

-----
 Random string (String): 6E6wfo0C

Best wishes ... cheers, drl
# 4  
Old 04-17-2012
Code:
#!/bin/bash

ary=({a..z} {A..Z} {0..9})

for i in {0..7}; do
    echo -n ${ary[RANDOM%${#ary[@]}]}
done
echo


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

6. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

7. UNIX for Dummies Questions & Answers

random words

Hi there folks, for an exercise for my pupils (you know i am always thinking of them!) i need to randomly re-arrange the words (blank space separated) in a sentence (a line in a textfile). Any inspiration?? Txk so much. (9 Replies)
Discussion started by: eldeingles
9 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. Shell Programming and Scripting

$random

I need to use the $RANDOM command to get a line from a list of lines in a file randomly. file is help go three house film how do i randomly get one word without looking into the file? (6 Replies)
Discussion started by: relle
6 Replies

10. Shell Programming and Scripting

Random

My problem is as follow and i hope you can help: I currently have this function: stored_word() { number=$RANDOM let "number %= 21" case $number in 0 ) echo "energy" ;; 1 ) echo "touch" ;; 2 ) echo "climbing" ;; 3 ) echo "declare" ;; 4 ) echo "marry" ;; 5 ) echo "relax" ... (8 Replies)
Discussion started by: keyvan
8 Replies

Featured Tech Videos