bash printf: ignore parameters or access by index?

2019-08-12 20:30发布

问题:

I have a bash function that fetches values (using curl and cut) and creates a file name from them. Now I want to support a second naming scheme that needs a different set of parameters.

Example:

#!/bin/bash

TEMPLATE="%02i. %s.txt"

foo() {
    a="Imagine these to"
    b="be set dynamically"
    c="42"

    filename="$(printf "$TEMPLATE" "$c" "$a")"
    # second: filename="$a - $b.txt"
    # or:     filename="$(printf "%s - %s.txt" "$a" "$b")"
    echo "$filename"
    # generate file
}

# actual script loops over:
foo

One of the values is a number that should be padded with leading zeros if required, thus printf in the current implementation.

Is there a way to implement this with just setting a different template globally? This would require that the template can access parameters by index or at least skip some of them.

If not, what are my alternatives? The template is to be chosen by command line parameter and does not change after initialization.

What does not work:

  • bash man page suggests that zero length output is not possible (to skip values)
  • C's printf man page mentions a "%m$" construct, which apparently is not supported in bash
  • the function itself generates the values, so it cannot receive the full filename as parameter

回答1:

If you need to skip an argument, you can use %.s. Examples:

$ printf "%.s%s\n" "Bonjour" "Hello"
Hello
$ printf "%s%.s\n" "Bonjour" "Hello"
Bonjour

AFAIK, you can't access arguments by index.

If you need to store the formated string in a variable, please don't use a subshell as in:

$ variable=$(printf "%.s%s" "Bonjour" "Hello")
$ echo "$variable"
Hello

Instead, use the -v option to printf (type help printf to have the details) as in:

$ printf -v variable "%.s%s" "Bonjour" "Hello"
$ echo "$variable"
Hello

Also, if your template can come from user input, I would advise you to add -- just before it (this is to end the options of the command, just in case a user wants to start a template with a dash). Hence I would replace you line

filename="$(printf "$TEMPLATE" "$c" "$a")"

with

printf -v filename -- "$TEMPLATE" "$c" "$a"

Finally, having upper case variable names is considered bad bash practice.



标签: bash printf