Find the answer to your Linux question:
Results 1 to 7 of 7
Ok i'll get straight to it. I have 2 bash scripts. From the first one I have this: Code: WHATSYOURDOMAIN="test.org" /root/SCRIPTS/add-virtual-domain And the variable $WHATSYOURDOMAIN needs to be carried over ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    14

    Any Bash scripters able to help with a problem?


    Ok i'll get straight to it. I have 2 bash scripts. From the first one I have this:

    Code:
    WHATSYOURDOMAIN="test.org"
    /root/SCRIPTS/add-virtual-domain
    And the variable $WHATSYOURDOMAIN needs to be carried over (dont know the technical term sorry) to the below script I have (add-virtual-domain):

    Code:
    echo '"' > /root/temp
    QUOTES=$(cat /root/temp)
    echo " " >> /root/thisisatest
    echo "zone $QUOTES$WHATSYOURDOMAIN$QUOTES' {" >> /root/thisisatest
    echo "		type master;" >> /root/thisisatest
    echo "		file $QUOTES/var/named/$WHATSYOURDOMAIN.hosts$QUOTES;" >> /root/thisisatest
    echo "	};" >> /root/thisisatest
    touch /root/$WHATSYOURDOMAIN.hosts
    echo '$ttl 38400' >> /root/$WHATSYOURDOMAIN.hosts
    echo "$WHATSYOURDOMAIN.	IN	SOA	test.my-site.co.uk. test.my-site.co.uk. (" >> /root/$WHATSYOURDOMAIN.hosts
    echo "				$RANDOM" >> /root/$WHATSYOURDOMAIN.hosts
    echo "			10800" >> /root/$WHATSYOURDOMAIN.hosts
    echo "			3600" >> /root/$WHATSYOURDOMAIN.hosts
    echo "			604800" >> /root/$WHATSYOURDOMAIN.hosts
    echo "			38400 )" >> /root/$WHATSYOURDOMAIN.hosts
    echo "$WHATSYOURDOMAIN.	IN	NS	ns.my-site.co.uk." >> /root/$WHATSYOURDOMAIN.hosts
    echo "$WHATSYOURDOMAIN.	IN	A	65.43.81.211" >> /root/$WHATSYOURDOMAIN.hosts
    echo "www.$WHATSYOURDOMAIN.	IN	CNAME	$WHATSYOURDOMAIN." >> /root/$WHATSYOURDOMAIN.hosts
    echo "ftp.$WHATSYOURDOMAIN.	IN	CNAME	$WHATSYOURDOMAIN." >> /root/$WHATSYOURDOMAIN.hosts
    echo "smtp.$WHATSYOURDOMAIN.	IN	CNAME	$WHATSYOURDOMAIN." >> /root/$WHATSYOURDOMAIN.hosts
    echo "pop.$WHATSYOURDOMAIN.	IN	CNAME	$WHATSYOURDOMAIN." >> /root/$WHATSYOURDOMAIN.hosts
    This second script all works fine and creates the entry as I want it in my test document for now.. but the variable just isn't read. Any ideas?

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    After you set the variable you should write export WHATSYOURDOMAIN

  3. #3
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    Posts
    796
    Exporting the variable won't work in all cases. What you could do is call the second script with $WHATSYOURDOMAIN as a parameter to it:

    Code:
    WHATSYOURDOMAIN="test.org" 
    /root/SCRIPTS/add-virtual-domain $WHATSYOURDOMAIN
    Then in your second script, the first argument to the command (in this case $WHATSYOURDOMAIN) can be accessed with $1:

    Code:
    WHATSYOURDOMAIN=$1
    echo '"' > /root/temp
    QUOTES=$(cat /root/temp)
    echo " " >> /root/thisisatest echo "zone $QUOTES$WHATSYOURDOMAIN$QUOTES' {" >> /root/thisisatest 
    ...

  4. #4
    Just Joined!
    Join Date
    May 2005
    Posts
    18

    A variable from one program to another

    If all the rest fails, you can write the value of your variable
    in a file with the first program and read this value from the
    file with the other program.

    It's slow, bad programming practice, but it works. Even in
    Prolog, where it is a way to go round the "no global variable"
    rule.

    Mariane

  5. #5
    Just Joined!
    Join Date
    Apr 2005
    Posts
    14
    Ok I got it working with the below code (which will be repeated on seperate lines lots of times):
    Code:
    WHATSYOURDOMAIN="testing1.co.uk";export WHATSYOURDOMAIN;"/root/SCRIPTS/add-virtual-domain";
    Funnily though (in a pull your hair out kind of way).. this line brings up a command not found error when ran... Although it functions 100% correctly.

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    My guess is that if you remove the quotes from around "/root/SCRIPTS/add-virtual-domain"
    you won't be getting the error message.

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Your use of /root/temp is wasteful. Instead,
    Code:
    QUOTES='"'
    will do the trick. Incidentally, if you're set on reading files into shell variables,
    Code:
    VAR=$&#40;< /some/file&#41;
    is much more cunning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •