scripting in linux mint 12

Forum rules
Before you post please read how to get help
so_opel_z
Level 1
Level 1
Posts: 2
Joined: Mon Feb 27, 2012 10:41 am

scripting in linux mint 12

Postby so_opel_z » Mon Feb 27, 2012 10:55 am

Hi. I'm new to bash and I'm trying to write a simple script that needs to convert a string into an array with whitespace as a delimiter.
However when i try to run the script it gives an error of "unexpected:(" or something like that. i'm away from my work station now so I'm not sure of the exact error.

My script goes something like:

#!/bin/bash
string="this is a sentence."
array=( $string )

The error occurs at the 3rd line but when I run it in terminal it doesn't give any error.
Any help would be appreciated. thanks!

User avatar
xenopeek
Level 23
Level 23
Posts: 19371
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: scripting in linux mint 12

Postby xenopeek » Mon Feb 27, 2012 12:16 pm

That example works fine, perhaps you should share the actual script with a problem.

Running this:

Code: Select all

#!/bin/bash
string="this is a sentence."
array=( $string )
for item in "${array[@]}"; do
   echo $item
done

Gives the expected output:

Code: Select all

this
is
a
sentence.
Image

so_opel_z
Level 1
Level 1
Posts: 2
Joined: Mon Feb 27, 2012 10:41 am

Re: scripting in linux mint 12

Postby so_opel_z » Tue Feb 28, 2012 1:12 am

Thanks for the quick response.
This is the error that your script returns:
xtest.sh: 3: Syntax error: "(" unexpected

The error only shows when I try to run it as a script but when I input each line directly into the terminal it works as normal.
I'm using LM 12, btw. I read somewhere that LM uses dash instead of bash but I started my script with #! /bin/bash so I have no idea why it doesn't work.

edit: I chmod +x my script and now it works fine. I didn't know that there would be any difference. Thanks for the help.


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest