0

We have problem defining variables between two shell scripts, basically what we want to do is run ./get_variables.sh, and fill in the details for "Enter the domain: " and "Enter the vestacp password:" then get the details as variables in vestacp.sh

get_variables.sh content

#!/bin/sh
echo "Enter the domain: "
read $domain
echo "Enter the vestacp password:"
read $rvp


/root/vestacp.sh $domain $rvp

and

vestacp.sh content

sed -i s/VAR1/$domain/g config.php
sed -i s/VAR2/$rvp/g config.php

config.php file content

VAR1
VAR2

VAR1 and VAR2 should be replaced by the data that we enter when running ./get_variables.sh using sed on vestacp.sh

The problem that we are getting just an empty config.php after running ./get_variables.sh

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
  • Yes, and? What did you try, what did you expect to happen, what did actually happen? (Hint: you probably want to *source* `./get_variables.sh` with `. ./get_variables.sh`, not *run* it.) – AlexP Nov 21 '18 at 23:29
  • 2
    why are you doing `read $domain` and not `read domain` ? – thrig Nov 21 '18 at 23:41
  • 1
    https://www.shellcheck.net will help with the typical shell errors – glenn jackman Nov 21 '18 at 23:49
  • Relating: https://unix.stackexchange.com/questions/129059/how-to-ensure-that-string-interpolated-into-sed-substitution-escapes-all-metac – Jeff Schaller Nov 22 '18 at 00:31
  • Also possibly related: [Why does my shell script choke on whitespace or other special characters?](//unix.stackexchange.com/q/131766) – Kusalananda Nov 22 '18 at 07:34

1 Answers1

-1

I see two errors:

  • read variable should not have a $
  • variables are not passed by name. The 2nd script is trying to read a variable by the name it was called in the 1st script. It can not know what it was called in first script.

Also break your problems down. There is no way that anyone can write a big program, and then run it, and it work. It this case test each script separately. Get then to work, on their own, then get them to work together.

Also use shellcheck to test you scripts. It will find some of the errors. You will still have to test it. sudo apt-get install shellcheck to install on Debian.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102