Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Read line containing $ in bash.

  1. #1
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    175

    Default Read line containing $ in bash.

    Hello, and good evening.

    I try to manipulate some live GPS-data. But the data starts with $
    Code:
    gpspipe -r
    $GPGGA,175546.835 ...
    $GPGSA,A, ...
    $GPGSV,3, ...
    $GPGSV,3, ...
    $GPGSV,3, ...
    $GPRMC,175546.835,A, ...
    $GPVTG, ... 
    If the data is read into variables the first field (first field as in before firts comma (,)) is removed.
    Code:
    user $ line="$GPGGA,165706.836,1,2,3,4"
    user $ echo $line
    ,165706.836,1,2,3,4
    How can the data containing $ be put into variables?
    /J
    ASUS G46VW running openSUSE LEAP 42.1, Windows 8
    Samsung NP300V3A running openSUSE LEAP 42.1 Windows 7
    Some EEEPC running openSUSE LEAP 42.1, "Server" running sshfs, (mini)DLNA, NFS
    Raspberry Pi, Weezy, openSUSE 13.1 (two different SD-cards)

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,588

    Default Re: Read line containing $ in bash.

    Do not use "...", but '...'
    Henk van Velden

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,588

    Default Re: Read line containing $ in bash.

    This was maybe a bit a short answer.
    Code:
    henk@boven:~> line='$GPGGA,165706.836,1,2,3,4'
    henk@boven:~> echo $line
    $GPGGA,165706.836,1,2,3,4
    henk@boven:~>
    This is btw about assignment of a value to a variable, not about reading (as your title says).

    And you could have found this yourself by studying the man page of bash. I know, it is not alwyas easy to find the appropriate parts. But IMHO the part about Quoting is rather easy to detecft:
    QUOTING

    Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

    Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it is to represent itself.

    When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually !, must be quoted to prevent history expansion.

    There are three quoting mechanisms: the escape character, single quotes, and double quotes.

    A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>. If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

    Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

    Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `,  " , \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

    The special parameters * and @ have special meaning when in double quotes (see PARAMETERS below).

    Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
    \a
    alert (bell)
    \b
    backspace
    \e
    \E
    an escape character
    \f
    form feed
    \n
    new line
    \r
    carriage return
    \t
    horizontal tab
    \v
    vertical tab
    \\
    backslash
    \'
    single quote
    \"
    double quote
    \nnn
    the eight-bit character whose value is the octal value nnn (one to three digits)
    \xHH
    the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
    \uHHHH
    the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)
    \UHHHHHHHH
    the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)
    \cx
    a control-x character


    The expanded result is single-quoted, as if the dollar sign had not been present.

    A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.
    Henk van Velden

  4. #4
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    175

    Default Re: Read line containing $ in bash.

    Quote Originally Posted by hcvv View Post
    And you could have found this yourself by studying the man page of bash.
    [Pulling my hair ... to try to keep awake] There are som much information, fell asleep all the time. I'm probably a baby that can't handle that amoutn of information without a nap.
    Thanks for pulling out the good stuff!
    ASUS G46VW running openSUSE LEAP 42.1, Windows 8
    Samsung NP300V3A running openSUSE LEAP 42.1 Windows 7
    Some EEEPC running openSUSE LEAP 42.1, "Server" running sshfs, (mini)DLNA, NFS
    Raspberry Pi, Weezy, openSUSE 13.1 (two different SD-cards)

  5. #5
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,665

    Default Re: Read line containing $ in bash.

    A Linux (and UNIX®) shell tutorial needs at least 4 sessions, each of about 3 hours duration, with homework exercises to be done between the sessions.

  6. #6
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    20,197
    Blog Entries
    14

    Default Re: Read line containing $ in bash.

    Quote Originally Posted by dcurtisfra View Post
    A Linux (and UNIX®) shell tutorial needs at least 4 sessions, each of about 3 hours duration, with homework exercises to be done between the sessions.
    So wrong :
    A Linux shell tutorial needs at least 4 sessions, each of about 3 hours duration, with homework exercises to be done between the sessions.
    A UNIX® shell tutorial needs at least 4 sessions each of about $3499, with homework exercises to be done after work hours.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  7. #7
    Join Date
    Jun 2008
    Location
    Auckland, NZ
    Posts
    20,769
    Blog Entries
    1

    Default Re: Read line containing $ in bash.

    Quote Originally Posted by Knurpht View Post
    So wrong :
    A Linux shell tutorial needs at least 4 sessions, each of about 3 hours duration, with homework exercises to be done between the sessions.
    A UNIX® shell tutorial needs at least 4 sessions each of about $3499, with homework exercises to be done after work hours.
    That's a lot of beer and drinking time

  8. #8
    Join Date
    Nov 2013
    Location
    Kamloops, BC, Canada
    Posts
    3,974

    Default Re: Read line containing $ in bash.

    Quote Originally Posted by Knurpht View Post
    So wrong :
    A Linux shell tutorial needs at least 4 sessions, each of about 3 hours duration, with homework exercises to be done between the sessions.
    A UNIX® shell tutorial needs at least 4 sessions each of about $3499, with homework exercises to be done after work hours.
    You nailed it, Knurpht!
    -Gerry Makaro
    Fraser-Bell Info Tech
    Solving Tech Mysteries since the Olden Days!
    ~~
    If I helped you, consider clicking the Star at the bottom left of my post.

  9. #9
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    2,665

    Default Re: Read line containing $ in bash.

    Everything should be as simple as possible but, no simpler than that!!
    OK, the original was spoken in German by a physicist and, it's a rough translation of what he possibly said. There's also another version, also in German; a rough translation is:
    One must make things as simple as possible. But, not (absolutely) simple.

  10. #10
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    175

    Default Re: Read line containing $ in bash.

    Quote Originally Posted by deano_ferrari View Post
    That's a lot of beer and drinking time
    Ohhhh, is that the way to keep awake... Thanks for suggestions!
    ASUS G46VW running openSUSE LEAP 42.1, Windows 8
    Samsung NP300V3A running openSUSE LEAP 42.1 Windows 7
    Some EEEPC running openSUSE LEAP 42.1, "Server" running sshfs, (mini)DLNA, NFS
    Raspberry Pi, Weezy, openSUSE 13.1 (two different SD-cards)

Page 1 of 2 12 LastLast

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
  •