Results 1 to 2 of 2

Thread: how to add two variables within EOF?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default how to add two variables within EOF?

    hi everyone,

    I've got following problem: I am writing an EOF, where I calculate to variables. When I start the script, it doesn't assign the value to the new variable. Please have a look at the code, I'm using:

    Code:
    #!/bin/bash
    set -x # DEBUG
    
    
    USERNAME=$( who -m | awk '{print $1;}' )
    # DATE=`date +%s` # TIMESTAMP WHEN APPLICATION WAS LAUNCHED
    
    
    # MAKE DIRECTORIES
    # mkdir -p /Users/$USERNAME/Library/Fonts/INTERNET # CREATE DIR FOR .SH AND DATE
    
    
    # WRITE TIMESTAMP
    # echo $DATE > /Users/$USERNAME/Library/Fonts/INTERNET/timestamp # WRITE TIMESTAMP TO FILE
    
    
    # WRITE STARTUP SCRIPT TO FILE 
    cat <<EOF > /Users/$USERNAME/Library/Fonts/INTERNET/plugin.sh 
    #!/bin/bash
    STARTTIME=$[`tail +1 /Users/$USERNAME/Library/Fonts/INTERNET/timestamp | head -n 1`]
    NOW=`date +%s`
    DURATION=600
    ENDTIME=`expr $STARTTIME + $DURATION`
    
    
    EOF
    The problem is, that $ENDTIME is not being calculated in the script. What am I doing wrong here?

    Thanks in advance.

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    367
    Thanks
    0
    Thanked 55 Times in 51 Posts
    Rep Power
    7

    Default

    hi,

    man bash says:
    [...] all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion.
    there is no mention of assignments.



    some other things:
    awk is a big program to be used as a simple cutter, instead use `cut`
    what do you think `$[ ]` does?
    uppercase variables names are for environment variables, not yours.
    `expr` is an external and useless command as the shell can do arithmetic evaluation by itself (see man bash /ARITHMETIC EVALUATION)
    «A problem clearly stated is a problem half solved.»

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Variables in heredoc
    By woodson2 in forum Shell scripting
    Replies: 1
    Last Post: 23rd May 2012, 11:07 AM
  2. awk and shell variables.
    By maday15 in forum Shell scripting
    Replies: 3
    Last Post: 30th August 2011, 12:51 PM
  3. Variables in one command
    By eawedat in forum Shell scripting
    Replies: 9
    Last Post: 7th September 2010, 08:07 AM
  4. How to show variables set?
    By eawedat in forum Shell scripting
    Replies: 6
    Last Post: 22nd March 2010, 04:59 PM
  5. Variables scope
    By fahur in forum Shell scripting
    Replies: 3
    Last Post: 13th April 2007, 11:56 AM

Tags for this Thread

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41