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

455
Visualizações
shell script - how to use variables for a shell script without defining the variable in the script

Raspbian (Jessy) - root@Raspberry Pi - Putty

In the Terminal i type in

finalanswer=0

now i got a script with this code

#!/bin/bash
source /lib/lsb/init-functions

echo $finalanswer              #just as a test
if [ ! "$finalanswer" = "0" ]
then
        rm -r mnt/objects/all
        log_warning_msg "All Files has been deleted" || true
        touch its_over.txt
else
        let finalanswer=1
        log_action_msg "Var finalanswer was 0. setting back to 1" || true
fi

there is a cronjob that starts this script every hour

sooo. somewhere there must be an error. because he is reading the Variable $finalanswer as nothing.

that means variables that has been defined outside of this script will not work?

how do i fix this?

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

0

Shell variables are not inherited by child processes. If you want a variable to be inherited, it has to be an environment variable. You create environment variables using the export command.

export finalanswer=0

or

finalanswer=0
export finalanswer

You can also export a variable just for the duration of a command by putting the assignment at the beginning of the command:

finalanswer=0 /path/to/script

Note that variables you assign in your shell will not be accessible to cron jobs. Variables can only be exported to processes that are descended from the shell, and processes run by cron are not related to your shell process. If you want to set a variable for use in a cron job, you can put the assignment into the crontab file itself.

over 4 years ago · Santiago Trujillo Relatório

0

You can define a variable for a single command by placing its definition before the command you wish to run:

$ VARIABLE=hunter perl -E 'say $ENV{VARIABLE}'
hunter

you can do the same thing for a cron entry:

*/10 * * * * VARIABLE=hunter <command>
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