开发者

Problem with run commands in shell (bash) with arguments as variables !

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜