scripting in linux mint 12

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
so_opel_z

scripting in linux mint 12

Post by so_opel_z »

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!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29612
Joined: Wed Jul 06, 2011 3:58 am

Re: scripting in linux mint 12

Post by xenopeek »

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

Re: scripting in linux mint 12

Post by so_opel_z »

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.
Locked

Return to “Scripts & Bash”