开发者

keeping the bash parameters array intack

I have a bash script

fooA

#!/bin/bash
script_name=$1;
script_params=$( echo $@ | awk '{ $1=""; print $0 }' );
bash /path/to/scripts/$script_name $script_params > /dev/stdout;

and another script fooB in the .../scripts/ directory.

#!/bin/bash

echo 1. $1
echo 2. $2

My plan is simple:

fooA fooB "some sentence 1" "some sentence 2"

should produce:

  1. some sentence 1
  2. some sentence 2

Using my current script, I would get

  1. some
  2. sentence

Because the double quotes are not preserved when calling fooB from fooA.

Keeping in mind that there开发者_高级运维 many other scripts in the .../scripts directory, how would I change the script_params=$(...) line in fooA file to preserve variables when calling other scripts.


@jm666's answer will work fine if there are no additional constraints. For completeness, though, I'll give a version that doesn't mess with the first script's argument list:

#/bin/bash
script_name="$1"
script_params=( "${@:2}" )
bash /path/to/scripts/"$script_name" "${script_params[@]}" > /dev/stdout

Or you can skip the variables entirely:

#/bin/bash
bash /path/to/scripts/"$1" "${@:2}" > /dev/stdout


#!/bin/bash
name="$1"
shift
"/path/to/script/$name" "$@"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜