0

Could some one help to find out what is problem with my for loop script. I'm trying to install packages from a list of variables using a for loop. My issues is when go grab the first package to be installed it and goes grab package from the list at the same time. Also it will install the packages all at the same time. How do I check fix it. I need to do one at a time.

REQPKGS="redhat-lsb-core telnet"

for pkg in $REQPKGS; do
    if yum -q list installed "$REQPKGS" > /dev/null 2>&1; then
        echo -e "$pkg is already installed"
    else
        yum install $REQPKGS -y
        echo "Successfully installed $REQPKGS"
    fi
done

Result of my script:

+ REQPKGS='redhat-lsb-core telnet'
+ for pkg in '$REQPKGS'
+ yum -q list installed 'redhat-lsb-core telnet'
+ yum install redhat-lsb-core telnet -y
+ echo 'Successfully installed redhat-lsb-core telnet'
Successfully installed redhat-lsb-core telnet
+ for pkg in '$REQPKGS'
+ yum -q list installed 'redhat-lsb-core telnet'
+ yum install redhat-lsb-core telnet -y
Loaded plugins: product-id, refresh-packagekit, rhnplugin, security, subscription-manager
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
Package redhat-lsb-core-4.0-7.el6.x86_64 already installed and latest version
Package 1:telnet-0.17-48.el6.x86_64 already installed and latest version
Nothing to do
+ echo 'Successfully installed redhat-lsb-core telnet'
Successfully installed redhat-lsb-core telnet

closed as off-topic by Stephen Harris, G-Man Says 'Reinstate Monica', Eric Renouf, mdpc, roaima Sep 16 '16 at 18:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Stephen Harris, G-Man Says 'Reinstate Monica', Eric Renouf, mdpc, roaima
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Also, why not use yum install $REQPKGS -y on its own rather than this for loop? yum will skip over packages that are already installed on its own. – Centimane Sep 16 '16 at 15:43
5

You use your array variable ($REQPKGS) inside your loop instead of the loop variable ($pkg), so it's doing exactly what you told it to do, install both packages at the same time twice.

Change the inside of your for loop to:

REQPKGS=(redhat-lsb-core telnet)

for pkg in "${REQPKGS[@]}"; do
    if yum -q list installed "$pkg" > /dev/null 2>&1; then
        echo -e "$pkg is already installed"
    else
        yum install "$pkg" -y && echo "Successfully installed $pkg"
    fi
done

When using a for loop, the variable you define after for holds the value for each iteration of the loop; in your case the variable that contains a single package at a time is $pkg.

Alternatively

REQPKGS=(redhat-lsb-core telnet)

yum install -y "${REQPKGS[@]}"

Will install any packages specified that aren't installed, and skip any packages that are already installed.

  • Centimane, It works great now. I got confused with the name of the variables. Thank a lot.!!! – pirulo Sep 16 '16 at 14:28
  • 1
    I would suggest a small improvement of yum install $pkg -y && echo "Success..." so that you're not misled by a yum failure and a "Successful" echo statement. – Jeff Schaller Sep 16 '16 at 15:07
  • @JeffSchaller That is better, I was focusing on the change in variable name rather than improving the logic itself, but I'll add that in – Centimane Sep 16 '16 at 15:40
  • @JeffSchaller honestly if we're going to improve the logic just doing yum install -y $REQPKGS would probably be the way to go, yum will skip over installed packages on its own. – Centimane Sep 16 '16 at 15:42
  • I don't disagree but the Q does ask for one at a time. – Jeff Schaller Sep 16 '16 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.