If option statement

I working on my Apache2 restore script.

This is the code so far

updatedb
prompt_list "Whick configuration_backup file do you want to use?" `find /restore/configuration/ -name configuration_backup* -type d`
BACKUP_OBJECT=${CHOICE}
echo "--------------------------------------"

cd ${CHOICE}

tar_gz=`find @etc@apache2@mods-available.tar.gz @etc@apache2@mods-enabled.tar.gz @etc@apache2@ports.conf.tar.gz @etc@apache2@sites-available.tar.gz \
@etc@apache2@sites-enabled.tar.gz`

echo "Founded Apache2 tar.gz files"
echo "--------------------------------------"
echo "${tar_gz}"

#tar -zvf ${tar_gz}

if [ -f "{$tar_gz}" ]
then
        echo "found"
else
        echo "not found"
fi

My problem now is the if and else. I want that the if statement finds the @etc@apache2 tar.gz files that i found with the "find" cmd so i can continue to extract the tar.gz files and other commands.
Somebody knows the good variabel of something else that can help?

Thank you

If tar_gz holds more than one file name, the expression fails, as it has "too many arguments". Make sure to work on one file at a time, e.g. by using a for loop or similar.

1 Like

Thank you for you message. I will make a for loop. But is necessary that all the 5 tar_gz are in the backup for the restore and if not that the restore script exits. Is this possible in a for loop you think?

Check the number of files before, e.g. make tar_gz an array and use count of elements (bash: ${#tar_gz[@]} )

Do you have a example maybe for me? I'm doing anything but nothing works..

Not knowing what shell you use, here's a bash proposal:

tar_gz=( $(find @etc@apache2...) )
[ ${#tar_gz[@]} -ne 5 ] && { echo "file set incomplete"; exit 1; }
for FN in ${tar_gz[@]}; do ... ; done 
1 Like

Thank you for helping.

This is my code now

tar_gz=( $(find @etc@apache2@mods-available.tar.gz @etc@apache2@mods-enabled.tar.gz @etc@apache2@ports.conf.tar.gz @etc@apache2@sites-available.tar.gz \
@etc@apache2@sites-enabled.tar.gz) )

cd ${CHOICE}

echo "Founded Apache2 tar.gz files"
echo "--------------------------------------"
echo "$tar_gz"

if [ ${#tar_gz[@]} -eq 5 ]
then
        echo "found"
else
        echo "not found"
-- INSERT -- 

But for my i won't work

My bash is /bin/bash/

That's not enough. What does not work? Error msgs? Undesired output? And, why didn't you copy my proposal fragment?

I'm totally lost in it now.. I tried your piece of code.

tar_gz=( $(find @etc@apache2@mods-available.tar.gz @etc@apache2@mods-enabled.tar.gz @etc@apache2@ports.conf.tar.gz @etc@apache2@sites-available.tar.gz \
@etc@apache2@sites-enabled.tar.gz) )

if [ ${#tar_gz[@]} -ne 5 ] 
then
        echo "file set incomplete"
        exit 1
fi

for FN in ${tar_gz[@]} 
do 
        if [ -f $tar_gz ]
        then
                echo "found"
                tar -zvf $tar_gz
        fi

done

Want i want for this script:

Find all the 5 tar.gz files, if they're found than get a message and extract them if they're not found exit this script.

Try

for FN in ${tar_gz[@]} 
do 
        if [ -f "$FN" ]
        then
                echo "found"
                tar -zvf "$FN"
        fi

done

Hello RudiC

This is my code now and it works.

FILES=`find ${CHOICE} -name @etc@apache2@*.tar.gz  -type f`

echo "Founded tar.gz files from Apache2"
echo "--------------------------------------"
echo "${FILES}"
echo "--------------------------------------"

for FILE in ${FILES}
do
        tar -zxf ${FILE} -C /etc/apache2
        RESULT=$?
        echo "Result off tar.gz ${FILE} extract = ${RESULT}"
done

service apache2 reload

Thank you for helping me! I appreciate it. :slight_smile: