Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question