pass swithes along to subroutines 

Newsgroups: comp.unix.shell
 > I want to pass a swich taken from command line into subroutines.
 > E.g, for the following code, I want to pass the command line swich
 > -t and its parameter into sub2:
[snip]
 > You can see, the first time it works fine, but the second time it is
 > not. and the output of the echo (with "!!" as comment) is wrong too.
 >
 > The reson I can't pass directy is that I also want to do some
 > processing along the passing. This is a general question about how
 > to pass swithes along to subroutines with proper quoting and in
 > proper way.
>sub1(){
>
>  [ "x$1" = "x-t" ] && {
>    file_title_s="-t '$2'"
>    shift 2
>    }
>  sub2 $file_title_s "$@" more from sub1
>}

Is there a reason that you are restricted to one variable? It's simpler if you use two:

sub1(){
  case $1 in
   -t) file_title_s=-t
       file_title_o=$2
       shift 2 ;;
  esac
  sub2 "$file_title_s" "$file_title_o" "$@" more from sub1
}

Ken Pizzini

pass swithes along to subroutines 

this one works fine.

$ cat /tmp/test
sub1(){
echo sub1:$#
        [ "x$1" = "x-t" ] && {
                file_title_s="-t \"$2\""
                shift 2
        }
        args=
        for arg in "$@"; do args="$args \"$arg\""; done
        eval set -- $file_title_s $args
        sub2 "$@" "5 6"
}

sub2(){
echo sub2:$#
        [ "x$1" = "x-t" ] && {
                file_title="$2"
                shift 2
        }
        echo "$1 should be: 2nd"
        echo $file_title
        echo "$@"
}

[ "x$1" = "x-t" ] && {
        file_title_s="-t \"$2\""
        shift 2
}

echo sub0:$#
args=
for arg in "$@"; do args="$args \"$arg\""; done
eval set -- $file_title_s $args
sub1 "$@" "3 4"

$ ast-ksh -x /tmp/test -t '1 2' 2nd 3rd
+ [ x-t = x-t ]
+ file_title_s='-t "1 2"'
+ shift 2
+ echo sub0:2
sub0:2
+ args=''
+ args=' "2nd"'
+ args=' "2nd" "3rd"'
+ eval set -- -t '"1' '2"' '"2nd"' '"3rd"'
+ set -- -t '1 2' 2nd 3rd
+ sub1 -t '1 2' 2nd 3rd '3 4'
+ echo sub1:5
sub1:5
+ [ x-t = x-t ]
+ file_title_s='-t "1 2"'
+ shift 2
+ args=''
+ args=' "2nd"'
+ args=' "2nd" "3rd"'
+ args=' "2nd" "3rd" "3 4"'
+ eval set -- -t '"1' '2"' '"2nd"' '"3rd"' '"3' '4"'
+ set -- -t '1 2' 2nd 3rd '3 4'
+ sub2 -t '1 2' 2nd 3rd '3 4' '5 6'
+ echo sub2:6
sub2:6
+ [ x-t = x-t ]
+ file_title='1 2'
+ shift 2
+ echo '2nd should be: 2nd'
2nd should be: 2nd
+ echo 1 2
1 2
+ echo 2nd 3rd '3 4' '5 6'
2nd 3rd 3 4 5 6

Cyrille.

pass swithes along to subroutines 

Though seem complicated, the above is really the minimum steps. Here is a trying-to-be-simple one and its failure record:

[Note]

copied exactly from above

sub1(){
echo sub1:$#
        [ "x$1" = "x-t" ] && {
                file_title_s="-t \"$2\""
                shift 2
        }
        args=
        for arg in "$@"; do args="$args \"$arg\""; done
        eval set -- $file_title_s $args
        sub2 "$@" "5 6"
}

[ "x$1" = "x-t" ] && {
    file_title_s="-t \"$2\""
    shift 2
    }
[Note]

neglect the for loop

eval set -- $file_title_s "$@"
  sub1 $file_title_s more parameters
[Tip]

!!

$ sh -x test_script0 -t "This is a test" 2nd 3rd
+ [ x-t = x-t ]
file_title_s=-t 'This is a test'
+ shift 2
+ sub1 -t 'This is a test' more parameters
+ echo sub1:7
sub1:7
+ [ x-t = x-t ]
file_title_s=-t "'This"
+ shift 2
args=
args= "is"
args= "is" "a"
args= "is" "a" "test'"
args= "is" "a" "test'" "more"
args= "is" "a" "test'" "more" "parameters"
+ eval set -- -t "'This" "is" "a" "test'" "more" "parameters"
+ set -- -t 'This is a test' more parameters
+ sub2 -t 'This is a test' more parameters 5 6
+ [ x-t = x-t ]
file_title='This
+ shift 2