I try to explain exactly what i'm doing a complete way..
There are several systems that has an appliance installed and some paths are different (example log path is /var/log/DirSite1 /var/log/DirSite2 etc etc). Those systems are in some networks that could not access to internet so i wanted to create a script that collects all configuration files, gzip them so che customer could send me everything.
The idea is simple.. I declare some variables (sitename,time, date and the new log dir path created with Logs_Sitename_date_time format, and then i go to the paths to collect the files i need. The main script goes to read into a file nammed command.txt the "relative" paths and copy them into the new dir log i've crated and then zip everything
The problem is that in the While Ifs cycle the variables i've declared in the initial part of the script are lost
this is the example code (complete)
Note: the DirName variable gets the name of the site because there's a dir in home named siteName1 SiteName2, the result will be Name1 or Name2)
#!/bin/bash
clear
# Declare system fixed variables
DirName=$(ls /home/ | grep -i site | awk '{print substr($0,5)}')
DateYmd=$(date +%Y%m%d)
DateHMS=$(date +%H%M%S)
LogDir=Logs"_"$DirName"_"$DateYmd"_"$DateHMS
# Create start point in Getlogs.log
echo Site: $DirName - Date $DateYmd - Time $DateHMS | tee -a GetLogs.log
# Execute commands in commands.txt
while IFS='' read -r line || [[ -n "$line" ]]; do
if
$line > /dev/null 2>&1;then
echo "Success....... - $line" | tee -a GetLogs.log
else
echo "FAILED........ - $line" | tee -a GetLogs.log
fi
done < "$1"
so i create a txt file named commands.txt where into you could put those lines
/bin/mkdir "$LogDir"
/bin/cp /etc/hosts "$LogDir"
/bin/cp /etc/fstab "$LogDir"
/bin/cp /etc/init/site"$DirName"/* "$LogDir"
(i tired with or without " and the result is the same)
Pratically when a command is passed into the while cycle the system is unable to retain the variable value and create a dir named $LogDir and copies files into a new file named $LogDir
i tried to move the variables into the cycle after while but the result is the same. It is like reading from an external file let variables to be lost.
Any solution or there's no way to solve them?
Regards