0

I'm new around the neighborhood and stuck with a syntax error. Please take a look and maybe someone can assist. I'm trying to run the following script:

#!/bin/bash

main () {
 dpkg -query -s $1 &> /tmp/pkg_verify
 if grep -q 'not installed' /tmp/verify
 then
  echo -e "\e[31m$1 is not installed. installing..\e[0m"
  apt-get install $1
  echo -e "\e[31m$1 is not installed and ready to use\e[0m"
 else
  echo -e "\e[31m$1 is already installed\e[0m"
 fi
 rm -f /tmp/pkg_verify
 for test in $@; do main $test; shift; done
 echo -e "\e[31mDone\e[0m"
}
for test in $@; do main $test; shift; done

echo -e "\e[31mDone\e[0m"

But when I try to execute it I'm facing with endless loop:

grep: /tmp/verify: No such file or directory
16 is already installed

I truly tried to find the answer, tried to change the if to couple of different forms but with out any success. Does any one have an idea why that is? What should I change so that the script can run?

Thanks in advance to all the helpers.

3
  • BTW your "shift" is unneeded, as the for loop steps through the parameters in $@ anyway. Commented Dec 16, 2013 at 11:56
  • 1
    I think /tmp/verify should be /tmp/pkg_verify (i.e., you're using the wrong temporary file name). Commented Dec 16, 2013 at 17:49
  • thanks @chepner, but still after changing the file name the next error is apperd: dpkg: error: unknown option -q Type dpkg --help for help about installing and deinstalling packages []; Use dselect' or aptitude' for user-friendly package management; Type dpkg -Dhelp for a list of dpkg debug flag values; Type dpkg --force-help for a list of forcing options; Type dpkg-deb --help for help about manipulating *.deb files; Options marked [] produce a lot of output - pipe it through less' or more' ! top is already installed Done any idae why is that? Commented Dec 16, 2013 at 17:56

2 Answers 2

3

You have two else following each other. That can't work. It's either elif condition or just a single else.

The infinite loop is caused by main calling itself recursively.

And third, it's probably a bug to shift when iterating with for i in "$@".

To debug a script (free of syntax errors) use set -x near the beginning.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace this line:

if [[ -z `grep 'not installed' /tmp/pkg_verify` ]]

with this if condition:

if grep -q 'not installed' /tmp/pkg_verify

Full Script:

main () {
     dpkg-query -s "$1" > /tmp/pkg_verify
     if grep -q 'not installed' /tmp/pkg_verify
     then
       echo -e "\e[31m$1 is not installed. installing..\e[0m"
       apt-get install "$1"
       echo -e "\e[31m$1 is not installed and ready to use\e[0m"
    else
       echo -e "\e[31m$1 is already installed\e[0m"
    fi
 }
 rm -f /tmp/pkg_verify
 for test in $@; do main "$test"; done
 echo -e "\e[31mDone\e[0m"

13 Comments

@nubhava thanks for the quick replay, as you said i changed the if condition and when im trying to run it i get: line 21: syntax error near unexpected toke 'else' line 21: 'else' any idie? p.s can you please explain what was wrong in the if condition that i write?
Problem with your condition was that the output of grep has white-spaces in it and because of that if condition gets multiple arguments after -z where it expects only one argument. Why you are getting error elsewhere in the code is difficult to say until I see full script. Post your complete script in the question so that I check and suggest.
@nubhave: as your request i'm posting the script after changed the if condition, please check the question
Ah looks like you have 2 else blocks that is the problem now.
Also I posted your full script (fixed) in my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.