script in Bash 2.05b.0(1) and 3.2.25(1) Red Hat


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers script in Bash 2.05b.0(1) and 3.2.25(1) Red Hat
# 1  
Old 06-22-2009
script in Bash 2.05b.0(1) and 3.2.25(1) Red Hat

Hi
I have this script

#!/bin/bash
cat status.txt | while read LINE
do
linija="$LINE"
echo $linija
echo $linija | awk '{print $1}' > temp.txt
koj=`tail -1 temp.txt`
echo $koj
echo $linija | awk '{print $2}' > temp1.txt
kolku=`tail -1 temp1.txt`
vkupno=$(( vkupno + kolku ))
echo $kolku
echo $vkupno > vkupno_temp.txt
done
kolku_vkupno=`tail -1 vkupno_temp.txt`
echo $kolku_vkupno

and its working perfectly in 2.05b.0(1) bash called with ksh and bash
But in 3.2.25(1) version of bash only the first line of status.txt passes. Then (on the second line of the file status.txt) i get this error : arithmetic syntax error
on this line vkupno=$(( vkupno + kolku )). Can someone pls help. What should i change in the script to work in new bash.

Thanks in advance

P.S. status.txt consists of lines :
sometext 4
another text 12
and so on
# 2  
Old 06-22-2009
Try this..

vkupno=`expr $vkupno + $kolku`
# 3  
Old 06-22-2009
maybe i mislead

in P.S. i wrote
sometext 4
another text 12

This would be an obvious mistake because awk '{print $2}' will get text and so on.
I must apologize

status.txt consist of
sometext 4
anothertext 12
thirdtext 45

---------- Post updated at 03:42 PM ---------- Previous update was at 03:04 PM ----------

Thanks but I get the same error with your suggestion.
Anyone pls?
It's driving me mad...
# 4  
Old 06-22-2009
You do not initialize a variable.
Code:
#!/bin/bash
vkupno=0
cat status.txt | while read LINE
do
...
...
done

The "cat" should also not be there - it is called UUOC - useless use of cat - a waste of resources. There are also some other oddities in there - like using two awk calls each writing to a file, instead of just reading in the two variables on the while line.

If I understand your code I would write this:
Code:
#!/bin/bash
vkupno=0
while read koj kolku
do
  echo $koj
  echo $kolku
  vkupno=$(( vkupno + kolku ))
done < status.txt 
kolku_vkupno=$vkupno
echo $kolku_vkupno

I left in the echo statements, they look more like debug statements....
# 5  
Old 06-22-2009
I tested your code on bash 3.2.38 and it worked fine. It might be that specific version has an issue or compatibility problem.

You could try this:
Code:
let vkupno=( $vkupno + $kolku )

A couple of things you can do to shorten or simplify this code:

Instead of two lines:

Code:
 
echo $linija | awk '{print $1}' > temp.txt
koj=`tail -1 temp.txt`

it could be written as one:

Code:
koj=$( echo $linija | awk '{print $1}' )

I might shorten the script by several lines and write the script this way:

Code:
while read txt num
do
    echo $txt
    echo $num
    let sum=( $sum + $num )
    echo $sum
done < status.txt
echo $sum

# 6  
Old 06-23-2009
Thanks for the replyes, but...

Here is the whole script:
#!/bin/bash
vkupno=0
while read txt num
do
echo $txt
echo $num
let vkupno=( $vkupno + $num )
echo $vkupno
if [ $(( num )) -lt 5 ]; then
STATUS="OK"
fi
if [ $(( num )) -ge 5 ]; then
STATUS="WORRIED"
fi
if [ $(( num )) -ge 10 ]; then
STATUS="PANIC"
fi
done < status.txt
echo $vkupno
if [ $(( vkupno )) -lt 10 ]; then
STATUS="OK"
fi
if [ $(( vkupno )) -ge 10 ]; then
STATUS="WORRIED"
fi
if [ $(( vkupno )) -ge 20 ]; then
STATUS="PANIC"
fi

Whatever i write on older bash it's working, and on this newer version nothing. I'm still getting the arithmetic error, even on if [ $(( num )) -lt 5 ]; then lines.
Anyway thanks a lot guys. If the script won't work at least i learned to write little more nicer code.
But if You have any ideas please....

Last edited by Ruzeil; 06-23-2009 at 06:22 AM.. Reason: syntax errors
# 7  
Old 06-23-2009
I did find a few posts about syntax error issues with bash 3.2.x versions. A little more research is needed though. You didn't by chance edit the file on a Windows system? This would have left \n\r on the lines and cause errors. What version of RedHat are you running? Have you checked for updates to the bash rpm for that release?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to know if i use "Red Hat Enterprise Linux" or "Red Hat Desktop" ?

how to know if i use "Red Hat Enterprise Linux" or "Red Hat Desktop" ? (2 Replies)
Discussion started by: ahmedamer12
2 Replies

2. Red Hat

HELP - Resource/Performance Monitoring Script - Red Hat Enterprise Linux Server

Hi all, ------------------------- Linux OS Version/Release: ------------------------- Red Hat Enterprise Linux Server release 5.5 (Tikanga) Linux <hostname> 2.6.18-194.8.1.el5 #1 SMP Wed Jun 23 10:52:51 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux I have a server that hosts 30+ Oracle... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Red Hat

Red-hat

Hello, How do I see what IP addresses are connected to my machine? thanks (2 Replies)
Discussion started by: sonomao
2 Replies

4. Red Hat

Red Hat E 3

I'm am working with a Red Hat Enterprise 3 server. This is a dedicated server that is supposedly dedicated to one domain, but I have been tasked with trying to figure out if there are files on this system that are being accessed by other IP's. Does anyone know if how I would go about finding... (0 Replies)
Discussion started by: chrisPlusPlus
0 Replies

5. Linux

red hat ent 4 cron script

hi i need to put the full path of every executable i need to put on my script. on the testing you don't need it but if it's running from cron you need to put the full path of the executable or another script. what's the work around for this? Thanks. (1 Reply)
Discussion started by: itik
1 Replies

6. Shell Programming and Scripting

telnet shell script on red hat 9 cmd line only

i would like to make a shell script (red hat 9 cmd line only) to telnet to my local isp's webmail server on port 25 and send it commands such as helo :) help would be much appreciated, and i found no posts similar that answered my question... the closest i've gotten to an answer from about 8... (3 Replies)
Discussion started by: kypeswith
3 Replies

7. Linux

red hat ee 2.6.9-42

hello. I would be greatfull if someone could tell me how will i see what dns server and gateway my red hat server uses. I tryied to find out by typing ifconfig command but i got : -bash: ifconfig: command not found , although man ifonconfig gives output with info about using that command. Is... (3 Replies)
Discussion started by: tonijel
3 Replies

8. UNIX for Dummies Questions & Answers

Red Hat 9 help

please help me how to install softwares in linux.i have a ethernet internet connection.i try to open the site on LAN to download internet client.but i cannot as our lan supports only internet explorer....i downloaded the linux client by booting thru XP......but i cannot c my NTFS partition thru... (0 Replies)
Discussion started by: shobhit143
0 Replies

9. UNIX for Dummies Questions & Answers

Red Hat 7.2.....

I just installed red hat 7.2 on my laptop. it's dual booted with xp and red hat. when i boot in to linux it boots up to the screen to ask me my name and pass....i put in root and my password. after this it goes to a blue screen and sits there. the after about 2 minutes it comes up with a fatal... (1 Reply)
Discussion started by: muzscman
1 Replies
Login or Register to Ask a Question