eval is your friend
AMP=
if [[ $BgFlag == y ]]
AMP="&"
fi
eval myprogram options_and_args $AMP
--- Mario DeFazio <[email protected]> wrote:
> Hi everyone,
>
> I want to pass an option flag to a process startup script to tell it to run
> one particular program within the script in the background or not.
>
> I discovered a long time ago that I cannot simply set
> something like BgOpt='&' and stick it at the end of the
> target command within the script -- it just doesn't jive
> with the deep magic of how sh/ksh scans and parses a command line.
> In the past I have simply resigned myself to using an if-then-else
> construct with dupe copies of the entire command, one with the '&'
> at the end, and one without:
>
> if [[ $BgFlag == y ]]
> then
> myprogram options_and_args &
> else
> myprogram options_and_args
> fi
>
> I never liked the duplication, but I like working code more, so I have
> lived with it.
> But now I have a really long command to run, and duplicating it just seems
> too prone to maintenance errors.
> I suppose I could wrap it in a function and put the function in the above
> if-then-else, but since I have always wondered if there was a single
> line solution
> for this task, I have finally decided to write this up and put it out there.
>
> One last point I should mention: this might have to be run in a /bin/sh
> (Bourne) script,
> so I need to know if any solution has a portability issue.
>
> Regards,
> Mario DeFazio
>
>
> _______________________________________________
> ast-users mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-users
>
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users