Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2003
    Location
    San Mateo, Rizal, Philippines
    Posts
    18

    Question Unanswered: Simple Unix Scripts

    Hi Gurus,

    I am creating a simple shell scripts with the use of tilde or `

    It seems that something wrong with my commands or configurations.
    Please help me on this. I am figuring this out for at least a week now.
    I still can't understand why this script produces an error.
    Please see below.

    filename: sample.sh and it contains the following lines
    #!/bin/ksh

    a= `cat mydate.txt`
    echo $a

    ----
    When I run the sample.sh at the command line, it produces an error :
    sample.sh[3]: 20040714: not found.


    mydate.txt contains only 20040714.
    I was trying to transfer the value of the text file to a variable and I want it to display at the screen. It seems that tilde was not recognize.... but other scripts uses tilde and it works fine. Can you give me an idea why this simple unix script does not work using tilde...

    Any help will do..

    Me-an

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    It's a space that's causing your problem.

    A variable assignation followed by a command sets that variable ONLY within the scope of that command.

    e.g.

    var=gubbins aScript; echo $var

    The above would set var to "gubbins" within the context of aScript. The subsequent echoing of $var would output an empty string as var would remain unset in the current shell.

    What you are trying to do is set 'a' to an empty string within the context of the command derived from the output of mydate.txt.

    i.e.

    a= `cat mydate.txt`

    is the same as...

    a= 20040714

    ...and seen as there is no such command as 20040714 you get the error.

    In short, what you really mean is...

    a=`cat mydate.txt`

    Incidentally, seen as you are only talking about a single line, you could also write this as...

    read a < mydate.txt

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    ...and ` is a backtick not a tilde ~

    :-)

  4. #4
    Join Date
    Dec 2003
    Location
    San Mateo, Rizal, Philippines
    Posts
    18

    Thumbs up Thanks.

    Thanks Damian for the very fast response.

    And I also tried removing the space and it works!

    Thank you once again..

    me-an from Philippines

Posting Permissions

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