Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2008
    Posts
    7

    Unanswered: Error in Shell script

    Hi,
    I want to write a shell script in that i am sending a file name as argument.
    I want to check whether the file contain "#student layout" . if so i have to skip next 3 lines and print the line after that.
    FILE="$1"
    key=" "
    i=0
    exec 3<&0
    exec 0<$FILE
    while read line
    do
    echo $line
    if [ $line == "#student layout" ]
    then
    for i in 1 2 3
    do
    read line
    done
    fi
    done
    exec 0<&3

    exit 0

    Can anyone tell me is there any syntaxt error in this. I am getting an error "syntax error near unexpected token 'do in line no 12.
    I am working on Solaris

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Syntax seem OK
    your problem is that you do not quote your variable resulting in something funny on your if statement
    change to
    if [ "$line" ...etc.

  3. #3
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Talking grep -N

    This will work on some "Unix" flavors:

    Code:
    grep -N 4 "#student layout" $FILE|tail -1
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    And just to prove there's loads a ways to skin a cat:

    Program:
    Code:
    #!/bin/sh
    
    echo -n "File:"
    read file
    
    cat $file | sed -n '/#student layout/{n;n;n;n;p;}'
    
    exit 0
    test data:
    Code:
    # cat tmp.dat
    line 1
    #student layout
    line 3
    line 4
    line 5
    line 6
    
    #student layout
    line 9
    line 10
    line 11
    line 12
    line 13
    #student layout
    Output:
    Code:
    # ./tmp
    File:tmp.dat
    line 6
    line 12

Posting Permissions

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