-->

DEVOPSZONES

  • Recent blogs

    How to Generate Random Numbers in Linux Shell Scripting with an index

     There could be situations wherein you need to create random number or a random file name or a random password. Lets do it through a shell script. 


    To generate a random number in a UNIX or Linux shell, the shell called a shell variable named RANDOM. Each time this variable is called, a random number between 0 and 32767 is generated.

    The RANDOM variable should be initialized  to an initial value (ike the process ID of the current shell, which is maintained by the variable $$.

    Example 1: 

    Lets check how we generate 10 random numbers. 

    Script:

    generate 10 random numbers
    generate 10 random numbers
    Output: Make the script executable and then run the script.

    [root@devopszones ~]#chmod +x 10-random-numbers.sh
    [root@devopszones ~]#
    [root@devopszones ~]#
    [root@devopszones ~]#./10-random-numbers.sh
    14499
    24829
    17111
    28602
    9732
    22096
    21859
    31971
    14950
    4219
    [root@devopszones ~]#
    
    Example 2: Let Generate as many as random numbers you want

    We will generate as many as random numbers we want by giving a positional argument while executing the script.

    Script:

    with Positional argument
    with Positional argument

    Output: Make the script executable and then run the script.

    [root@devopszones ~]#chmod +x random_generate_with_positional_arg.sh
    [root@devopszones ~]#
    [root@devopszones ~]#
    [root@devopszones ~]#./random_generate_with_positional_arg.sh 12
    4325
    14709
    14637
    25770
    176
    18926
    15211
    9889
    18253
    11598
    31544
    14930
    23961
    [root@devopszones ~]#
    
    Example 3: Let Generate as many as random numbers you want with an Index
    We will generate as many as random numbers we want by giving a positional argument while executing the script. It will add an index to it.

    Script:

    positional argument with index
    positional argument with index

    Output: Make the script executable and then run the script.

    [root@devopszones ~]#chmod +x random_generate_with_positional_arg_index.sh
    [root@devopszones ~]#./random_generate_with_positional_arg_index.sh 13
    0 27576
    1 3467
    2 10018
    3 12771
    4 14833
    5 32188
    6 24743
    7 10792
    8 26533
    9 28763
    10 13392
    11 13133
    12 24793
    13 23472
    [root@devopszones ~]#
    
    Example 4: Let Generate as many as random numbers you want with an Index and with a default value for Positional argument.
    We will generate as many as random numbers we want by giving a positional argument while executing the script. It will add an index to it. If we do not provide an positional argument to the script it'll take 10 as default value.

    Script:
    Positional Argument with default Value
    Positional Argument with default Value

    Output: Make the script executable and then run the script.

    [root@devopszones ~]#chmod +x random_num_sec_example_default.sh
    [root@devopszones ~]#
    [root@devopszones ~]#
    [root@devopszones ~]#
    [root@devopszones ~]#./random_num_sec_example_default.sh
    0 28281
    1 19562
    2 24905
    3 5437
    4 2123
    5 10329
    6 7947
    7 7441
    8 28206
    9 15030
    10 9124
    [root@devopszones ~]#./random_num_sec_example_default.sh 14
    0 20047
    1 9996
    2 2719
    3 22236
    4 7134
    5 14774
    6 31742
    7 24772
    8 9092
    9 13406
    10 13866
    11 15672
    12 17290
    13 16315
    14 3968
    [root@devopszones ~]#
    
    Example 5: Let Generate as many as random numbers you want with an Index and with a default value for Positional argument. Send the Generated value to a file.
    We will generate as many as random numbers we want by giving a positional argument while executing the script. It will add an index to it. If we do not provide an positional argument to the script it'll take 10 as default value. As a final nail in the coffin it'll send the output to a file.

    Script:
    Positional Argument with default Value. Send Output to a file
    Positional Argument with default Value. Send Output to a file

    Output: Make the script executable and then run the script.

    [root@devopszones ~]#chmod +x random_num_sec_example.sh
    [root@devopszones ~]#./random_num_sec_example.sh 15
    [root@devopszones ~]#cat inputfile3
    0 6749
    1 21349
    2 17039
    3 30703
    4 593
    5 11085
    6 26653
    7 26761
    8 13332
    9 3551
    10 26855
    11 19697
    12 32595
    13 25168
    14 12581
    15 31042
    [root@devopszones ~]#
    




    No comments