LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH If-then-else Scripting Help (https://www.linuxquestions.org/questions/programming-9/bash-if-then-else-scripting-help-248345/)

xianzai 10-28-2004 08:33 AM

BASH If-then-else Scripting Help
 
I know this is a really stupid question, but can anybody tell me what is wrong with this?? I haven't got a clue!! It seems like its correct! But it errors....

Code:

#!/bin/bash
  NAME_STRING="Matthew Jones"
    # String contains no wildcards - formulate search string.

    forename=""
    surname=""
    nextchar="0"
   
    for i in `seq 1 ${#NAME_STRING}`;
    do
    {
        if[ "${$NAME_STRING:$i-1:1}" != " " ]; then
        {
            if[ $nextchar = "0" ]; then
            {
                    $forename=${$forename+${NAME_STRING:$i-1:1}}
            }
            else
            {
                    $surname=${$surname+${NAME_STRING:$i-1:1}}
              }
            fi
        }
        else
        {
            $nextchar="1";
        }
        fi
    }
    done

    echo "Forename = $forename\n";
    echo "Surname  = $surname\n";

All i get when i run it is:

Code:

./test: line 13: syntax error near unexpected token `then'
./test: line 13: `      if[ "${$NAME_STRING:$i-1:1}" != " " ];  then'
[root@172 bin]#

Thanks!!!

Philipp Wesche 10-28-2004 08:47 AM

Re: BASH If-then-else Scripting Help
 
Quote:

Originally posted by xianzai

Code:

./test: line 13: syntax error near unexpected token `else'
./test: line 13: `      if[ "${$NAME_STRING:$i-1:1}" != " " ]; else'
[root@172 bin]#

Thanks!!!

Try a space between if and the [

Philipp

Edit: okay, that doesn't work... try putting else on the next line?

xianzai 10-28-2004 08:56 AM

IT WAS A SPACE BETWEEN THE IF AND THE [

THANKS!!

jlliagre 10-28-2004 09:56 AM

There are too much dollars in your code !
Remove those on the left side of assignments (e.g. $nextchar="1"; should be nextchar=1) .

jschiwal 10-29-2004 04:09 AM

Two of the answers came up while I was preparing my response.

Why not use
Forename=${NAME_STRING% *}
Sirame=$[NAME_STRING#* }

Also, for ${var:n:m} if the offset is negative, you may need to write it
${var:$(( n )):m}


All times are GMT -5. The time now is 05:54 PM.