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

244
Visualizações
Is it possible to return the result of javascript method while executing that in shell script?

I am new to javascript. I have a js file something like below(test.js):

async function getData() {
   let a =100;
   // Some other function calls which modifies a
   // Some more console logs here.
   return a;
}
(async () => {
  console.log(await getData()); // When trying to return await getData() giving some error.
})();

I am executing the above script output = $(node test.js) in shell script and I need the value of a in the shell script but here I am getting all the console.log statements. Is it possible to get the value?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you know that output will always have one line (by output I mean returned value from getData()), you can use $(node test.js | tail -n 1) to grab only one last line from node test.js. Otherwise you can do this little hacky workaround by replacing console.log function:

async function getData() {
   let a = 100;
   console.log("Some cosole log");
   console.log("Some other log");
   return a;
}
(async () => {
        console_log = console.log; // Save original function for later
        console.log = ()=>{}; // Replace original with "dummy" function
        console_log(await getData()); // Use saved original to log only what we want
})();

This will work unless some part of code use process.stdout.write to log/print data directly. To avoid this, we can again replace process.stdout.write with some dummy function but since this is likely to break some other things, I would avoid it.

about 4 years ago · Juan Pablo Isaza 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