#!/usr/bin/ksh
a="Run successfully"
cd $APPS
ls -l *.txt | while read $txt
do
if [grep -q '$txt'] then
cp $APPS/$txt cp $hist/$txt
else
rm $APPS/$txt
echo "Files has been removed"
fi
done
New in shell script please help me out
Around 100 txt files in $APPS dir i want to search pattern from all the the txt which is generated today or t-1 date files if pattern found then copy else move
Sorry not described clearly
1.Directory contained 10 days .txt files want to search pattern'good script' from all the txt files one by one from current date or t-1 date
2.IF Search patern'good script' from any of the txt files then process---- cp $APP/.txt cp $HIST/.txt (which are contained pattern)
3.or Go to else part if patern'good script' not found all the txt files
#!/usr/bin/ksh
a = "good script "
cd $APPS
ls -l *.txt | while read $txt
do
if [grep -q 'a' txt] then
cp $APP/*.txt cp $HIST/*.txt
else
rm $APP/*.txt
echo "Exit successfully"
fi
done
---------- Post updated at 12:18 PM ---------- Previous update was at 12:06 PM ----------
Continue using your script for learning, maybe we can make it work like this:
#!/usr/bin/ksh
pattern="good script"
# Change me to the real directory paths.
APPS="/path/to/apps_directory"
HIST="/path/to/history_directory"
cd $APPS || exit # exist if it cannot cd into $APPS
find . -path './*' -prune -name '*.txt' -type f -mtime -1 | while read filename
do
if [ grep -q "$pattern" "$filename" ]; then
# remove # from cp line if successful.
#cp "$filename" "$HIST/${filename#./}"
# remove the following line if successful.
echo "$filename would had been copied to $HIST/${filename#./}"
else
# remove rm comment if test is successful.
# remove the echo line if successful.
#rm "$filename" && echo "$filename removed"
echo "$filename would had been deleted"
fi
done
Hi Aia,
Thank you for helping Kalia, I think you missed a pair of double-quote characters. With:
pattern="good script"
... ...
if [ grep -q $pattern "$filename" ]; then
the expansion of $pattern needs to be quoted as well (i.e., "$pattern" ). Without the quotes, grep will be looking for the string good in two files (one file named script and the other file named by the expansion of $filename ) instead of looking for the string good script in a single file.
Exit successfully==== file not contained pattern
Exit successfully======file not contained pattern
Exit successfully======file not contained pattern
Exit successfully======file not contained pattern
filename would had been copied to $HIST/${filename#./} filed contained.
But i need output should be display only the true part not false part if any of the files contained pattern then copy if not then go to else part
ex:it should be display only this filename would had been copied to $HIST/${filename#./} filed contained.