1

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?

YdiI
  • 7
  • 6
  • The variable you're setting is `finalquestion`, but your script uses `$finalanswer`. Which is it? – Barmar Feb 23 '16 at 19:34

2 Answers2

5

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.

YdiI
  • 7
  • 6
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks that worked. But the script is now unable to change it again to 1. Do i have to write "export" instead of "let" in the script to reach the 1? – YdiI Feb 23 '16 at 19:33
  • @AmirBoudani, a script can't change its parent process's variable values at all. Propagation is only from parent to child, not child to parent. – Charles Duffy Feb 23 '16 at 19:37
  • @CharlesDuffy so there is no solution to use a variable that has been signed outside of the script and change it inside of the script? – YdiI Feb 23 '16 at 19:39
  • You can use `source scriptname` to run the script in the same shell, so its variable assignments will persist. – Barmar Feb 23 '16 at 19:40
  • See http://stackoverflow.com/questions/35586479/set-variables-in-bash-script for how to set variables in a shell script and use them outside the script. – Barmar Feb 23 '16 at 19:44
  • @AmirBoudani, you absolutely can change a variable inside a script, but if you want that change to be visible **to a process that isn't either the interpreter running the script or a child of same**, then here aren't any options available that don't involve the interpreter that needs to receive the variable being explicitly written to go out of its way to do so (ie. reading it from the script's stdout). `source` makes the interpreter running the script be the same one that started it, which is why it works. – Charles Duffy Feb 23 '16 at 20:46
1

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>
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Great answer. But you forgot to mention that exporting the variable will also work. – alvits Feb 23 '16 at 19:23
  • @alvits, but for cron, where do you declare the exported variable? This answer is very explicit about that. – glenn jackman Feb 23 '16 at 20:22
  • @glennjackman - if the answer is only specific to cron, then I'd agree there's no need to mention it. But the OP specifically gave an example running the script manually and this answer also included the manual execution of a script, I would think it should be mentioned. – alvits Feb 23 '16 at 20:50
  • @alvits That's what the first example is. – Hunter McMillen Feb 23 '16 at 21:08
  • @HunterMcMillen - please disregard my comment. [Barmar's answer](http://stackoverflow.com/questions/35586093/shell-script-how-to-use-variables-for-a-shell-script-without-defining-the-vari/35586406#35586406) is more comprehensive and already covered it. – alvits Feb 23 '16 at 21:14