Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    89

    Question Unanswered: variable not inserted as filename

    Hi folks.

    I ran into a problem with trying to create a filename in a ksh script (on SunOS 5.9), which has a variable at the beginning.

    Like this:
    Code:
    myvar=Hoppla
    cp original_file.log $myvar_copy.log
    The resulting filename is then:
    _copy.log
    However, this works:
    Code:
    myvar=Hoppla
    cp original_file.log copy_$myvar.log
    Resulting filename now:
    copy_Hoppla.log
    I noticed the same issue also when redirecting output to a file this way:
    Code:
    myvar=Hoppla
    echo "Test" > $myvar_testfile.log
    Result:
    _testfile.log
    What's the deal with the position of the parameter?
    "My brain is just no good at being a relational Database - my relations suck real bad!"

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Code:
    myvar=Hoppla
    cp original_file.log $myvar_copy.log
    The shell is looking for a variable called myvar_copy and can't find it so you're getting wierd results. I suggest you use {} around your variables to make things clearer ie
    Code:
    myvar=Hoppla
    cp original_file.log ${myvar}_copy.log

  3. #3
    Join Date
    Mar 2008
    Posts
    89
    Ah, of course.... That's just me being a noob, like always.
    Thanks for feeding the noob, mike.
    "My brain is just no good at being a relational Database - my relations suck real bad!"

Posting Permissions

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