LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-28-2004, 08:33 AM   #1
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Rep: Reputation: 15
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!!!

Last edited by xianzai; 10-28-2004 at 08:54 AM.
 
Old 10-28-2004, 08:47 AM   #2
Philipp Wesche
LQ Newbie
 
Registered: Oct 2004
Location: Edinburgh, Scotland, UK
Distribution: Fedora Core 2
Posts: 1

Rep: Reputation: 0
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?

Last edited by Philipp Wesche; 10-28-2004 at 08:48 AM.
 
Old 10-28-2004, 08:56 AM   #3
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Original Poster
Rep: Reputation: 15
IT WAS A SPACE BETWEEN THE IF AND THE [

THANKS!!
 
Old 10-28-2004, 09:56 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
There are too much dollars in your code !
Remove those on the left side of assignments (e.g. $nextchar="1"; should be nextchar=1) .
 
Old 10-29-2004, 04:09 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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}

Last edited by jschiwal; 10-29-2004 at 04:11 AM.
 
  


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 scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM


All times are GMT -5. The time now is 03:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration