simulation of wc command giving problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simulation of wc command giving problems
# 1  
Old 02-18-2009
simulation of wc command giving problems

Hello,
i am developing a simple shell script for simulation of wc command but it's not working.Please tell me what is the problem. I have commented the problematic line

Code:
if [ $# -lt 1 ]
then
	echo "<Improper Usage>"
	echo "wcassg <options> file"
else
	eval file=\$$#
	if [ -f $file ]
	then
		chars=0
		words=0
		lines=0
		if [ $# -eq 1 ]
		then
			exec<$file
			while read sentence
			do
#the line below this is not working i dont know i am not able to use string functions on $sentence
				chars=`expr $chars + \`expr length $sentence\``
				set $sentence
				lines=`expr $lines + 1`
				words=`expr $words + $#`
			done
			echo "Number Of Lines :$lines"
			echo "Number Of Words :$words"
			echo "Number Of Characters :$chars"
		fi
	else
		echo "$file does not exists"
	fi
fi

# 2  
Old 02-18-2009
Quote:
Originally Posted by salman4u
Hello,
i am developing a simple shell script for simulation of wc command but it's not working.Please tell me what is the problem. I have commented the problematic line

Code:
if [ $# -lt 1 ]
then
	echo "<Improper Usage>"
	echo "wcassg <options> file"
else
	eval file=\$$#


That fails if there are more than 9 arguments. Use:

Code:
eval "file=\${$#}"

Quote:
Code:
	if [ -f $file ]
	then
		chars=0
		words=0
		lines=0
		if [ $# -eq 1 ]
		then
			exec<$file
			while read sentence
			do
#the line below this is not working
# i dont know i am not able to use string functions on $sentence


Why can't you use string functions on $sentence?
Quote:
Code:
				chars=`expr $chars + \`expr length $sentence\``


There's no need to use an external command to do integer arithmetic:

Code:
chars=$(( $chars + ${#sentence} ))

Quote:
Code:
				set $sentence


That may fail in various ways, depending on the contents of $sentence. Use:

Code:
set -f
set -- $sentence
set +f

Quote:
Code:
				lines=`expr $lines + 1`
				words=`expr $words + $#`

Code:
lines=$(( $lines + 1 ))
words=$(( $words + $# ))

Quote:
Code:
			done
			echo "Number Of Lines :$lines"
			echo "Number Of Words :$words"
			echo "Number Of Characters :$chars"
		fi
	else
		echo "$file does not exists"
	fi
fi

# 3  
Old 02-18-2009
gosh! cfajohnson you did the operation of my whole program. Smilie
Thanks a lot! for pointing out the mistakes
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

2. AIX

Lsof command giving while loop

Hello, There is a process in AIX which is actually a oracle database user session but is running very slow When I use lsof it give below output lsof /proc/21955180 In while loop:256 In while loop:256 In while loop:256 In while loop:256 Value of I :183 np:1024 Please... (1 Reply)
Discussion started by: Vishal_dba
1 Replies

3. Shell Programming and Scripting

Remote simulation and 'at' command

Hey, Task seems to be quite easy, but I'm still a bit green in shell scripting. I hope you can help me a bit. I have to run some simulation at the distance by remote terminal. Normally when I'm working on the server directly I just type: mpirun -np 8 compressibleInterFoam -parallel > log.txt... (7 Replies)
Discussion started by: PiPrus
7 Replies

4. Linux

acroread command giving error

Hi, When I run the command acroread it is giving error ERROR: Cannot find installation directory. When I look at /usr/local/Adobe/Acrobat7.0/bin/ an exicutable file named acroread is there. When i run ./acroread from that directory it is working . What can i do so that I can give... (3 Replies)
Discussion started by: iamjayanth
3 Replies

5. UNIX for Dummies Questions & Answers

Last Command not giving year

Hi All, i want the last login details along with year. i tried below command but not giving me the year of last login. last <$Userid> | head -1 i heard that if it is current year then it wont display the year else it will display the year. is it so? if yes then , is there any way of... (1 Reply)
Discussion started by: ani_rvce
1 Replies

6. Shell Programming and Scripting

Last Command not giving year

Hi All, i want the last login details along with year. i tried below command but not giving me the year of last login. last <$Userid> | head -1 i heard that if it is current year then it wont display the year else it will display the year. is it so? if yes then , is there any way of... (1 Reply)
Discussion started by: ani_rvce
1 Replies

7. Linux

Terminal Execution By Giving a Command

Hi Guys, I am using Red Hat Linux 5 and GNOME Terminal is available there in the Accessories menu of Applications. But I don't see any run command option which can be used to type the name of the terminal and execute it directly as I used to do it under Mandrake Linux wherein I would type... (2 Replies)
Discussion started by: indiansoil
2 Replies

8. Solaris

^p not giving command line history.

I am used to using "set -o emacs" and then using "CNTL-P" for getting previous commands in solaris but on one host it does not work and instead just makes a DONG!! # ksh # set -o emacs # ls # ^p Can anyone offer guidance as to why this is? Thank you my friends. akbar (0 Replies)
Discussion started by: akbar
0 Replies

9. HP-UX

RLOGIN giving problems.

Hi, I am using RDIST to sync /interfaces directory between two systems say ukblx176 (source) & ukapx063 (target), but when i do rlogin ukapx063 from ukblx176 its asking for password (i dont want it to ask password). when i do rlogin ukblx176 from ukapx063 its doesnt ask for password. ukapx063... (8 Replies)
Discussion started by: vishal_ranjan
8 Replies

10. Solaris

/sbin/zonename file giving me problems

I'm trying to install the recommended patch cluster on a x86 Solaris 10 box. I get this error: ERROR: /sbin/zonename cannot be found. install_cluster for 10_x86 patch cluster not applied. Exiting. I tried touching the file and it gave a different error of: expected argument.. anyone... (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies
Login or Register to Ask a Question