You could try something like this:
#!/bin/bash
clear
ls -d */
echo "Elige el directorio del cual hacer el backup (sin incluir el /)"
read directorio
echo "Realizando el backup de $directorio..."
tar -zcvf $directorio"_"$(date +%F).tar.gz $directorio
The error you have in the program is in the last line.
When you do this:
$(directorio)_$(date +%F).tar.gz
By using the operator $()
, you are indicating that what is inside the parentheses is a command , therefore he ignores this and omits it, which results in it being < strong> _aaaa-mm-dd .
Therefore.
The solution consists of two steps:
- Call the variable in the appropriate way
- Concatenate to the name of the directory the character _
How do we do it?
Like this:
$directorio"_"$(date +%F).tar.gz
What we indicate to the interpreter is that the name of the directory concatenates the character _
, it should be concatenated since if it were added without the quotes, like this:
$directorio_$(date +%F).tar.gz
The interpreter would look for the variable $directorio_
, which would leave the file with the name yyyy-mm-dd.tar.gz .