File values alwaya null


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File values alwaya null
# 1  
Old 06-04-2013
File values alwaya null

Hi All ,

below is my shell program.

Code:
!/bin/sh
set -x
#----------------------------------------------------------------------------------------
# Program        :  weekly_remove_icd_file.sh
# Author         :
# Date           :  04/06/2013
# Purpose        :  Execute the script to remove five days older files from archive directory
#-------------------------------------------------------------------------------------------

OUTARCHDIR=/projects/psoft/twest/archive

#cd ${OUTARCHDIR}
echo "Checking five days Older files"
find . -type f -name '*.TXT' -print|wc -l >total_files
#find . -type f -name '*.TXT' -mtime +5 -print|wc -l >total_files
if [ $total_files -gt 0 ]
then
echo " removing five days older files "
##remove command
#echo 'Total number files :- $total_files ' | mailx -s "Removed five days older files" krupa.@kk.com
cat <<-EOF | mailx -s " removed five days older files from archive " krupa.behera@alcatel-lucent.com
"Total No of files :- cat`$total_files`"
EOF
else
#echo "There is no files"
#echo 'Total number files :- $total_files'|mailx -s "xxxremoved older files" krupa.behera@alcatel-lucent.com
cat <<-EOF | mailx -s " Today no files to remove from archive " krupa@kk.com
"Total No of files :- cat`$total_files`"
EOF
fi
exit

My Problem is 'total_files' count always giving null in if block and i don't want to store in to a variable like
total_files=find . -type f -name '*.TXT' -print|wc -l
but when i checking manually cat total_files it is giving value 1

how to get values also in Total No of files :- cat`$total_files`
Please advice me .

Thanks,
Krupa
# 2  
Old 06-04-2013
how can you check the count of file called total_files in if block?
and since you are storing the total file count in a file called total_files just cat total_files will display the count.
I am still unsure why you dont wanna use variable here? I cant see any pat of the code where it should be a file? If you want to be it as a file at the end of the code just echo the variable able store it in file.
# 3  
Old 06-04-2013
Quote:
Originally Posted by krupasindhu18
Hi All ,

below is my shell program.

Code:
!/bin/sh
set -x
#----------------------------------------------------------------------------------------
# Program        :  weekly_remove_icd_file.sh
# Author         :
# Date           :  04/06/2013
# Purpose        :  Execute the script to remove five days older files from archive directory
#-------------------------------------------------------------------------------------------

OUTARCHDIR=/projects/psoft/twest/archive

#cd ${OUTARCHDIR}
echo "Checking five days Older files"
find . -type f -name '*.TXT' -print|wc -l >total_files
#find . -type f -name '*.TXT' -mtime +5 -print|wc -l >total_files
if [ $total_files -gt 0 ]
then
echo " removing five days older files "
##remove command
#echo 'Total number files :- $total_files ' | mailx -s "Removed five days older files" krupa.@kk.com
cat <<-EOF | mailx -s " removed five days older files from archive " krupa.behera@alcatel-lucent.com
"Total No of files :- cat`$total_files`"
EOF
else
#echo "There is no files"
#echo 'Total number files :- $total_files'|mailx -s "xxxremoved older files" krupa.behera@alcatel-lucent.com
cat <<-EOF | mailx -s " Today no files to remove from archive " krupa@kk.com
"Total No of files :- cat`$total_files`"
EOF
fi
exit

My Problem is 'total_files' count always giving null in if block and i don't want to store in to a variable like
total_files=find . -type f -name '*.TXT' -print|wc -l
but when i checking manually cat total_files it is giving value 1

how to get values also in Total No of files :- cat`$total_files`
Please advice me .

Thanks,
Krupa
Why don't you want to do:
Code:
total_files=$(find . -type f -name '*.TXT' -print|wc -l)

This command sets the shell variable total_files to the number of files in and under the current directory whose names end in ".TXT". You can then find that value by expanding the variable using $total_files. The command you are using:
Code:
find . -type f -name '*.TXT' -print|wc -l > total_files

stores the same number in a file named total_files; not in a shell variable. When you store the data in a file, and don't set the shell variable, expanding the (unset) shell variable using $total_files will produce an empty string.

If you set the shell variable as shown above, then to put that number in your mail body, you could just use:
Code:
echo "Total No of files: $total_files" |
    mailx -s "Today no files to remove from archive" krupa@kk.com

Rather than either of these, you'd probably be better off with a while read loop to actually remove the files and adjust the count based on the number of times you successfully removed a file.

Even more common (and much more efficient) would be to remove the files in the find command using a -exec rm {} +, but that wouldn't give you the number of files removed. (It would just tell you if all of the files more than five days old were successfully removed.)
# 4  
Old 06-04-2013
File values alwaya null

Hi Don,

Thanks a lot Smilie

Thanks,
Krupa
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count null values in a file using awk

I have the following a.txt file A|1|2|3|4|5| A||2|3|0|| A|1|6||8|10| A|9|2|3|4|1| A|0|9|3|4|5| A||2|3|4|5| A|0|av|.9|4|9| I use the following command to count null values for 2nd field awk -F"|" '!$2 { N++; next } END {print N}' a.txt It should give the result 2, but it is giving... (2 Replies)
Discussion started by: RJG
2 Replies

2. Shell Programming and Scripting

Fields shifting in file, do to null values?

The below code runs and creates an output file with three sections. The first 2 sections are ok, but the third section doesn't seem to put a . in all the fields that are blank. I don't know if this is what causes the last two fields in the current output to shift to a newline, but I can not seem... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Grep null values in a file with no delimiter

Hi Folks, We have a file that has null values but there are no delimiters. So all columns are considered as a single column. Ex: abc def 123 abcdef1234567 hijklmn7896545 Now from "a" till "3" all are considered as a single column from the first row. Our requirement is like, we... (2 Replies)
Discussion started by: jayadanabalan
2 Replies

4. Shell Programming and Scripting

Replace a field where values are null in a file.

Hi, I've a pipe delimited file and wanted to replace the 3rd field to 099990 where the values are null. How can I achieve it using awk or sed. 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S... (12 Replies)
Discussion started by: rudoraj
12 Replies

5. Shell Programming and Scripting

Find for line with not null values at nth place in pipe delimited file

Hi, I am trying to find the lines in a pipe delimited file where 11th column has not null values. Any help is appreciated. Need help asap please. thanks in advance. (3 Replies)
Discussion started by: manikms
3 Replies

6. Shell Programming and Scripting

How to use sort with null values?

Hello everyone I am doing a join command. Obviously, before I need two files sorted first. ( Both files have headers and have about 2 million lines each one ) The problem is, one of the files has null values in the key to sort (which is the first filed ). For example I have the original... (4 Replies)
Discussion started by: viktor1985
4 Replies

7. UNIX for Advanced & Expert Users

How to Compare Null values??

Hi, Can someone help me comparing Null values. Scenario is as follows: I have a variable which "cache_prd" which can have either some integer or nothing(Null) if it is integer I have to again do some comparision but these comparisons give me this error:( "line 32: [: 95: unary operator... (3 Replies)
Discussion started by: Yagami
3 Replies

8. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies

9. Shell Programming and Scripting

identifying null values in a file

I have a huge file with 20 fileds in each record and each field is seperated by "|". If i want to get all the reocrds that have 18th or for that matter any filed as null how can i do it? Please let me know (3 Replies)
Discussion started by: dsravan
3 Replies

10. Shell Programming and Scripting

Null values after emptying a log file

Hi, I have a log file which is constantly being written to by some process. I need to clear that log file on a daily basis. The problem is that when I issue this command: echo "" > logfile.log the file gets filled with nulls thus increasing the size of the file. Is there a way to... (2 Replies)
Discussion started by: kasie4life
2 Replies
Login or Register to Ask a Question