LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-04-2012, 05:18 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Rep: Reputation: Disabled
some strange problem with return data in sctipt


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,
 
Old 01-04-2012, 08:25 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
The value of $Count is lost because you used an unprotected variable name ($Count"hi" instead of "${Count}hi"). To find out which values get assigned run the script as '/bin/bash -vx /path/to/script'. To compare integers you can use [ $Count == $file_count ] or [ $Count -eq $file_count ].

You should not test scripts as root user and not use unprotected, odd case or inconsistently named variable names.

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NetSnmp exec script return data type hemulll Linux - Networking 8 11-28-2011 07:00 AM
Pseudo Terminal needs return before reading data MrUmunhum Programming 6 11-22-2008 02:10 PM
Serial port return wrong data levka Programming 3 04-13-2007 01:54 AM
Qt dialogs return data Ephracis Programming 7 02-21-2007 11:49 AM
Strange behavior: string within scope return 0 if not within a while loop. RHLinuxGUY Programming 2 08-05-2006 11:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration