some strange problem with return data in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting some strange problem with return data in script
# 1  
Old 01-04-2012
some strange problem with return data in script

Hi,
I have come across a little strange problem in my script. i tried the same logic in some other script where its working fine. but in other script its not behaving as expected.
Here is the code:
Code:
#!/bin/bash

run_count="2"    
catalog_name="demo"
cd /root/Test_project/Files/$catalog_name
for file in $run_count"_"*.csv; do
 echo $file
 file_count=`grep -c "." $file`
 echo $file_count"file_count \n"
 FILE_NAME=$run_count"_"$catalog_name"_Row_Count.prop"
 PROP_KEY=$file
 Count_file=$(awk -F"=" '{ ()print $1; }' $FILE_NAME )
 echo "file count= "$Count_file
 Count=`cat ${FILE_NAME} | grep ${PROP_KEY} | cut -d'=' -f2`

 echo $Count  #[Here its working fine, its showing the correct values which is returned from top]
 echo $Count"hi" #[here if i'm printing something with it then the value of $Count is lost. and its only printing hi] don't know why
 
#and based on that furhter logic is not working which is under if

if [ "$Count" == "$file_count" ]; then
 echo "column header count row proper"
 else
 echo "Error send a mail as count is not correct in "$file
 exit 1
fi
done
exit 0

My question is this
1. Why the value is lost of $Count when am trying to print something with it.
2. Is there any problem of data type?
3. What type of datatype is returned by cut command in above?
Here I need to return integer and then to compare them
Count is the number of files, which is specified in a property file from where its picking in above case.
file_count is number of rows in a file.

I need to compare them both and then have to take action further.
Help me out if there is something wrong with data type or logic or something else.

Thanks,
# 2  
Old 01-04-2012
Try in this way ..
Code:
$ echo $a
1
$ echo "${a}hi"
1hi
$

And also if both $Count and $file_count values are integers, then use -eq comparator.
Code:
if [ "$Count" -eq "$file_count" ]; then

# 3  
Old 01-04-2012
Reference post #1.
It is quite possible that $Count contains a trailing carriage-return character (ctrl/M) such that the "hi" overwrites the number on the screen. This will also mess up your "if" test.
If the data file $FILE_NAME was created on a Microsoft platform, this is quite likely.

Quick fix (though it would be better to fix the data file):
Code:
Count=`cat ${FILE_NAME} | grep ${PROP_KEY} | cut -d'=' -f2 | tr -d '\r'`

# 4  
Old 01-31-2012
Quote:
Originally Posted by methyl
Reference post #1.
It is quite possible that $Count contains a trailing carriage-return character (ctrl/M) such that the "hi" overwrites the number on the screen. This will also mess up your "if" test.
If the data file $FILE_NAME was created on a Microsoft platform, this is quite likely.

Quick fix (though it would be better to fix the data file):
Code:
Count=`cat ${FILE_NAME} | grep ${PROP_KEY} | cut -d'=' -f2 | tr -d '\r'`

thanks @methyl it works like a charm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Shell Programming and Scripting

Strange problem with shell script commands

i have a very strange problem.....i gave the following command on the terminal sed '15 w temp' f1 -n | temp1=$(awk '{printf $3}' temp) | sed '/'$temp1'/s/1.330000/1.232032/' f1 > f10 where f1, temp and f10 are filenames. This works sometimes and the same command doesn't work sometimes.... (2 Replies)
Discussion started by: Agastya
2 Replies

3. Shell Programming and Scripting

Script problem with deleting data

The 10.Delete data doesnt work at all, plesase anyone could help on that. When I choose options 10 to delete a record it only copy the selected data on the other file dbs1 but doesnt delete it from the database where other records are. #! /bin/bash i="y" echo "Enter name of database " read db... (6 Replies)
Discussion started by: Lina_14
6 Replies

4. Shell Programming and Scripting

Strange Problem

Hi All, I am encountering an unexpected problem while running my process, using daemon placed in /etc/init.d/. I have a process that is opening shared library using dlopen. When I run my process on the command line, it is able to open the library. But when I used daemon to run my process, I... (0 Replies)
Discussion started by: qwerty-1
0 Replies

5. Shell Programming and Scripting

Problem - script that parses $date data...

This is probably archaic, but I'm new to unix and this is my first shell script. I'm writing this script to use in another script. All I am trying to do is make the parts of the output from date usable in my other script. I was wondering if you could stand looking at this and see if you notice... (8 Replies)
Discussion started by: Dalcron
8 Replies

6. Shell Programming and Scripting

Strange problem.

Well, my script started off to do what i wanted. Now, i think its not recognizing the pattern so its not moving anything. What i have to do is execute my script command for the move to take effect. So i did that and yayy it worked. Strange thing is that my DESTDIR was empty to begin with.... (2 Replies)
Discussion started by: oxoxo
2 Replies

7. Shell Programming and Scripting

How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code: x="O1164885.DAT" y=`ls -ltr *${x}|awk '{print substr($0,3)}'` echo ${x}|awk '{print substr($0,3)}' echo "y="$y I can echo it to the screen just fine but I... (3 Replies)
Discussion started by: ttunell
3 Replies

8. Shell Programming and Scripting

Perl script strange problem

Hi All, I have one strange problem in perl script. Bellow is the contents of the script i am trying to write: #!/usr/local/bin/perl my $module_list = ''; my @running_module; my $count=0; my $flag=0; my $ccprojecthome=$ENV{CCPROJECTHOME}; @module_list = `cat... (2 Replies)
Discussion started by: vaibhav
2 Replies

9. Linux

very strange problem

I have installed Fedora Core on a Toshiba Satellite Pro4600 laptop recently I have experienced a rather mysterious problem if I touch anything specially the keyboard or mouse I see this stuff “67yujhnmyyy” straight away some time it won't stop for while like this... (5 Replies)
Discussion started by: kemobyte
5 Replies

10. UNIX for Dummies Questions & Answers

Strange Problem

I did an installation of Redhat 7.1 and it seem to install smoothly. I go and reboot and I get the following message: "INIT: Id "x" respawing too fast: disabled for 5 minutes" The screen flashes 4 times every 10 seconds or so. I can't seem to figure why this does this. It might be a simple... (2 Replies)
Discussion started by: forjohnny
2 Replies
Login or Register to Ask a Question