sh question: commands as variables / stream operators in a variable

Christopher Friedt cfriedt-u6hQ6WWl8Q3d1t4wvoaeXtBPR1lH4CV8 at public.gmane.org
Wed Jul 11 11:40:39 UTC 2007


woops,

sorry... .. was saying bzip instead of bzip2

... :)

correct script is here:


#!/bin/sh

echo 'Hello World!' > test.txt
CMD="cat test.txt"
OUTFILE="test2.txt"

# COMP can be one of '', 'bzip2', 'gzip'
COMP="bzip2"
COMP_CMD=""
case "$COMP" in
("bzip2")
   COMP_CMD=" | bzip2 -9 "
   OUTFILE="${OUTFILE}.bz2"
;;
("gzip")
   COMP_CMD=" | gzip -c -9 "
   OUTFILE="${OUTFILE}.gz"
;;
("")
   COMP_CMD=""
;;
(*)
exit 1
;;
esac

CMD="${CMD} ${COMP_CMD} > ${OUTFILE}"
exit $?


Christopher Friedt wrote:
> This is a bit of a tricky thing to work around, but i would basically 
> like to include shell stream operators in a variable that will 
> eventually be a command string.
> 
> Particularly, I would like to make compression selectable in a script 
> through a variable which contains the pipe operator '|' and then run a 
> command (stored also as a variable) which would include a redirect on 
> stdout '>'
> 
> Does anyone know why something like this wouldn't work (at least in bash) ?
> 
> My output is:
> cat: invalid option -- 9
> Try `cat --help' for more information.
> 
> Any suggestions are very welcome ;-)
> 
> ~/Chris
> 
> A simple test script is here:
> 
> #!/bin/sh
> 
> echo 'Hello World!' > test.txt
> CMD="cat test.txt"
> OUTFILE="test2.txt"
> 
> # COMP can be one of '', 'bzip2', 'gzip'
> COMP="bzip2"
> COMP_CMD=""
> case "$COMP" in
> ("bzip2")
>   COMP_CMD=" | bzip -9 "
>   OUTFILE="${OUTFILE}.bz2"
> ;;
> ("gzip")
>   COMP_CMD=" | gzip -c -9 "
>   OUTFILE="${OUTFILE}.gz"
> ;;
> ("")
>   COMP_CMD=""
> ;;
> (*)
> exit 1
> ;;
> esac
> 
> CMD="${CMD} ${COMP_CMD} > ${OUTFILE}"
> exit $?
> -- 
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
> 
> 
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list