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

Asked

Viewed 684 times

3

I isolated a problem from a larger script that I am producing. I basically 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 the execution of this function.


UPDATING

[28/12/2015]

Importantly, the variable should be interpolated into a string. In this case, every time we print obter_data the departure shall 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 when the script source was loaded and not the current time.


To illustrate, we print the variable 3x with intervals of 1 second.

The desired output is as follows below:

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

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

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

However, the example code below produces the following output:

[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}

4 answers

2

Its function data_hora_atual is returned a echo and not a command itself, thus the two variables $obter_data and $data_hora was guarding the exit of the echo and not the command date.

Well, I doctored 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
  • Dener, I need the variable to be interpolated inside a string. I didn’t leave it specified in the question, I will update it.

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

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}

0

To do what you want, simply place the contents of the string you want to be executed inside severe 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}`

The severe accents will perform the instruction contained inside and return the output which in case is the return of date. It cannot be reversed as in your example, because obter_data=$(data_hora_atual) will cause date receive a value that does not change unless it is executed again, which does not happen in the example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.