shell - tar command unable to add attachment files in unix sun solaris -
i have been passing attachment filenames program , tar command unable create archive attachment files , attachment files can contain spaces in filenames . can me in identifying root cause
here atachment files "v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf"
ksh testing.ksh 2015-0974.htm '"v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf"' int 5
testing.ksh
file=${1} attachedfiles="${2}" echo ${attachedfiles} targetenv=${3} priority=${4} fnwp=${file%.*} ext=${file#*.} fn=${fnwp##*/} val="tar cvf ${fn}.tar title html email delivertime "${attachedfiles}"" echo $val package=${priority}_$(date +"%y%m%d%h%m%s")_eytaxalert.nwf gzip -cv ${fn}.tar > ${package} exit 0
output:
a title 1k html 33k email 4k 2015-0974.tar
expected output:
a title 1k html 33k email 4k v al 2015-0974_ca.pdf 20k v al 2015-0974_ma.pdf 15k 2015-0974.tar
this line
testing.ksh 2015-0974.htm '"v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf"' int 5
invokes testing.ksh
script 4 parameters, literally:
2015-0974.htm "v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf" int 5
note second parameter single string 4 quote marks embedded in it. assign $attachedfiles
, (i think) execute following (my doubt comes fact merely echo in snippet in question, i'm guessing pasted debugging version):
tar cvf ${fn}.tar title html email delivertime "${attachedfiles}"
that calls tar 7 arguments
cvf 2015-0974.tar title html email delivertime "v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf"
tar
interpret first argument anoption string, second 1 filename newly-created tarfile (because of f
in option string), , remaining 5 filenames. since "v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf"
not name of existing file, not added tarfile.
what intended invoke tar these arguments:
cvf 2015-0974.tar title html email delivertime v al 2015-0974_ca.pdf v al 2015-0974_ma.pdf
like pretty every other programming language, in bash quotes part of syntax. string created not include quotes. if insert quotes string (by escaping them, or using 2 types of quoting in example), string has quotes in it. doesn't scanned again.
if want provide list of filenames on command line , filenames need quoted, need quote them individually. since script not able know how many filenames there are, best put list @ end. can refer list of arguments using slice syntax (in either bash or ksh93): "${@:4}"
list of verbatim arguments (exactly provided script) starting argument 4. "${@:4:2}"
precisely 2 arguments starting argument 4, think in case want of arguments end. note quotes in syntax required; indicate arguments should passed through is, rather being wordsplit.
so script might this:
file=${1} targetenv=${2} priority=${3} fnwp=${file%.*} ext=${file#*.} fn=${fnwp##*/} tar cvf "${fn}.tar" title html email delivertime "${@:4}"
and called this:
ksh testing.ksh 2015-0974.htm int 5 "v al 2015-0974_ca.pdf" "v al 2015-0974_ma.pdf"
edit:
before solaris 11,
ksh
shell ksh88, not ksh93, above not work. see below. apparently, there ksh93 implementation availabledtksh
.in case non-standard syntax
"${@:4}"
doesn't work, here simple posix-compatible alternative:file=${1} targetenv=${2} priority=${3} # remove 3 arguments argument list: shift 3 fnwp=${file%.*} ext=${file#*.} fn=${fnwp##*/} tar cvf "${fn}.tar" title html email delivertime "${@}"
Comments
Post a Comment