Store shell arguments in a file while saving citations

How can shell arguments be stored in a file for later use when saving citations?

To be clear: I don't want to pass arguments in place, which can easily be done with "$@". But actually you need to store them in a file for later use.

#!/bin/sh
storeargs() {
    : #-)
}
if "$1"
then
    # useargs is actuall 'git filter-branch'
    useargs "$@"
    storeargs "$@"
else
    # without args use those from previous invocation
    eval useargs $(cat store)
fi

.

$ foo 'a "b"' "c 'd'" '\'' 'd
e'
$ foo # behave as if called with same arguments again

The question probably boils down to how to quote a string using common tools in general (awk, perl, ...). I would prefer a solution that does not make the quoted string unreadable. The content storeshould look more or less like what I specify on the command line.

, /, , (shell) / () , .

+3
2

?

storeargs() {
     while [ $# -gt 0 ]
     do 
         printf "%q " "$1"
         shift
     done   
}

storeargs "some" "weird $1 \`bunch\` of" params >  myparams.txt
storeargs "some" 'weird $1 \`bunch\` of' params >> myparams.txt
cat myparams.txt

some weird\ \ \`bunch\`\ of params
some weird\ \$1\ \\\`bunch\\\`\ of params
+5

, . , , ( useargs() { "$ @"; do echo $i; done;}):

storeargs() { printf "%q\n" "$@"; } > store

if test -n "$1"; then
  useargs "$@"
  storeargs "$@"
else
  eval useargs $args
fi

- EDIT-- % q printf, ( ). , % q printf bash, printf.

+1

All Articles