Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   CentOS (http://www.linux-archive.org/centos/)
-   -   Syntax Help on a Bash Script (http://www.linux-archive.org/centos/521891-syntax-help-bash-script.html)

Robert 05-04-2011 04:33 AM

Syntax Help on a Bash Script
 
Hi All,

I'm brand new at doing anything linux and would like feedback on this
script I'm trying to understand from an example I'm working on..

Oh, running Centos 5.6

Anyhow, I run this bash script:

#!/bin/bash
# send data to the table in the MySQL database

MYSQL='which mysql'

if [ $# -ne 4 ]
then
echo "Usage: mtest4 empid lastname firstname salary"
else
statement="insert into employees values ($1, '$2','$3', $4)"
$MYSQL test << EOF
$statement
EOF
If [ $? -eq 0 ]
then
echo "Data successfully added"
else
echo "Problem adding data"
fi
fi


and here is the error I get:

[Bobster@localhost ~]$ ./mtest4 5 Johnson John 120000
./mtest4: line 15: syntax error near unexpected token `then'
./mtest4: line 15: ` then'

Thanks in advance for any inputs.

Bobster



_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Steven Crothers 05-04-2011 04:36 AM

Syntax Help on a Bash Script
 
Possibly an invisible character. Delete the entire line with "then"
and re-type it. That error seems odd.


On Wed, May 4, 2011 at 12:33 AM, Robert <bobster58@gmail.com> wrote:
> Hi All,
>
> I'm brand new at doing anything linux and would like feedback on this
> script I'm trying to understand from an example I'm working on..
>
> Oh, running Centos 5.6
>
> Anyhow, I run this bash script:
>
> #!/bin/bash
> # send data to the table in the MySQL database
>
> MYSQL='which mysql'
>
> if [ $# -ne 4 ]
> then
> * * *echo "Usage: mtest4 empid lastname firstname salary"
> else
> * * *statement="insert into employees values ($1, '$2','$3', $4)"
> * * *$MYSQL test << EOF
> * * *$statement
> EOF
> * * *If [ $? -eq 0 ]
> * * *then
> * * * * * echo "Data successfully added"
> * * *else
> * * * * * echo "Problem adding data"
> * * *fi
> fi
>
>
> and here is the error I get:
>
> [Bobster@localhost ~]$ ./mtest4 5 Johnson John 120000
> ./mtest4: line 15: syntax error near unexpected token `then'
> ./mtest4: line 15: ` * * then'
>
> Thanks in advance for any inputs.
>
> Bobster
>
>
>
> _______________________________________________
> CentOS mailing list
> CentOS@centos.org
> http://lists.centos.org/mailman/listinfo/centos
>



--
Steven Crothers
steven.crothers@gmail.com
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Andrew Harley 05-04-2011 04:42 AM

Syntax Help on a Bash Script
 
On Wed, 2011-05-04 at 00:36 -0400, Steven Crothers wrote:
> Possibly an invisible character. Delete the entire line with "then"
> and re-type it. That error seems odd.
>

Or could change to:

if [ $? -eq 0 ];then

--

Andy

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Andrew Harley 05-04-2011 04:49 AM

Syntax Help on a Bash Script
 
On Tue, 2011-05-03 at 23:33 -0500, Robert wrote:
> MYSQL='which mysql'

Also this line should use either backticks or $() command substitution
instead:

For example:

[andrew@savoy ~]$ MYSQL='which mysql'
[andrew@savoy ~]$ echo $MYSQL
which mysql
[andrew@savoy ~]$ MYSQL=$(which mysql)
[andrew@savoy ~]$ echo $MYSQL
/usr/bin/mysql
[andrew@savoy ~]$ MYSQL=`which mysql`
[andrew@savoy ~]$ echo $MYSQL
/usr/bin/mysql

Cheers!

--

Andy

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Robert 05-04-2011 04:52 AM

Syntax Help on a Bash Script
 
Thanks for the help thus far. Have tried recommendations on the error
line and still am getting the error. Oh well, its late so I'm going to
stop trying tonight and will take this up again in the AM.

Bob


On 05/03/2011 11:49 PM, Andrew Harley wrote:
> On Tue, 2011-05-03 at 23:33 -0500, Robert wrote:
>> MYSQL='which mysql'
> Also this line should use either backticks or $() command substitution
> instead:
>
> For example:
>
> [andrew@savoy ~]$ MYSQL='which mysql'
> [andrew@savoy ~]$ echo $MYSQL
> which mysql
> [andrew@savoy ~]$ MYSQL=$(which mysql)
> [andrew@savoy ~]$ echo $MYSQL
> /usr/bin/mysql
> [andrew@savoy ~]$ MYSQL=`which mysql`
> [andrew@savoy ~]$ echo $MYSQL
> /usr/bin/mysql
>
> Cheers!
>
> --
>
> Andy
>
> _______________________________________________
> CentOS mailing list
> CentOS@centos.org
> http://lists.centos.org/mailman/listinfo/centos

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

"John R. Dennison" 05-04-2011 04:59 AM

Syntax Help on a Bash Script
 
On Tue, May 03, 2011 at 11:33:52PM -0500, Robert wrote:
>
> #!/bin/bash
> # send data to the table in the MySQL database
>
> MYSQL='which mysql'

You want "MYSQL=$(which mysql)"

> If [ $? -eq 0 ]
> then
> echo "Data successfully added"
> else
> echo "Problem adding data"
> fi

"If" is incorrect.




John

--
<zu22> Hellow: bears are wonderful animals
<zu22> I love bears!
<zu22> I want to feed them marshmellows.
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Les Mikesell 05-04-2011 05:00 AM

Syntax Help on a Bash Script
 
On 5/3/11 11:33 PM, Robert wrote:
> Hi All,
>
> I'm brand new at doing anything linux and would like feedback on this
> script I'm trying to understand from an example I'm working on..
>
> Oh, running Centos 5.6
>
> Anyhow, I run this bash script:
>
> #!/bin/bash
> # send data to the table in the MySQL database
>
> MYSQL='which mysql'
>
> if [ $# -ne 4 ]
> then
> echo "Usage: mtest4 empid lastname firstname salary"
> else
> statement="insert into employees values ($1, '$2','$3', $4)"
> $MYSQL test<< EOF
> $statement
> EOF
> If [ $? -eq 0 ]
> then
> echo "Data successfully added"
> else
> echo "Problem adding data"
> fi
> fi
>
>
> and here is the error I get:
>
> [Bobster@localhost ~]$ ./mtest4 5 Johnson John 120000
> ./mtest4: line 15: syntax error near unexpected token `then'
> ./mtest4: line 15: ` then'
>
> Thanks in advance for any inputs.

The If on line 14 should be if (lowercase).

--
Les Mikesell
lesmikesell@gmail.com
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Robert 05-04-2011 01:01 PM

Syntax Help on a Bash Script
 
On 05/04/2011 12:00 AM, Les Mikesell wrote:
> On 5/3/11 11:33 PM, Robert wrote:
>> Hi All,
>>
>> I'm brand new at doing anything linux and would like feedback on this
>> script I'm trying to understand from an example I'm working on..
>>
>> Oh, running Centos 5.6
>>
>> Anyhow, I run this bash script:
>>
>> #!/bin/bash
>> # send data to the table in the MySQL database
>>
>> MYSQL='which mysql'
>>
>> if [ $# -ne 4 ]
>> then
>> echo "Usage: mtest4 empid lastname firstname salary"
>> else
>> statement="insert into employees values ($1, '$2','$3', $4)"
>> $MYSQL test<< EOF
>> $statement
>> EOF
>> If [ $? -eq 0 ]
>> then
>> echo "Data successfully added"
>> else
>> echo "Problem adding data"
>> fi
>> fi
>>
>>
>> and here is the error I get:
>>
>> [Bobster@localhost ~]$ ./mtest4 5 Johnson John 120000
>> ./mtest4: line 15: syntax error near unexpected token `then'
>> ./mtest4: line 15: ` then'
>>
>> Thanks in advance for any inputs.
> The If on line 14 should be if (lowercase).
>
Hey that was the fix..

[Bobster@localhost ~]$ ./mtest4 5 Johnson John 120000
/usr/bin/mysql
/usr/bin/test
Data successfully added

Thanks Les!

Bob

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos


All times are GMT. The time now is 05:06 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.