Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

362
Visualizações
Running commands in a loop without spawning a new subshell each time

I have a bash script that reads in lots of dates in epoch time, and determines the (local) hour of the day at which they occurred. Relevant snippet:

while read ts
do
  hour="$(date -d@$((${ts} / 1000)) +%H)"
  ((hourly_counts["${hour}"]+=1))
done < "${jqfile}"

It's rather slow, because it spawns a new subshell for each invocation of date. Is there a sensible way round this? It looks as though date doesn't support multiple queries.

All I can think of is to spawn a new shell, and pipe date commands to it and get the results back, so that they all execute in the same shell. But I'm not clear on how to do this, and it seems a little over-engineered.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use printf:

printf -v hour '%(%H)T' "$(( ts / 1000 ))"

Type help printf to know more about the command.

Also check strftime(3). Behavior may depend on the value of TZ and LC_TIME.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda