The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
simple script rajivn786 Shell Programming and Scripting 5 11-30-2008 11:26 PM
Simple Script mojoman Shell Programming and Scripting 6 05-12-2008 03:55 AM
simple script help ali560045 Shell Programming and Scripting 4 01-24-2008 05:24 AM
I need a simple script mehmetned Shell Programming and Scripting 2 02-10-2007 02:16 AM
simple awk script... moxxx68 Shell Programming and Scripting 3 01-24-2005 04:17 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-07-2009
ali560045's Avatar
ali560045 ali560045 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 328
Simple script

I have a script that will check for integer line by line and if it encounter any blank space will echo it:

Below the script:
Code:

#!/bin/ksh
while read i
do
echo "Value is $i"

count=`expr substr "$i" 1 3`
echo $count

if [ $count == 000 ] && [ "$i" != " " ]
then
echo "Matched"
else
echo "Blank Space Found"
fi

done < a2.txt
contents of a2.txt
Quote:
0001650111
<--------Blank Line-------->
----------------------------

having provlem when the script encounter the blank line in a2.txt.
Below the output:
Quote:
Value is 0001650111
000
Matched
Value is

./a9.sh[9]: test: argument expected
Blank Space Found

Last edited by ali560045; 01-07-2009 at 02:07 AM..
  #2 (permalink)  
Old 01-07-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ali560045 View Post
I have a script that will check for integer line by line and if it encounter any blank space will echo it:

Below the script:
Code:

#!/bin/ksh
while read i

If a line begins or ends with a space, that read command will strip it, and it will not be part of $i.

To capture leading and trailing spaces, set IFS to en empty string:

Code:
while IFS= read i
Quote:
Code:
do
echo "Value is $i"

count=`expr substr "$i" 1 3`

In a POSIX shell, there is no need to use expr

Code:
temp=${i#???}
count=${i%"$temp"}
Quote:
Code:
echo $count

if [ $count == 000 ] && [ "$i" != " " ]

The == operator is not standard; use =

The second test in unnecessary. If $count is 000, it cannot also be a space.

Better still, use a case statement:

Code:
case $i in
     000*) count=000;
esac
Code:
Quote:
then echo "Matched" else echo "Blank Space Found" fi done < a2.txt
Quote:

contents of a2.txt


----------------------------

having provlem when the script encounter the blank line in a2.txt.
Below the output:
Code:
Value is 0001650111
000
Matched
Value is

./a9.sh[9]: test: argument expected
Blank Space Found Value is 0001650111 000 Matched Value is ./a9.sh[9]: test: argument expected Blank Space Found
  #3 (permalink)  
Old 01-07-2009
ali560045's Avatar
ali560045 ali560045 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 328
still having the same problem not reading blank line.....below the script


Code:

#!/bin/ksh
while IFS= read i
do
echo "Value is $i"

#count=`expr substr "$i" 1 3`
#echo $count

temp=${i#???}
count=${i%"$temp"}
echo "$count"
if [ "$count" = "   " ]
then
echo "Blank Space Found"
else
echo "Value found"
fi


done < a2.txt
output:
Quote:
Value is 0001650111
000
Value found
Value is

Value found

Last edited by ali560045; 01-07-2009 at 05:43 AM..
  #4 (permalink)  
Old 01-07-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ali560045 View Post
still having the same problem not reading blank line.....below the script

You haven't tested for a blank line. You have tested for a line beginning with 3 spaces.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0