Variable interpolation in shell script that returns the output of a function

3

I've solved a problem with a larger script I'm producing. Basically I have a function that returns the current date and time in a specific format. I need every time the variable data_hora is printed on the screen, its values are updated from the return of execution of that function.

UPDATE

[12/28/2015]

It is important to note that the variable must be interpolated into a string. In case, every time we print% w / o the output should be the updated date.

This code is used in a script that is loaded only once (it composes my .dotfiles). Every time this variable is interpolated into a string it has returned to the date / time the font of the script was loaded and not the current time.

For example, we print the variable 3x at intervals of 1 second.

The desired output is as follows:

[27/12/2015 22:26:00]

[27/12/2015 22:26:01]

[27/12/2015 22:26:02]

However, the sample code below outputs the following:

[27/12/2015 22:26:00]

[27/12/2015 22:26:00]

[27/12/2015 22:26:00]

Below the source code:

function data_hora_atual {
    echo 'date +"[%d/%m/%Y %H:%M:%S]"'
}

obter_data=$(data_hora_atual)

data_hora=$(echo "${obter_data}")

echo ${data_hora}

sleep 1

echo ${data_hora}

sleep 1

echo ${data_hora}
    
asked by anonymous 28.12.2015 / 03:43

4 answers

2

Its function data_hora_atual is returned echo and not a command itself, so the two variables $obter_data and $data_hora were saving the output of echo and not date .

Well, I did an adulteration in your script and it worked:

#!/bin/bash

function data_hora_atual {
    date +"[%d/%m/%Y %H:%M:%S]"
}

data_hora_atual 

sleep 1

data_hora_atual 

sleep 1

data_hora_atual
    
28.12.2015 / 04:41
1
#!/bin/bash

function data_hora_atual {
    date +"[%d/%m/%Y %H:%M:%S]"
}

clear # limpa tela

obter_data=data_hora_atual # instancia da função

data_hora=${obter_data} # interpolação da instancia

data_hora_atual # 1° print da função

sleep 1

${obter_data} # 2° print da instancia

sleep 1

${data_hora} # 3° print da interpolação
    
28.12.2015 / 21:36
0

To do what you want, just put the contents of the string that you want to be executed in the deep accents'

#!/bin/bash
print_date="date +[%d/%m/%Y0\x20%H:%M:%S]" # \x20 evita problemas de quebra de lista de argumentos

echo '${print_date}'

sleep 1

echo '${print_date}'

sleep 1

echo '${print_date}'

Serious accents will execute the statement contained within and return the output which in the case is return date . It can not be reversed as in its example, since% wont% will get get_data receive a value that does not change unless it is executed again, which does not happen in the example.

    
28.12.2015 / 22:42
0

Maybe

function data_hora_atual {
    echo 'date +"[%d/%m/%Y %H:%M:%S]"'
}

obter_data=$(data_hora_atual)

data_hora=$(echo "${obter_data}")

echo ${data_hora}

sleep 1

obter_data=$(data_hora_atual)

data_hora=$(echo "${obter_data}")

echo ${data_hora}

sleep 1

obter_data=$(data_hora_atual)

data_hora=$(echo "${obter_data}")

echo ${data_hora}
    
21.09.2016 / 20:23