How to create a bash variable like $RANDOM

后端 未结 3 1776
不思量自难忘°
不思量自难忘° 2021-01-16 23:46

I\'m interest in some thing : every time I echo $RANDOM , the show value difference . I guess the RANDOM is special (When I read it , it may call a function , s

相关标签:
3条回答
  • 2021-01-16 23:50

    The special behavior of $RANDOM is a built-in feature of bash. There is no mechanism for defining your own special variables.

    You can write a function that prints a different value each time it's called, and then invoke it as $(func). For example:

    now() {
        date +%s
    }
    
    echo $(now)
    

    Or you can set $PROMPT_COMMAND to a command that updates a specified variable. It runs just before printing each prompt.

    i=0
    PROMPT_COMMAND='((i++))'
    

    This doesn't work in a script (since no prompt is printed), and it imposes an overhead whether you refer to the variable or not.

    0 讨论(0)
  • 2021-01-16 23:55

    If you are BASH scripting there is a $RANDOM variable already internal to BASH. This post explains the random variable $RANDOM:

    http://tldp.org/LDP/abs/html/randomvar.html

    It generates a number from 0 - 32767.

    If you want to do different things then something like this:

    case $RANDOM in
    [1-10000])
      Message="All is quiet."
      ;;
    [10001-20000])
      Message="Start thinking about cleaning out some stuff.  There's a partition that is $space % full."
      ;;
    [20001-32627])
      Message="Better hurry with that new disk...  One partition is $space % full."
      ;;
    esac
    
    0 讨论(0)
  • 2021-01-17 00:02

    I stumbled on this question a while ago and wasn't satisfied by the accepted answer: He wanted to create a variable just like $RANDOM (a variable with a dynamic value), thus I've wondered if we can do it without modifying bash itself.

    Variables like $RANDOM are defined internally by bash using the dynamic_value field of the struct variable. If we don't want to patch bash to add our custom "dynamic values" variables, we still have few other alternatives.

    An obscure feature of bash is loadable builtins (shell builtins loaded at runtime), providing a convenient way to dynamically load new symbols via the enable function:

    $ enable --help|grep '\-f'
    enable: enable [-a] [-dnps] [-f filename] [name ...]
          -f        Load builtin NAME from shared object FILENAME
          -d        Remove a builtin loaded with -f
    

    We now have to write a loadable builtin providing the functions (written in C) that we want use as dynamic_value for our variables, then setting the dynamic_value field of our variables with a pointer to the chosen functions.

    The production-ready way of doing this is using an another loadable builtin crafted on purpose to do the heavy-lifting, but one may abuse gdb if the ptrace call is available to do the same.

    I've made a little demo using gdb, answering "How to create a bash variable like $RANDOM?":

    $ ./bashful RANDOM dynamic head -c 8 /dev/urandom > /dev/null
    $ echo $RANDOM
    L-{Sgf
    
    0 讨论(0)
提交回复
热议问题