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

4
  • This is confusing. Are you now saying that even after removing $ from first statement, it is still not working? Commented Oct 12, 2010 at 10:08
  • Sorry guys, above is the real example. $ sign error occurred when I tried to simplify the problem Commented Oct 12, 2010 at 10:19
  • What are the exact errors? It's rather hard to diagnose a problem without knowing what it is. Commented Oct 12, 2010 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. Commented Oct 12, 2010 at 10:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I've corrected your answer's code example (I think it's now what you really mean)
In which case do you not mean: temp="app" dir_name=$temp mkdir $dir_name
@Delan: Thanks, just spotted it myself and was correcting it when I saw a nice popup appear.
I'm sorry for my mistake on posting the question. Actually I simplified the code, so pls let me try my original code.
$dir_to_split is actually set, and no spaces, tabs. but only underscores. Let me try your method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.