Shall script use and understanding

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shall script use and understanding
# 1  
Old 11-23-2016
Shall script use and understanding

Below script is called as Reducer, I am not sure how it work, can some expert explain me what this script does as i am a beginner.

inputfile:
Code:
hi hi how are are you

code:
Code:
#!/bin/bash
lastkey=""; -- what does this mean, because i saw in debug mode it is taking value as hi 
count=0;
total=0;
iteration=1
while read line
 do
  newkey=`echo $line | awk '{print $1}'`
  value=`echo $line | awk '{print $2}'`
   if [ "$iteration" == "1" ] 
then
	lastkey=$newkey;c
	iteration=`expr $iteration + 1`;
   fi
   if [[ "$lastkey" != "$newkey" ]] 
then
	average=`echo "scale=5;$total / $count" | bc`;
	echo -e "$lastkeyt$average"
	count=0;
	lastkey=$newkey;
	total=0;
	average=0;
   fi
   total=`expr $total + $value`;
   lastkey=$newkey;
   count=`expr $count + 1`;
done

is it expected to give below output:
Code:
hi 2
how 1
are 2 
you 1

I am getting below error when i run it
Code:
expr:non numeric argument


Last edited by rbatte1; 11-23-2016 at 10:52 AM.. Reason: Added CODE tags for input file
# 2  
Old 11-23-2016
The author does like ; quite a lot. They are not needed at the end of a line in bash and the lastkey=""; -- what does this mean ..... line would give a syntax error similar to:-
Code:
bash: --: command not found

The comment character is a hash #

The error you are seeing is because one or more of the variables being fed into expr are not numeric. Try running it like this:-
Code:
bash -x Reducer

.... although I don't see where you read an input file. You might also manage it with
Code:
bash -x Reducer < inputfile

Using the -x flag will cause it to show you more about what is happening in your code as it runs.


Does this help enough?

Robin
# 3  
Old 11-23-2016
Dear,

HTML Code:
lastkey=""
is not throwing any error, you can run the script on my given file.. it is taking some value as
HTML Code:
hi
.
i need to know from where it does that
# 4  
Old 11-23-2016
Are you sure the input file is correctly shown? The script is intended to read a multi line file with each line consisting of a string and a value (sort of what the shown "output" is), which your input definitely is not. It will perform exactly one single loop on the input, having newkey = lastkey = value = "hi" before it exits. That also explains the error you get - $value will contain "hi" which is not numeric.
And, there are some syntax errors in the script:
- lastkey=$newkey;c will try to run the command c after the assignment.
- echo -e "$lastkeyt$average" will not find the lastkeyt variable
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-23-2016
Rudic:
so does the lastkey="" always take the last value in the file and yes you are right the input to the file is what i mentioned as output . you are heaven sent. but this code now when i run is running fine after changing the input file but the out put is not expected.
input is :
Code:
hi 1
hi 1
how 1
are 1
are 1
you 1

and the expected out put is
Code:
hi 2
how 2
are 2
you 1

# 6  
Old 11-23-2016
Quote:
Originally Posted by mirwasim
Rudic:
so does the lastkey="" always take the last value in the file
No. That assigns the empty string to lastkey. lastkey=$newkey; with $newkey holding "hi" will do what you suspect.


Quote:
.
.
.
but the out put is not expected.
.
.
.
What, then, is the actual output? Did you correct the errors?
# 7  
Old 11-23-2016
now some average is coming as out put, but can we some modification and get the above mentioned out put.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Understanding a script for sum

Hello, How come the following script adds each numeric value to a total sum ? x=$1 func() { for i in $1 $2 $3; do let x= $x+$i done } func "8 8 8" 9 9 echo $x A.How the program sums the string "8 8 8" if it`s only the first field value ($1)? B.If we define x to be $1... (3 Replies)
Discussion started by: uniran
3 Replies

2. Shell Programming and Scripting

Help understanding the script

Could someone please help me in understanding the code below: #!/usr/bin/ksh Month=`date|cut -c5-7` Day=`date|cut -c9-10` Year=`date|cut -c27-28` Rom2Jul() { case $Month in Feb) Day=$(( $Day+31 ));; Mar) Day=$((... (27 Replies)
Discussion started by: hasn318
27 Replies

3. Shell Programming and Scripting

Help--understanding the script

Hi, New to this forum, as well as to unix scripting..need help to understand below script ... sendNotice_sp() { ATTACH=${LNXLOG:-} if ; then if ; then mail -s "$ERR_MSG" $ERR_EMAIL_TO < $ATTACH fi else mail -s "$ERR_MSG" $ERR_EMAIL_TO < /dev/null fi } afaik this is sm kind of... (6 Replies)
Discussion started by: gnnsprapa
6 Replies

4. Shell Programming and Scripting

Help understanding the script

Hi Guys, I am new to scripting , I am trying to rebuild a script based on an old script. Can someone help me figure out what the script is doing? This is only a part of the script. I am looking to interpret these two points in the scripts:- 1) test=`echo $?` while I do not... (3 Replies)
Discussion started by: rajsan
3 Replies

5. UNIX for Dummies Questions & Answers

help with understanding script

i am trying to understand this script example. the text does not explain it. can someone tell me briefly what each of the functions do. any help will be appreciated. #!/bin/bash killtree() { local _pid=$1 local _sig=${2-TERM} for _child in $(ps -o pid --no-headers --ppid... (1 Reply)
Discussion started by: iluvsushi
1 Replies

6. Shell Programming and Scripting

Need help understanding this script.

Can someone explain what is happening line by line in this script, particularly after the do statement. The script works, it renames all the files in my directory that has a date in the file name. But I would like to know more about it. #!/bin/bash newdate=12-10-1995 for file in *--* do ... (6 Replies)
Discussion started by: Harleyrci
6 Replies

7. UNIX for Advanced & Expert Users

understanding awk in this script

i am analyzing a query written by another developer ,need to understand part of script am looking at a code ..and it converts comma files to pipe delimited and also takes away quotes from any columns, source field format: 2510,"Debbie",NewYork changes to target: 2510|Debbie|NewYork ... (1 Reply)
Discussion started by: coolrock
1 Replies

8. Shell Programming and Scripting

Need help for understanding of script

# sub: find block (in cols), return line-numbers (begin-end) or 0 if notfound sub findb{ my ($exp1,$col1,$exp2,$col2)= @_; # $exp = expression to find, $col - column to search in my $cnt=0; my ($val1,$val2); my ($beg,$end); for($cnt=1;$cnt<=65536;$cnt++){ $val1 =... (3 Replies)
Discussion started by: suvenduperl
3 Replies

9. Shell Programming and Scripting

Help understanding a script

Hello everybody, Can anybody tell me of what "~" refers to in the below code snippet. lsvg $vgNAME | awk 'BEGIN {freeDISK=1} {if (($4 ~ /PP/ && $5 ~ /SIZE/) || ($4 ~ /FREE/ && $5 ~ /PPs/)) {freeDISK *= $6 }} END {print freeDISK*1024 }' Thanks in advance, (6 Replies)
Discussion started by: tenderfoot
6 Replies

10. UNIX for Dummies Questions & Answers

Need help understanding script command

We use a UNIX-based system (Lawson) at work and I was given this command to request a data extract from the db admin. The only thing I really understand is the last line as it appears to be joining the files created from the first three lines into one. Is there anyone who can help me breakdown the... (4 Replies)
Discussion started by: KGee
4 Replies
Login or Register to Ask a Question