1

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

outis
  • 75,655
  • 22
  • 151
  • 221
Morpheus
  • 1,722
  • 5
  • 27
  • 38
  • This is confusing. Are you now saying that even after removing $ from first statement, it is still not working? – Manoj R Oct 12 '10 at 10:08
  • Sorry guys, above is the real example. $ sign error occurred when I tried to simplify the problem – Morpheus Oct 12 '10 at 10:19
  • What are the exact errors? It's rather hard to diagnose a problem without knowing what it is. – outis Oct 12 '10 at 10:46
  • Actually, no errors in the display. But happening is $splited_1 is contain all the data as same as $dir_to_split and others (splited_x) don't. Now I think, above spiting thing isn't working anymore. – Morpheus Oct 12 '10 at 10:59

1 Answers1

1

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

Community
  • 1
  • 1
schot
  • 10,958
  • 2
  • 46
  • 71