bash script continues to show "does not exist. file " - csv

I'm running this bash scripts to read a CSV File which contains almost 10 lines with 05 columns:
Variables Global
DATE=$(date) #Set DATE equal to the output of running the shell command date
HOSTNAME=$(hostname) #Set HOSTNAME equal to the output of the hostname command
CSV_FILE='/scrpt/jobs_aix01.csv'
CSV_DELIMITADOR=','
#Variables
contador_sql=0
cadena_sql="error"
## Script
while IFS=\, read col_job_name col_os col_server col_user col_script
do
#echo "$col_job_name $col_os $col_server $col_user $col_script"
ls -l $col_script
done < "$CSV_FILE"
The script must read the CSV file and run the command ls -l but I get "does not exist. file "
Any ideas?
note: the files exist.
regards
unix jose

Related

unoconv return error when running as www-data

When running this from command line as root it works
unoconv -f csv $file
But when running it as www-data this error is returned
Traceback (most recent call last):
File "/usr/bin/unoconv", line 1114, in <module>
office_environ(of)
File "/usr/bin/unoconv", line 203, in office_environ
os.environ['PATH'] = realpath(office.basepath, 'program') + os.pathsep + os.environ['PATH']
File "/usr/lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'PATH'
update
echo shell_exec('echo $PATH');
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
centos 7.3 with php via php-fpm
the env in php is cleaned by php-fpm
u can use putenv to set evn["PATH"] in php code, examples
putenv("PATH=/sbin:/bin:/usr/sbin:/usr/bin");
var_dump(shell_exec('unoconv -vvvv -f pdf -o 123.pdf 123.doc));
or u can set env use one line shell cmd
var_dump(shell_exec('PATH=/sbin:/bin:/usr/sbin:/usr/bin'.' unoconv -vvvv -f pdf -o 123.pdf 123.doc));
or u can change /etc/php-fpm.d/www.conf to pass the env to php, add this line
clean_env = no
and the restart php-fpm
systemctl restart php-fpm.service
The PHP call you used (pasted from chat):
exec("unoconv -f csv $file")
My guess is that exec() is giving you an environment that is too limited. To work around this, you could set up a polled directory. The PHP script would copy files to be converted into the polled directory and wait for the files to be converted.
Then create a bash script (either running as root or a somewhat more secure user) to run in an infinite loop and check the polled directory for any incoming files. See How to keep polling file in a directory till it arrives in Unix for what the bash script might look like.
When the bash script sees incoming files, it runs unoconv.
Found a solution myself by running libreoffice directly
sudo libreoffice --headless --convert-to csv --outdir $tmp_path $file

Copying mysqldump files from one directory to a backups directory

I'm building a shell script that will run nightly with the help of crontab and my script keeps bombing out and giving me the error "Syntax error: word unexpected (expected "do").
The script itself is creating a new directory for each MySQL file it finds in a directory and then copies the files to a created directory from the respective mysqldump directory. Please let me know if I am on the right track with this code and what could be causing my end of file error. I am new to shell scripting and would accept any advice given. If there is a better way to write my code then feel free to help in that respect as well.
CDPATH="/backups/mysql"
#
now=$(date +"%Y_%m_%d")
#
# Find Directory Name
#
for file in */; do
dir=${file%/}
if [[ -e "$dir"]]
then
echo "Directory Exists!"
else
echo "Directory doesn't exist."
fi
done
#
# Copy MySQL Databases
#
while [ -d $dir ]; do # Check existing Dirs
cp -upf /dbase/files/*.sql /backups/mysql/$dir/$now # If Dir exists, create copy
if [ ! -d $dir ]; then # If Dir nonexistant create
mkdir -p $dir
cp -upf /dbase/files/*.sql /backups/mysql/$dir/$now
else # If all else fails just create a copy in /mysql
cp -upf /dbase/files/*.sql /backups/mysql/$dir/$now
fi
done
Thanks for the help in advance!
1. do not use "then" after "else"
2. The second line may does not what you want. It just set the variable dir to the string "dir (/backups/mysql/*)"
... obsolete since question has changed.
I assume, files in the following form ...
/dbase/files/db1.sql
/dbase/files/db2.sql
...
... should be backed up to the following destination:
/backups/mysql/db1/2014_11_18/db1.sql
#!/bin/bash
BACKUP_DIR="/backups/mysql/"
FILE_DIR=/dbase/files/
now=$(date +"%Y_%m_%d")
# setting the input field seperator to newline
IFS=$'\n'
# find db backups and loop over
for file in $(find ${FILE_DIR} -maxdepth 1 -name "*.sql" -type f -exec basename {} \;); do
# create backup directory:
mkdir -p "${BACKUP_DIR}${file%.sql}/${now}"
# copy file over
cp "${FILE_DIR}${file}" "${BACKUP_DIR}${file%.sql}/${now}/"
done
Convert the script to unix encoding:
dos2unix script.sh

Issues with MySQL restart on running through a crontab scheduler

I have written a shell script which starts MySQL when its killed/terminated. I am running this shell script using a crontab.
My cron looks for the script file named mysql.sh under /root/mysql.sh
sh /root/mysql.sh
mysql.sh:
cd /root/validate-mysql-status
sh /root/validate-mysql-status/validate-mysql-status.sh
validate-mysql-status.sh:
# mysql root/admin username
MUSER="xxxx"
# mysql admin/root password
MPASS="xxxxxx"
# mysql server hostname
MHOST="localhost"
MSTART="/etc/init.d/mysql start"
# path mysqladmin
MADMIN="$(which mysqladmin)"
# see if MySQL server is alive or not
# 2&1 could be better but i would like to keep it simple
$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
# MySQL's status log file
MYSQL_STATUS_LOG=/root/validate-mysql-status/mysql-status.log
# If log file not exist, create a new file
if [ ! -f $MYSQL_STATUS_LOG ]; then
cat "Creating MySQL status log file.." > $MYSQL_STATUS_LOG
now="$(date)"
echo [$now] error : MySQL not running >> $MYSQL_STATUS_LOG
else
now="$(date)"
echo [$now] error : MySQL not running >> $MYSQL_STATUS_LOG
fi
# Restarting MySQL
/etc/init.d/mysql start
now1="$(date)"
echo [$now1] info : MySQL started >> $MYSQL_STATUS_LOG
cat $MYSQL_STATUS_LOG
fi
When I run the above mysql shell script manually using webmin's crontab, MySQL started successfully (when its killed).
However, when I schedule it using a cron job, MySQL doesn't starts. The logs are printed properly (it means my cron runs the scheduled script successfully, however MySQL is not restarting).
crontab -l displays:
* * * * * sh /root/mysql.sh
I found from URL's that we should give absolute path to restart MySQL through schedulers like cron. However, it haven't worked for me.
Can anyone please help me!
Thank You.
First, crontab normaly looks like this:
* * * * * /root/mysql.sh
So remove the surplus sh and put it at the beginning of the script - #!/bin/bash I suppose (why are you referring to sh instead of bash?) and don't forget to have an execute permission on the file (chmod +x /root/mysql.sh)
Second, running scripts within crontab is tricky, because the environment is different! You have to set it manually. We start with PATH: go to console and do echo $PATH, and then copy-paste the result into export PATH=<your path> to your cron script:
mysql.sh:
#!/bin/bash
export PATH=.:/bin:/usr/local/bin:/usr/bin:/opt/bin:/usr/games:./:/sbin:/usr/sbin:/usr/local/sbin
{
cd /root/validate-mysql-status
/root/validate-mysql-status/validate-mysql-status.sh
} >> OUT 2>> ERR
Note that I also redirected all the output to files so that you don't receive emails from cron.
Problem is how to know which other variables (besides PATH) matter. Try to go through set | less and try to figure out which variables might be important to set in the cron script too. If there are any MYSQL related variables, you must set them! You may also examine the cron script environment by putting set > cron.env to the cron script and then diff-ing it against console environment to look for significant differences.

Bash for loop picking up filenames and a column from read -r and gnu plot

The top part of the following script works great, the .dat files are created via the MySQL command, and work perfectly with gnu plot (via the command line). The problem is getting the bottom (gnuplot) to work correctly. I'm pretty sure I have a couple of problems in the code: variables and the array. I need to call each .dat file (plot), have the title in the graph (from title in customers.txt)and name it (.png)
any guidance would be appreciated. Thanks a lot -- RichR
#!/bin/bash
set -x
databases=""
titles=""
while read -r ipAddr dbName title; do
dbName=$(echo "$dbName" | sed -e 's/pacsdb//')
rm -f "$dbName.dat"
touch "$dbName.dat"
databases=("$dbName.dat")
titles="$titles $title"
while read -r period; do
mysql -uroot -pxxxx -h "$ipAddr" "pacsdb$dbName" -se \
"SELECT COUNT(*) FROM tables WHERE some.info BETWEEN $period;" >> "$dbName.dat"
done < periods.txt
done < customers.txt
for database in "${databases[#]}"; do
gnuplot << EOF
set a bunch of options
set output "/var/www/$dbName.png"
plot "$dbName.dat" using 2:xtic(1) title "$titles"
EOF
done
exit 0
customers.txt example line-
192.168.179.222 pacsdbgibsonia "Gibsonia Animal Hospital"
Error output.....
+ for database in '"${databases[#]}"'
+ gnuplot
line 0: warning: Skipping unreadable file ".dat"
line 0: No data in plot
+ exit 0
to initialise databases array:
databases=()
to append $dbName.dat to databases array:
databases+=("$dbName.dat")
to retrieve dbName, remove suffix pattern .dat
dbName=${database%.dat}

How do I name the output textfile to YYYYMMDD based on the system date?

How do I name the output textfile to YYYYMMDD based on the system date?
sqlcmd -S DataBBB -i c:\scripts\followup.sql
-o %DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt -s ; -W -u
Now the output text file is 01_31_2012.txt.
How can I change it to 2012_01_31.txt?
Tried it under Windows 7 Premium (German), may be dependent upon OS and Local Time Format.
sqlcmd -S DataBBB -i c:\scripts\followup.sql
-o %date:~-4%_%date:~3,2%_%date:~0,2%.txt -s;
You have to edit this part for your system
%date:~-4%_%date:~3,2%_%date:~0,2%.txt
The statement used the command line internal %date% - variable with the extension :~start,length. So you can create the filename with different parts from the date- variable.