LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-25-2013, 02:26 PM   #1
Curiosity42
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.

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

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

while read line
  do
    line=$(echo $line | sed s/^\ *//)
    if [[ $line =~ ^\\[menu\\] ]]
    then
      line=$(echo "$line" | sed s/\\[menu\\]\ *//)
      PXE_ENTRIES[$IN]=$line
      let IN++
    elif [[ $line =~ ^\\[data\\] ]]
    then
      line=$(echo "$line" | sed s/\\[data\\]\ *//)
      DATA[$DI]=$line
      let DI++
    fi
  done < $PXE_CFG_DATA_FILE
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,314

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
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,024

Rep: Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136Reputation: 2136
To add to the above, you also do not need to escape the space.
 
Old 01-26-2013, 12:40 AM   #4
konsolebox
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:
Code:
while read LINE; do
    case "$LINE" in
    '[menu]'*)
        while read LINE; do
            : do something
        done
        ;;
    '[data]'*)
        while read LINE; do
            : do something
        done
        ;;
    *)
        # invalid line?
        ;;
    esac
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: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953Reputation: 1953
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:

Code:
shopt -s extglob
line=${line##*([[:space:]])}
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.

Code:
re='[[:space:]]*[[]menu[]]'
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
 
  


Reply


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 07:16 PM.

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