Visit the LQ Articles and Editorials section
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-25-2013, 02:26 PM   #1
LQ Newbie
Registered: Jan 2013
Location: Earth
Posts: 20

Rep: Reputation: 0
Bash problem

I have started a small bash script and I want to analyze a file. The file contains lines that start with the keywords [menu] or [data]. In the following script none of the keywords are recognized.

#Example lines:
[menu] non-RT ...........................

[data] CFG ............................
local DI=0
local IN=0

while read line
    line=$(echo $line | sed s/^\ *//)
    if [[ $line =~ ^\\[menu\\] ]]
      line=$(echo "$line" | sed s/\\[menu\\]\ *//)
      let IN++
    elif [[ $line =~ ^\\[data\\] ]]
      line=$(echo "$line" | sed s/\\[data\\]\ *//)
      let DI++
What have I done wrong in the if and elif statement?

Last edited by Curiosity42; 01-25-2013 at 03:15 PM. Reason: Formatting of script
Old 01-25-2013, 03:30 PM   #2
Ser Olmy
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 2,317

Rep: Reputation: Disabled
Change the double backslashes to single backslashes. As they stand, your expressions match "\menu\" and "\data\" at the start of a line.

Edit: Actually, the current expressions match "\m" and "\d" followed by characters from the sets "enu\" and "ata\" respectively.

Last edited by Ser Olmy; 01-25-2013 at 03:38 PM.
Old 01-25-2013, 11:58 PM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,111

Rep: Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185
To add to the above, you also do not need to escape the space.
Old 01-26-2013, 12:40 AM   #4
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I suggest using case statement instead:
while read LINE; do
    case "$LINE" in
        while read LINE; do
            : do something
        while read LINE; do
            : do something
        # invalid line?
done < "$YOURFILE"

Last edited by konsolebox; 01-26-2013 at 12:41 AM.
Old 01-27-2013, 02:37 PM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
What konsolebox said.

In addition, you usually don't need sed for this kind of thing, when you have lots of powerful string manipulation features already built into the shell.

To remove all whitespace at the start of the variable string, for example:

shopt -s extglob
And when using the shell's built-in regex ability, store the expression in a variable first (using hard quotes), then use that inside the [[..]] brackets, unquoted. This will make it easier to compose them without having to fiddle with all the backslashes.

if [[ $line =~ $re ]]; then
And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Last edited by David the H.; 01-27-2013 at 02:42 PM. Reason: added another point


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
[SOLVED] Bash problem Ashkan_s Programming 4 01-16-2011 06:15 AM
[SOLVED] bash problem struggled_newbie Linux - Newbie 5 09-07-2010 03:59 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM

All times are GMT -5. The time now is 12:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration