File not adding in zip by bash script but work in command line in terminal - bash

I am running a script in cron and trying to add a file in a zip by below code,
#!/bin/bash
cd /home/mainfolder/cron
date=$(date --date=yesterday +%F)
FILE=/home/mainfolder/folder/$date.zip
if test -f "$FILE"; then
echo $date
else
wget -r -np -nH --user=USER --password=PASSWORD https://sercret-website.com/$date.zip -P /home/mainfolder/folder/
zip -ur $date.zip file.txt
mkdir temp
cp /home/mainfolder/folder/$date.zip /home/mainfolder/temp/
cd /home/mainfolder/temp/
fi
my file name is yesterday date lets say today is 2019-12-18 (yyyy-mm-dd) my file name is 2019-12-17.zip
when I run in bash sh -x script.sh it didn't work
when i run normally in terminal it work well
zip -ur 2019-12-17.zip file.txt
I am using CentOS server.
Anyone know what I am doing wrong.
I tries
#!/bin/sh != #!/bin/bash
#!/bin/sh
#!/bin/bash
nothing work

I'd change date=$(date --date=yesterday +%F) to:
date=`date --date=yesterday +%F`
and write zip as
zip -ur ${date}.zip file.txt
and shebang as:
#!/bin/sh -x
Then I'd look for reason of failure in cron log files.

Related

Correct Regex in SFTP bash script

I want to automate a SFTP process to transfer the last file created in local server and send it to remote server.
In local server I have "/Source/Path/" I have files named like below:
Logfile_2019-04-24
Logfile_2019-04-24_old.txt
This is my current script:
dyear=`date +'%Y' -d "1 day ago"`
dmonth=`date +'%b' -d "1 day ago"`
ddate=`date +%Y-%m-%d -d "1 day ago"`
HOST='192.168.X.X'
USER='user'
PASSWD='password'
localpath='/Source/Path/'$dyear'/'$dmonth'/'*$ddate*'.txt'
remotepath='/Destination/Path/'$dyear'/'$dmonth'/'
echo $localpath
echo $remotepath
export SSHPASS=$PASSWD
sshpass -e sftp $USER#$HOST << EOF
put '$localpath' '$remotepath'
EOF
When I do echo $localpath it prints the correct file but in the script I get this error:
Connecting to 192.168.X.X...
sftp> put '/Source/Path/2019/Apr/*2019-04-24*' '/Destination/Path/2019/Apr/'
stat /Source/Path/2019/Apr/*2019-04-24*: No such file or directory
How would be the correct regex in this pasrt *$ddate*'.txt' in followingline:
localpath='/Source/Path/'$dyear'/'$dmonth'/'*$ddate*'.txt'
in order to transfer the file "Logfile_2019-04-24_old.txt"?
Thanks in advance
Replace
put '$localpath' '$remotepath'
with
put "$(echo $localpath)" '$remotepath'
to force wildcard (*) replacement in your here-doc.
This does not work if your wildcard is replaced by multiple files.
I don't think you need a regex for this problem. You can get the latest file created in the directory by the following shell command and assign it to your localpath variable.
ls -t directoryPath | head -n1
latestfile=`ls -t /Source/Path/$dyear/$dmonth | head -n1`
localpath='/Source/Path/'$dyear'/'$dmonth'/'$latestfile''
remotepath='/Destination/Path/'$dyear'/'$dmonth'/'
If you are able to get the filename, source and destination directories properly, you can directly use scp to copy the file to remote server:
sshpass -p $PASSWD scp $localpath $USER#$HOST:$remotepath

Running executable file with additional options or arguments

I'm writing a bash script Test.sh that aims to execute anotherscript (a linux executable file):
#!/bin/bash -l
mp1='/my/path1/'
mp2='/my/path2/anotherscript'
for myfile in $mp1*.txt; do
echo "$myfile"
"$mp2 $myfile -m mymode"
echo "finished file"
done
Notice that anotherscript takes as arguments $myfile and options -m mymode.
But I get the file not found error (says Test.sh: line 8: /my.path2/anotherscript: No such file or directory).
My questions are:
I have followed this question to get my bash script to run the executable file. But I'm afraid I still get the error above.
Am I specifying arguments as they should to execute the file?
I suggest you use
sh -c "$mp2 $myfile -m mymode"
instead of just
"$mp2 $myfile -m mymode"
#!/bin/bash -l
dir=`find /my/path1/ -name "*.txt"`
mp2='/my/path2/anotherscript'
for myfile in "$dir"; do
echo "$myfile"
"$mp2" "$myfile" -m mymode
echo "finished file"
done
Make sure anotherscript has execution right (chmod +x anotherscript).

Server shell backup script (bash)

Name of a script - backup_script.sh
Location of a script on server - /home/company_folder/company_site_backups
Line added to the cron file:
#monthly /home/company_folder/company_site_backups/backup_script.sh
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR; do zip -r "${i%/}.zip" "$BACKUPDIR/$i-$NOW"; done
ls -l
echo "Done!"
But unfortunately my script does not work properly. Actually. It does not run at all! I do not see any errors in the syntax.
Does anyone know how to fix it?
The cd $DIR seems strange; if the first entry found by /home/company_folder/company_applications/* is a directory it will change to that directory; if it is a file (or company_applications is empty) it will get an error.
Perhaps everything is running correctly except that because of the above your ls -l is not running in the directory you expect? Try removing the cd and changing it to ls -l $DIR.
It also seems very strange to me that you are zipping up content from a backup directory into an applications directory. Perhaps you meant to be doing:
zip -r "$BACKUPDIR/`basename $i`-$NOW" $i
could you try this;
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR
do
base=$(basename "$i")
zip -r $BACKUPDIR/$base-${NOW}.zip $i
done
ls -l $BACKUPDIR
echo "Done!"

Script to download, gunzip merge files and gzip the fil again?

My script skills are really limited so I wonder if someone here could help me. I would like to download 2 files, run gunzip, run a command called tv_merge and then gzip the new file. Here's what I Would like to be run from a script.
I would like to download two files (.gz) with wget:
wget -O /some/where/file1.gz http://some.url.com/data/
wget -O /some/where/file2.gz http://some.url.com/data/
Then gunzip the 2 files:
gunzip /some/where/file1.gz
gunzip /some/where/file2.gz
After that run a command called Tv_merge:
tv_merge -i /some/where/file1 -m /some/where/file2 -o newmaster.xml
After tv_merge. I would like to gzip the file:
gzip newmaster.xml
I would like to run all these commands in that order from a script, and I would like to put that to be run let's see every 8h like a crontab.
I'm assuming that file names are static. with provided information this should get you going.
#!/bin/bash
echo "Downloading first file"
wget -O /some/where/file1.gz http://some.url.com/data/
echo "First Download Completed"
echo "Downloading Second file"
wget -O /some/where/file2.gz http://some.url.com/data/
echo "Second Download Completed"
gunzip /some/where/file1.gz
gunzip /some/where/file2.gz
echo "Running tv_merge"
tv_merge -i /some/where/file1 -m /some/where/file2 -o newmaster.xml
gzip -c newmaster.xml > /some/where/newmaster.xml.gz
echo "newmaster.xml.gz is ready at /some/where/newmaster.xml.gz"
Save this to a file for example script.sh then chmod +x script.sh and you can run it with bash script.sh.

Biased behavior of bash script

I have 8 folders namely HCIVR5,HCIVR6,HCIVR7,HCIVR8,IVR5,IVR6,IVR7,IVR8.
I wrote down this code first
ls -d IVR* > temporary.txt
Saved this code in a file called run. then executed it using ./run. When i tried opening the file(using cat and normal opening through click), it said "cannot access temporary.txt.
This is my entire code
ls -d IVR* > temporary.txt
while read ivr_line;do echo $ivr_line;cd $ivr_line;./ivr_alarm;cd ..;done < temporary.txt
ls -d HCIVR* > temporary.txt
while read hcivr_line;do echo $hcivr_line;cd $hcivr_line;./hcivr_alarm;cd ..;done < temporary.txt
Now this program works for IVR5,6,7,8. After that i get the error
./run: line 4: temporary.txt: No such file or directory
even when i comment the first two lines i get the same error
code for ivr_alarm is this
mkdir TEMP
day=`date +%d-%m-%Y`
read ip<ip.txt
read ivr_number<ivr_number.txt
while read line;do scp -i ivr"$ivr_number"_key -r root#$ip:${line%?} ./TEMP/ ;done < files_needed.txt
mv ./TEMP/.bash_history ./LOGS/.bash_history_"$day"
cd TEMP
for file in *;do
mv $file ../LOGS/${file%.*}_"$day".${file##*.}
done
cd ..
rmdir TEMP
code for hcivr_alarm is this
day=`date +%m-%d-%Y`
read ip<ip.txt
read ivr_number<ivr_number.txt
rm alarmtest
rm ./LOGS/HEALTH_CHECK_$day.log
while read line;do
echo "ssh -i hcivr"$ivr_number"_key root#$ip '${line%?}'>>./LOGS/HEALTH_CHECK_$day.log" >> alarmtest
done < commands.txt
chmod 700 alarmtest
./alarmtest
and for alarmtest(for HCIVR5, key changes to hcivr6_key for HCIVR6 and so on) is this
ssh -i hcivr5_key root#172.19.66.137 'ocstatus'>>./LOGS/HEALTH_CHECK_04-25-2012.log
ssh -i hcivr5_key root#172.19.66.137 'ocmp-bre status'>>./LOGS/HEALTH_CHECK_04-25-2012.log
Now this program works for IVR5,6,7,8. After that i get the error
./run: line 4: temporary.txt: No such file or directory
even when i comment the first two lines i get the same error. Where am i going wrong . Because when i manually open all the HCIVR folders and run ./hcivr_alarm everything works fine
Try this:
ls -d IVR* > temporary.txt
while read ivr_line;do echo $ivr_line;pushd $ivr_line;./ivr_alarm;popd;done < temporary.txt
ls -d HCIVR* > temporary.txt
while read hcivr_line;do echo $hcivr_line;pushd $hcivr_line;./hcivr_alarm;popd;done < temporary.txt
I can't see how, but I think you're screwing up your current directory, somehow.

Resources