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, 09: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 09:54 AM.
 
Old 10-28-2004, 09: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 09:48 AM.
 
Old 10-28-2004, 09: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, 10:56 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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, 05:09 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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 05: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 01:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 02:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 05:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 01:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 02:10 AM


All times are GMT -5. The time now is 08:07 AM.

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