A
A
Alexander F2020-02-25 16:40:38
bash
Alexander F, 2020-02-25 16:40:38

How to handle an empty argument in a function?

I am writing a backup script.
It has a function using tar to make an archive.
There are 3 types of archives. Two of them have excluded folders. One does not.
Paths to be archived and excluded folders are passed using array references from the main script.
The problem is that in the third option, when there are no excluded folders, the script is interrupted.
The output of bash -x shows that an empty string is being added to tar, which I understand is the reason. Tell me how to fix this?
Script example:

#!/bin/bash
function backup() {
    local include=("${!1}")
  local exclude=("${!2}")
    tar -cvpzPf /home/backup/backup.tar.gz "${include[@]}" "${exclude[@]}"
}

case "$1" in
    1)
        include=(/foo2 /foo3)
        exclude=(/bar2 /bar3)
        backup include[@] exclude[@]
    ;;
    2)
        include=(/foo /foo1)
        exclude=(/bar /bar1)
        backup include[@] exclude[@]
    ;;
    3)
        include=(/etc /sbin)
        backup include[@]
    ;;
esac

The example is schematic, but shows the general essence

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CityCat4, 2020-02-25
@CityCat4

Check the lengths of the local_include and local_exclude lines and form one line from them, which is slipped into tar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question