views:

40

answers:

1

Hi all !

(Sorry for the confusion. Previous $ sign occurred when I tried to simplify the actual problem. Thanks for correcting the question)

I wanted to split a directory name on underscores (ex: dir_to_split="my_test_dir") like this:

my_dir=($dir_to_split)
var=$(echo $my_dir | awk -F"_" '{print $1,$2,$3}')   
set -- $var

splited_1=$1
splited_2=$2
splited_3=$3

now using these splited_x is causing me errors. ex.

myprograme $splited_1 $splited_2 $splited_3

Can anyone please help me with this ? Thank you....

+1  A: 

(Rewritten after updated question.)

What kind of errors do you get? I find it useful to add set -x to the top of my shell scripts when debugging, this lets the shell print all commands it executes so you can pinpoint the line where problems begin.

Are you sure that $dir_to_split is actually set? Does it contain spaces or tabs? Does it contain two underscores? I don't see any other problems right now.

There are in-shell methods of splitting a variable such as:

dir="my_test_dir"
OIFS="$IFS"
IFS="-"
set --
IFS="$OIFS"

See also this SO question.

schot
I've corrected your answer's code example (I think it's now what you really mean)
Delan Azabani
In which case do you not mean: temp="app"dir_name=$tempmkdir $dir_name
Liam Bailey
@Delan: Thanks, just spotted it myself and was correcting it when I saw a nice popup appear.
schot
I'm sorry for my mistake on posting the question. Actually I simplified the code, so pls let me try my original code.
Morpheus
hey ! I have posted the actual problem. Please refer it.....
Morpheus
$dir_to_split is actually set, and no spaces, tabs. but only underscores. Let me try your method.
Morpheus
hey ! how can I then access the individual elements in that array, without that for loop ?
Morpheus
@Morpheus: Your elements are still `$1`, `$2`, etc.
schot