check if file is empty or not - unix

How do I check if a file is empty in a korn script
I want to test in my korn script if the output CSV file is empty or not and if it is not empty then it should give the count of values.
Thanks.

The test(1) program has a -s switch:
-s FILE
FILE exists and has a size greater than zero

This is just another way of doing it, albeit a roundabout one:
if [ `ls -l <file> | awk '{print $5}'` -eq 0 ]
then
//condition for being empty
else
//condition for not being empty
fi

if [ ! -f manogna.txt ]
then
echo " Error: manogna.txt does not exist "
else
echo " manogna.txt exist "
echo " no of records in manogna.txt are `cat manogna.txt | wc -l`"
fi

Related

echo does not display proper output

Following code read the test.txt contents and based on first field it redirect third field to result.txt
src_fld=s1
type=11
Logic_File=`cat /home/script/test.txt`
printf '%s\n' "$Logic_File" |
{
while IFS=',' read -r line
do
fld1=`echo $line | cut -d ',' -f 1`
if [[ $type -eq $fld1 ]];then
query=`echo $line | cut -d ',' -f 3-`
echo $query >> /home/stg/result.txt
fi
done
}
Following is the contents of test.txt:
6,STRING TO DECIMAL WITHOUT DEFAULT,cast($src_fld as DECIMAL(15,2) $tgt_fld
7,STRING TO INTERGER WITHOUT DEFAULT,cast($src_fld as integer) $tgt_fld
11,DEFAULT NO RULE,$src_fld
everything works fine except output in result.txt is $src_fld instead of s1. Can anyone please tell me what is wrong in the code?
Try replacing the below line
echo $query >> /home/stg/result.txt
with this one
eval "echo $query" >> /home/stg/result.txt

unix command to redirects output to a file

I am trying to write a unix command which will write/redirects the output to a file i.e. create a file if there is difference in 2 files else it will not create the file.
I am using the below command but it always creates a file(of 0B if no diff), no matter there is any difference in file or not.
diff -u -w a.txt b.txt > diff.tmp
I am trying to write a single unix command that will create file "diff.tmp" if "a.txt" is not equal to "b.txt" else "diff.tmp" will not be created.
Thanks in advance,
Pritish
In bash you could remove it afterwards:
diff -u -w a.txt b.txt > diff.tmp && if [ -f diff.tmp ] && [ ! -s diff.tmp ]; then rm diff.tmp; fi
Note:
-f: to check if the file exits (-e to check if a file, directory, etc. exists)
-s: to check if the file is non-zero
However can will work for text files ..you can use cmp command as well.
cmp a.txt b.txt > cmp.tmp && if [ -f cmp.tmp ] && [ ! -s cmp.tmp ]; then rm cmp.tmp; fi
you can check return code of diff. From man page:
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
So I would write something like:
#!/bin/bash
diff "$1" "$2" 2>/dev/null 1>/dev/null
if [[ $? -eq 0 ]];then
echo "No diff found!"
else
echo "Diff saved in file "$3
diff $1 $2 > $3
fi
And then you call it like
./diff.sh a.txt b.txt diff.tmp
Hope it helps!
Bye
Piero

Unix determine if a file is empty

I am attempting to make a script that will check to see if there is any tyext within a file. I have developed the following script. I have made it check to see if there is exactly 2 arguments, see if the file exists, but I am having trouble checking the file for text within it. The code is as follows:
#!/bin/ksh
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage $0 inputfile outputfile"
exit 1
fi
#check if input file exists
if [ ! -f $1 ]; then
echo "$1 not found!"
exit 1
fi
#Check if input file is null
#This next block of code is where the issue is
if [ grep -q $1 -eq 0 ]; then
echo "$1 must have text within the file"
exit 1
fi
Any help would be appreciated
test's "-s" option checks if the file is empty -- see manual. So your last chunk would become
#Check if input file is null
#This next block of code is where the issue is
if [ ! -s $1 ]; then
echo "$1 must have text within the file"
exit 1
fi
Try using stat
stat -c %s filename

Need to add logic in Unix script convert from Windows to Unix format

I am having one script need to add logic that if some one add file from winscp and do not convert into plain text while transfer. so some time we get some special character (^m) in some value, i wanted to remove them
Here is my code.
cd $HOME_DIR
if [ $SHELL_STEP = 'step2' ]; then
if [ -s $DATA_DIR/$DATA_FILE.txt ]; then echo "The data file is present." cat -v new_reguest.txt $ awk '{ sub("\r$", ""); print }' new_request.txt > new_request.txt echo "Data file $DATA_FILE.txt found in $DATA_DIR directory." >> $LOG_FILE echo "" >> $LOG_FILE STATUS='good' else echo "The data file has not arrived yet." fi
if [ $STATUS = 'bad' ]; then
echo "The data file not found."
echo "The data file not found." >> $LOG_FILE
echo "" >> $LOG_FILE
SHELL_STEP='step5'
else
SHELL_STEP='step3'
fi
fi
I tried to using awk command, but it's not looking good.
please assist.
Most implementations will provide tools like dos2unix or d2u to remove carriage returns from the end of lines. You can use something like:
dos2unix new_request.txt >new_request_2.txt
mv new_request_2.txt new_request.txt
If you don't have the dos2unix command, you can do the same thing with sed:
sed -i 's/\r$//' new_request.txt
The -i is for in-place editing. If your version of sed doesn't have that, you'll have to resort to the same temporary file trick used in the dos2unix code above.

Unix script to delete file if it contains single line

Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it contains single line.
Something like, if 'wc -l abscde.txt' = 1 then rm abscde.txt
My system : Solaris
Here's a simple bash script:
#!/bin/bash
LINECOUNT=`wc -l abscde.txt | cut -f1 -d' '`
if [[ $LINECOUNT == 1 ]]; then
rm -f abscde.txt
fi
delifsingleline () {
if [ $(cat $1 | wc -l) = "1" ]
then
echo "Deleting $1"
echo "rm $1"
fi
}
Lightly tested on zsh. Should work on bash as well.
This is (mostly) just a reformat of Ben's answer:
wc -l $PATH | grep '^1 ' > /dev/null && rm -f $PATH

Resources