Question

Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command no...

Hello! In this bash program, there is an error when i try to execute it.

I am running it on Unix and I used emacs for the text editor

it says:

line 3: [0: command not found

line 9: if[-f ]: command not found

line 10: syntax error near unexpected token `then'

line 10: `then'

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

#!/bin/bash

if [ $# != 1 ]
then
echo "Usage: myScript.sh "
exit 1
fi

if [ -f $1 ]
then

awk '
BEGIN {
i=0
flag=0
avg=0
total=0
}
{
tmp =0
count=0
sum=0
flag = 0
for(i=1; i<=2; i++)
{
printf("%s ", $i);
}

for(i=3; i<=NF; i++)
{
tmp = $i + 0
if ($i == 0)
{
flag =1
}
printf("%s ",$i);
if(tmp || flag)
{
sum = sum + tmp
count++
}
}
printf("%d %d", sum, (sum/count) +0.5);

printf("\n");
}

END {

}
' < $1

else

echo "Error: file $1 does not exist"
fi

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the right script and following errors could be introduced by you.

1. You have leave space between "if" and "[" not as "if[ $# != 1]". So it should be like this"if [ $# != 1 ]"

2. And the same is in "if[ -f". So it should be like this "if [ -f $1 ]"

3. Use emacs to type the same program with exact spaces as mentioned above and right syntax

Program Screen Shot:

if[$# != 1 ] then echo Usage: myscript.ah exit 1 fi if -$1 then awk BEGIN flag avg o total-0 tmp =0 count-0 sum-0 flag 0 f

Add a comment
Know the answer?
Add Answer to:
Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command no...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If...

    Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If you could start the assignment and tell me how to do the rest it would be greatly appreciated! (Quick thumbs up answer response if thorough and correct) Maintain automobile records in a database Write a shell script to create, view and modify a simple database that contains automobile records. The shell script has to be done in Bourne shell syntax (bash as a matter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT