Results 1 to 9 of 9

Thread: read file's content into variable

  1. #1
    Join Date
    Apr 2007
    Beans
    34

    Question read file's content into variable

    I try to put the content of a file into a variable by doing:

    Code:
    $value=`cat sources.xml`
    but I get this error:

    test.sh: line 9: =[<?xml: command not found


    Here is my sources.xml file:

    Code:
    [<?xml version="1.0"?>
    <group.....
    .....
    />
    ]

  2. #2
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: read file's content into variable

    Don't put the dollar sign ($) in front of the variable name in an assignment.
    Try
    Code:
    value=`cat sources.xml`

  3. #3
    Join Date
    Apr 2007
    Beans
    34

    Re: read file's content into variable

    Quote Originally Posted by WW View Post
    Don't put the dollar sign ($) in front of the variable name in an assignment.
    Try
    Code:
    value=`cat sources.xml`
    Then I have this error:

    Error: Parse error: Didn't understand `[<?xml' (list must end with a ']')


  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: read file's content into variable

    show your whole code. what shell are you using.

  5. #5
    Join Date
    Apr 2007
    Beans
    34

    Re: read file's content into variable

    I use bash, and this line is the first one of my script. I want to retrieve the whole content of the file in a variable ; maybe there is another way to do...

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: read file's content into variable

    yes of course there are other ways to do it. What are trying to do actually? where is your code?

  7. #7
    Join Date
    Apr 2007
    Beans
    34

    Re: read file's content into variable

    Quote Originally Posted by ghostdog74 View Post
    yes of course there are other ways to do it. What are trying to do actually? where is your code?
    So, my script tries to update a gconf value:

    Code:
    # get the value
    gconftool-2 --get /apps/evolution/calendar/sources > sources.xml
    
    # update it
    sed  "s|\(name=\"On This Computer\".*\)</group>|\1<source uid=\"my.calendar\" name=\"my calendar\" relative_uri=\"mycal\" color_spec=\"#095401\"/></group>|" $SOURCES  > /dev/null
    
    # read the file
    NewValue=`cat sources.xml`
    
    # set the value
    gconftool-2 --type list --list-type string --set /apps/evolution/calendar/sources $NewValue

  8. #8
    Join Date
    Sep 2006
    Beans
    2,914

    Re: read file's content into variable

    and you said you put it on the very first line ?? remove the other commands first, and make sure you really have sources.xml in your current directory and run this script.
    Code:
    #!/bin/bash
    value=`cat sources.xml`
    echo $value
    if you are able to run this, then the problem lies with the other commands...where is your $SOURCES variable defined in you sed statement ?

  9. #9
    Join Date
    Apr 2007
    Beans
    34

    Re: read file's content into variable

    and you said you put it on the very first line ??
    Sorry I was a doing a test in which I put it on the first line, but actually I found the error comes from the line after (the set value) ; I need to put the variable between quotes:

    Code:
    gconftool-2 --type list --list-type string --set /apps/evolution/calendar/sources "$NewValue"
    Thanks anyway!

Bookmarks

Posting Permissions

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