Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
scenario is -
In the Location /opt/data/ there are 10 difference files with name ending with previous day
eg - file_1_20160627
I need to check whether these 10 files exist or not.
If one exists, I need to show output - "OK - file_1_20160627 exist" and write output in /tmp/report.txt file
If a file does not exist, I want the same as above - "Failed - file_1_20160627 not exist" and write output in the same /tmp/report.txt file
Every day when the script runs, content on that file must replace.
I tried to write but I'm not good in scripting. below script only for 4 files. I think so many things need to be change.
appreciate someone help me to create this script.
#!/bin/bash
now=`date +%Y%m%d%H%M%S`
time=`date +%H%M`
week=`date +%a`
/bin/rm -f /tmp/report.txt
if [ "$time" -ge 1300 ] && [ $time -lt 2359 ]; then
if [ "$week" == Sun ]; then
if [ -f "find /opt/data/ -type f -name "file_1_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_1 file does exist" >> /tmp/report.txt
else
echo "Failed - file_1 file does not exist." >> /tmp/report.txt
fi
if [ -f "find /opt/data/ -type f -name "file_2_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_2 file exist." >> /tmp/report.txt
else
echo "Failed - file_2 file does not exist" >> /tmp/report.txt
fi
else
fi
if [ -f "find /opt/data/ -type f -name "file_3_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_3 file exist" >> /tmp/report.txt
else
echo "Failed - file_3 file does not exist" >> /tmp/report.txt
fi
if [ -f "find /opt/data/ -type f -name "file_4_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_4 file exist" >> /tmp/report.txt
else
echo "Failed - file_4 file does not exist" >> /tmp/report.txt
fi
else
fi
PREFIX="/opt/data"
REPORT="/tmp/report.txt"
DATE=$( date +%Y%m%d )
rm "$REPORT"
for i in `seq 1 10`;
do
FILENAME="file_${i}_${DATE}"
FULLFN="$PREFIX/$FILENAME"
if [ -f "$FULLFN" ]; then
echo "OK - $FILENAME exists" >> $REPORT
else
echo "Failed - $FILENAME do not exist" >> $REPORT
fi
done
Related
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I'm trying to compare dates from a file and the current day.
The script should run every day and if the dates do not compare it should give a warning.
I made a test file with a 2015 date, but it keeps saying its "equal" to the current date.
#!/bin/bash
today= date +"%m-%d-%y"
filedate= date +"%m-%d-%y" -r fileName.txt
if [ $today == $filedate ];
then
echo $today;
echo $filedate;
echo 'Backup OK';
else
echo $today;
echo $filedate;
echo 'Backup ERROR';
fi
find utility is your friend.
modified=$(find fileName.txt -mtime -1)
if [[ -n $modified ]]; then
echo OK
else
echo ERROR
fi
As a piece of advice you may have to read carefully what atime and ctime means to the system time.
You can assign the date output to the today and filedate variables using command substitution. And you'd better double quote your variables in your comparison test:
today=$(date +"%m-%d-%y")
filedate=$(date +"%m-%d-%y" -r fileName.txt)
echo $today
echo $filedate
if [ "$today" == "$filedate" ];
then
echo $today;
echo $filedate;
echo 'Backup OK';
else
echo $today;
echo $filedate;
echo 'Backup ERROR';
fi
This works for me.
today=$(date +"%m-%d-%y")
filedate=$(date +"%m-%d-%y" -r fileName.txt)
echo $today
echo $filedate
if [ "$today" == "$filedate" ];
then
echo $today;
echo $filedate;
echo 'Backup OK';
else
echo $today;
echo $filedate;
echo 'Backup ERROR';
fi
I created this script for check whether specific files exist or not in the given location. but when I run this its always showing
Failed - Flag_lms_device_info_20160628.txt do not exist
Failed - Flag_lms_weekly_usage_info_20160628 do not exist
but both files are existing.
PREFIX="/opt/data"
REPORT="/tmp/report.txt"
DATE=$( date -d "${dtd} -1 days" +'%Y%m%d' )
rm -f "$REPORT"
FILENAME="Flag_lms_device_info_${DATE}.txt"
FULLFN="$PREFIX/$FILENAME"
if [ -f "$FULLFN" ]; then
echo "OK - $FILENAME exists" >> $REPORT
else
echo "Failed - $FILENAME do not exist" >> $REPORT
fi
FILENAME="Flag_lms_weekly_usage_info_${DATE}.txt"
FULLFN="$PREFIX/$FILENAME"
if [ -f "$FULLFN" ]; then
echo "OK - $FILENAME exists" >> $REPORT
else
echo "Failed - $FILENAME do not exist" >> $REPORT
fi
First of all, you have strange output in your question: your second line of output lacks a .txt extension. This might be an accident but if it's not it's worth investigating.
Assuming your date command is working correctly (I don't know that particular command), I would reduce your use of variables. In addition, I would use the -e test operator instead of -f because it's more inclusive. (If you haven't put data in the files yet, -f could return an error even if the file exists.) :
REPORT="/tmp/report.txt"
DATE=$( date -d "${dtd} -1 days" +'%Y%m%d' )
echo "" > "$REPORT" # Wipes file instead of completely removing it
filename="/opt/data/Flag_lms_device_info_$DATE.txt"
if [ -e "$filename" ]; then
echo "OK - Flag_lms_device_info_$DATE.txt exists" >> $REPORT
else
echo "Failed - Flag_lms_device_info_$DATE.txt doesn't exist" >> $REPORT
fi
filename="/opt/data/Flag_lms_weekly_usage_info_$DATE.txt"
if [ -e "$filename" ]; then
echo "OK - Flag_lms_weekly_usage_info_$DATE.txt exists" >> $REPORT
else
echo "Failed - Flag_lms_weekly_usage_info_$DATE.txt doesn't exist" >> $REPORT
fi
if [ -f "find "$FULLFN" -type f -name "$FILENAME"" ];then
Here you check for existance of a strange file named find... Use backquotes
if [ -f `find "$FULLFN" -type f -name "$FILENAME"` ];then
or, in bash,
if [ -f $(find "$FULLFN" -type f -name "$FILENAME") ];then
to get the command's output as a string.
Furthermore, your find invocation does not look promising. If you need to find a file named Flag_lms_device and so forth somewhere under /opt/data/, use find "$PREFIX" -type f -name "$FILENAME". If you know for sure that /opt/data is the exact location, then use
if [ -f "$FULLFN" ]
and you don't need to find the file.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I keep getting these errors running my script and i just cannot work it out...
the error that keeps coming up is;
rm: cannot remove ~/my-documents/article:': Is a directory. The directory its referring to is $2...here is my script.
#! /bin/sh
SRC=$1
DES=$2
if [ $# -ne 2 ]; then
echo "1. Please enter the source directory"
echo "2. Please enter the destination directory"
echo "thankyou"
exit
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory please enter a valid directory"
echo "thankyou"
exit
fi
#gives the user a error warning the source directory is invalid
if [ -d $2 ]; then
echo "output directory exists"
else
echo "Output directory does not exist, creating directory"
mkdir $2
fi
#creates the destination directory if one doesn't exist
IFILE=$GETFILES;
FINDFILE=$FINDFILE;
find $1 -name "*.doc" > FINDFILE
find $1 -name "*.pdf" > FINDFILE
find $1 -name "*.PDF" > FINDFILE
#finds doc, pdf & PDF files and sends data to findfile.
while read -r line;
do
cp $line $2
done < FINDFILE
#files read and copied to destination directory
IFILE=$2/$GETFILES;
ls -R $1 | egrep -i ".doc | .pdf" > IFILE;
LCOUNT=0
DIFFCOUNT=0
FOUND=0
ARCHIVE=1
BASE="${line%.*}"
EXTENSION="${line##*.}"
COUNT=$COUNT;
ls $2 | grep ${line%%.*} \; | wc -l
if [[ $COUNT -eq 0 ]];
then
cp $1/$line $2;
else
echo "there is already a file in the output so need to compare"
COMP=$2/$line
fi
while [[ $FOUND -eq 0 ]] && [[ $LCOUNT -lt $COUNT ]];
do
echo "diffcount is $DIFFCOUNT"
###compares the file from the input directory to the file in
###the output directory
if [ $DIFFCOUNT -eq 0 ];
then
echo "file has already been archived no action required"
FOUND=$FOUND [ $FOUND+1 ]
else
LCOUNT=$LCOUNT [ $LCOUNT+1 ]
COMP="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION"
echo "line count for next compare is $LCOUNT"
echo "get the next file to compare"
echo "the comparison file is now $COMP"
fi
if [ $LCOUNT -ne $COUNT ]; then
ARCHIVE=$ [ $ARCHIVE+1 ]
else
ARCHIVE=0
fi
if [ $ARCHIVE -eq 0 ];
then
NEWOUT="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION";
echo "newfile name is $NEWOUT"
cp $1/$LINE $NEWOUT
fi
done < $IFILE
rm $IFILE
OFILE=$2/DOCFILES;
ls $2 | grep ".doc" > $OFILE;
while read -r line;
do
BASE=${line%.*}
EXTENSION=${line##*.}
NEWEXTENSION=".pdf"
SEARCHFILE=$BASE$NEWEXTENSION
find $2 -name "$SEARCHFILE" -exec {} \;
done < $OFILE
rm $OFILE
### this will then remove any duplicate files so only
### individual .doc .pdf files will exist
a plain call to rm can only remove files, not directories.
$ touch /tmp/myfile
$ rm /tmp/myfile
$ mkdir /tmp/mydir
$ rm /tmp/mydir
rm: cannot remove ‘/tmp/mydir/’: Is a directory
You can remove directories by specifying the -d (to delete empty directories) or the -r (to delete directories and content recursively) flag:
$ mkdir /tmp/mydir
$ rm -r /tmp/mydir
$
this is well described in man rm.
apart from that, you seem to ignore quoting:
$ rm $OFILE
might break badly if the value of OFILE contains spaces, use quotes instead:
$ rm "${OFILE}"
and never parse the output of ls:
ls $2 | grep ".doc" > $OFILE
(e.g. if your "$2" is actually "/home/foo/my.doc.files/" it will put all files in this directory into $OFILE).
and then you iterate over the contents of this file?
instead, just use loop with file-globbing:
for o in "${2}"/*.doc
do
## loop code in here
done
or just do the filtering with find (and don't forget to call an executable with -exex):
find "$2" -name "$SEARCHFILE" -mindepth 1 -maxdepth 1 -type f -exec convertfile \{\} \;
This code is that I want to give two directory and this code will look if these two directory contains two files that contains the same information, and asks me which file I want to delete .
I have written this code but I don't know how to write the code that will delete the file , please help
#!bin/bash
echo "give the first directory"
read firstdir
echo "give the second directory"
read seconddir
for i in ` ls $firstdir`
do
echo $i
t= `md5sum $firstdir/$i`
s= `md5sum $seconddir/$i`
if [ "$t" ! = "$s" ]
then
echo " of which directory will be eliminated? $i"
read direct
( here I want the code to delete the directory ex : delete direct/i )
fi
done
Replace:
echo " of which directory will be eliminated? $i"
read direct
( here I want the code to delete the directory ex : delete direct/i )
With:
echo "of which directory will be eliminated? $i:
1)$firstdir
2)$seconddir
"
read -p "(1/2)" direct
case $direct in
1)
rm -v $firstdir/$i
;;
2)
rm -v $seconddir/$i
;;
*)
echo "ERROR: bad value, 1 or 2 expected!" ; exit 1
esac
Ok, try this. I just made my own solution based on your requirements. I hope you like it. Thanks
#!/bin/bash
# check for a valid first directory
while true
do
echo "Please, enter the first directory"
read FIRST_DIR
if [ -d $FIRST_DIR ]; then
break
else
echo "Invalid directory"
fi
done
# check for a valid second directory
while true
do
echo "Please, give the second directory"
read SECOND_DIR
if [ -d $SECOND_DIR ]; then
break
else
echo "Invalid directory"
fi
done
for FILE in `ls $FIRST_DIR`
do
# make sure that only files will be avaluated
if [ -f $FILE ]; then
echo $SECOND_DIR/$FILE
# check if the file exist in the second directory
if [ -f $SECOND_DIR/$FILE ]; then
# compare the two files
output=`diff -c $FIRST_DIR/$FILE $SECOND_DIR/$FILE`
if [ ! $output ]; then
echo "Which file do you want to delete?
1)$FIRST_DIR/$FILE
2)$SECOND_DIR/$FILE
"
# read a choice
read -p "(1/2)" choice
# delete the chosen file
case $choice in
1)
rm -v $FIRST_DIR/$FILE
;;
2)
rm -v $SECOND_DIR/$FILE
;;
*)
echo "ERROR invalid choice!"
esac
fi
else
echo "There are no equal files in the two directories."
exit 1
fi
else
echo "There are no files to be evaluated."
fi
done
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Below script is to push file to remote location through sftp,i faced lot of issues to write below code.But still i am facing some issue,Please guid me to resolve the issues.It's not working with sh.it is only working with ksh.
#test script
#-------------------------------------------------------------------
#!/bin/sh
#------------------------------------------------------------------------
# sftp_file_uploads.sh
#------------------------------------------------------------------------
export REMOTE_SERVER_PROD='192.168.0.1'
export REMOTE_SERVER_FAILOVER='192.168.0.2'
export SFTP_PORT='0001'
export SOURCE_FUNCTIONAL_ID='testusr'
export SOURCE_FILE_DIRECTORY='/var/temp/files/'
export SOURCE_ARCHIVE_DIRECTORY='/var/temp/files/archive'
export DATE_FORMAT=`date "+%Y%m%d"`
export LOG_DIRECTORY='/var/temp/logs'
export DESTINATION_FILE_DIRECTORY='/dest'
export LOG_FILE='$LOG_DIRECTORY/test_$DATE_FORMAT.log'
export SFTP_BATCH_FILE='/var/tmp/SFTP_BATCH_FILE'
#------------------------------------------------------------------------
# Find if the files are available at the source directory.
#------------------------------------------------------------------------
cd $SOURCE_FILE_DIRECTORY
export FILE_TO_UPLOAD_TESTD=`ls -lrt TESTD$DATE_FORMAT.csv | awk '/TESTD/{ f=$NF };END{ print f }'`
export FILE_TO_UPLOAD_TESTDF=`ls -lrt TESTDF$DATE_FORMAT.csv | awk '/TESTDF/{ f=$NF };END{ print f }'`
#------------------------------------------------------------------------
# Try 2 times and Sleep for 5 mins if either of the files is not present
#------------------------------------------------------------------------
counter=0
flag_file_found_TESTD=0
flag_file_found_TESTDF=0
while [ $counter –lt 2 ]
do
#---------------------------
# Check TESTD file arrived
#---------------------------
if [ -z $FILE_TO_UPLOAD_TESTD ] then
echo “No TESTD file to transfer. Sleeping for 5 mins” >> $LOG_FILE
sleep 300
else
echo “TESTD file found to transfer.” >> $LOG_FILE
flag_file_found_TESTD=1
fi
#---------------------------
# Check TESTDF file arrived
#---------------------------
if [ -z $FILE_TO_UPLOAD_TESTDF ] then
echo “No TESTDF file to transfer. Sleeping for 5 mins” >> $LOG_FILE
sleep 300
else
echo “TESTDF file found to transfer.” >> $LOG_FILE
flag_file_found_TESTDF =1
fi
if [[ flag_file_found_TESTD == 1 &&
flag_file_found_TESTDF == 1 ]] then
echo “Both files are found.” >> $LOG_FILE
break
else
echo “At least one of the files is not found. Retrying now.” >> $LOG_FILE
fi
counter=`expr $counter + 1`
done
if [[ flag_file_found_TESTD == 1 &&
flag_file_found_TESTDF == 1 ]] then
echo “Both files are found.”
break
else
if [ flag_file_found_TESTD == 0 ] then
echo “test file is not found and two attempts completed. Cannot transfer the file for today.” >> $LOG_FILE
fi
if [flag_file_found_TESTDF == 0 ] then
echo “test1 file is not found and two attempts completed. Cannot transfer the file for today.” >> $LOG_FILE
fi
fi
#------------------------------------------------------------------------
# Create sftp script
#------------------------------------------------------------------------
rm -f $SFTP_BATCH_FILE
echo "lcd $SOURCE_FILE_DIRECTORY " > $SFTP_BATCH_FILE
echo "cd $DESTINATION_FILE_DIRECTORY " >> $SFTP_BATCH_FILE
if [ -z $FILE_TO_UPLOAD_TESTD ] then
echo "put $FILE_TO_UPLOAD_TESTD " >> $SFTP_BATCH_FILE
fi
if [ -z $FILE_TO_UPLOAD_TESTDF ] then
echo "put $FILE_TO_UPLOAD_TESTDF " >> $SFTP_BATCH_FILE
fi
echo "bye" >> $SFTP_BATCH_FILE
#------------------------------------------------------------------------
# Do sftp
#------------------------------------------------------------------------
echo " Before SFTP " >> $LOG_FILE
if [[ -z $ FILE_TO_UPLOAD && -z $ FILE_TO_UPLOAD1 ]] then
echo “No files to transfer” >> $LOG_FILE
mv $LOG_FILE $LOG_DIRECTORY
exit 1
else
echo “Attempting to connect to Remote Server $REMOTE_SERVER_PROD” >> $LOG_FILE
/usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID#$REMOTE_SERVER_PROD >> $LOG_FILE 2 >> $LOG_FILE
fi
result=$?
errorConnectToProd=0
if [ $result -eq 0 ]
then
echo "SFTP completed successfully to Prod Remote Server" >> $LOG_FILE
else
errorConnectToProd=1
if [[ $result -eq 4 || $result -eq 5 ]]
echo "FAILED to connect to Server. " >> $LOG_FILE
else
echo "FAILED to SFTP to Remote Server. " >> $LOG_FILE
fi
fi
if [ errorConnectToProd == 1 ] then
echo “Attempting to connect to FAILOVER Remote Server $REMOTE_SERVER_FAILOVER” >> $LOG_FILE
/usr/bin/sftp –v -oPort=$SFTP_PORT -b $SFTP_BATCH_FILE $SOURCE_FUNCTIONAL_ID#$REMOTE_SERVER_FAILOVER >> $LOG_FILE 2 >> $LOG_FILE
fi
result=$?
if [ $result -eq 0 ]
then
echo "SFTP completed successfully to Failover Remote Server" >> $LOG_FILE
else
echo "FAILED to SFTP to Failover Remote Server. " >> $LOG_FILE
mv $LOG_FILE $LOG_DIRECTORY
exit 1
fi
fi
cd $SOURCE_FILE_DIRECTORY
mv $FILE_TO_UPLOAD_TESTD $SOURCE_ARCHIVE_DIRECTORY
echo “Moved $FILE_TO_UPLOAD_TESTD to archive direcotry.” >> $LOG_FILE
mv $FILE_TO_UPLOAD_TESTDF $SOURCE_ARCHIVE_DIRECTORY
echo “Moved $FILE_TO_UPLOAD_TESTDF to archive direcotry.” >> $LOG_FILE
rm -f $SFTP_BATCH_FILE
echo “Deleted the SFTP Batch file.” >> $LOG_FILE
echo “Upload completed.” >> $LOG_FILE
mv $LOG_FILE $LOG_DIRECTORY
exit 0
Getting below Errors:
test.ksh[41]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[55]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[56]: flag_file_found_TESTDF: not found
test.ksh[65]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[41]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[55]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[56]: flag_file_found_TESTNDF: not found
test.ksh[65]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[79]: [flag_file_found_TESTDF: not found
rm: /var/tmp/SFTP_BATCH_FILE is a directory
test.ksh[89]: /var/tmp/SFTP_BATCH_FILE: cannot create
test.ksh[90]: /var/tmp/SFTP_BATCH_FILE: cannot create
test.ksh[97]: B: not found
test.ksh[98]: B: not found
test.ksh[99]: B: not found
test.ksh[100]: B: not found
test.ksh[101]: B: not found
test.ksh[102]: B: not found
test.ksh[106]: /var/tmp/SFTP_BATCH_FILE: cannot create
test.ksh[113]: $LOG_DIRECTORY/test_$DATE_FORMAT.log: cannot create
test.ksh[114]: syntax error at line 114 : `FILE_TO_UPLOAD' unexpected
Regards,
Chai
This line is wrong:
export LOG_FILE='$LOG_DIRECTORY/test_$DATE_FORMAT.log'
It should use double quotes, so that the variables will be expanded:
export LOG_FILE="$LOG_DIRECTORY/test_$DATE_FORMAT.log"
Another error:
if [flag_file_found_TESTDF == 0 ] then
needs a space after [. [ is a command (it's a synonym for test), and all commands are separated from their arguments by spaces.
The whole section labeled "Create sftp script" is failing because /var/tmp/SFTP_BATCH_FILE already exists and is a directory; rm -f won't delete a directory, you need to use rm -rf.
if [[ flag_file_found_TESTD == 1 &&
flag_file_found_TESTDF == 1 ]] then
is missing the $ before the variable names.
if [[ -z $ FILE_TO_UPLOAD && -z $ FILE_TO_UPLOAD1 ]] then
Get rid of the space after $.
UPDATE 2:
In all your if statements, you're missing the ; (or newline) before then.
I'm not sure what's causing all the "B: not found" errors. But after you fix all the other errors, maybe it will go away or be easier to find.