LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2009, 01:53 PM   #1
jax8
Member
 
Registered: Feb 2004
Location: Australia
Distribution: Ubuntu, Fedora 10
Posts: 632

Rep: Reputation: 31
Using apt-get in if statement in bash script


Hi

I have a problem using apt-get in my bash script.

Quote:
logFile='log.txt'
packageList='slapd'


for package in $packageList; do
echo -n "Installing $package : Please Wait "
if [ $(apt-get -y install "$package" | tee -a "$logFile") ]; then
echo '(Installed)'
else
echo "Error installing $package - view $logFile for details"
exit 1
fi
done
When I run this I get an error that says

" ./ubuntuSchoolServerInstaller.sh: line 63: [: too many arguments"

In my script line 63 is in bold above.

Last edited by jax8; 03-28-2009 at 01:55 PM.
 
Old 03-28-2009, 02:12 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 356

Rep: Reputation: 43
Try this (I have not tested)

Code:
logFile='log.txt'
packageList='slapd'

exec >$logFile 2>&1


for package in $packageList; do
echo -n "Installing $package : Please Wait "
if apt-get -y install "$package"; then
echo '(Installed)'
else
echo "Error installing $package - view $logFile for details"
exit 1
fi
done
 
Old 03-28-2009, 02:52 PM   #3
jax8
Member
 
Registered: Feb 2004
Location: Australia
Distribution: Ubuntu, Fedora 10
Posts: 632

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by TBC Cosmo View Post
Try this (I have not tested)

Code:
logFile='log.txt'
packageList='slapd'

exec >$logFile 2>&1


for package in $packageList; do
echo -n "Installing $package : Please Wait "
if apt-get -y install "$package"; then
echo '(Installed)'
else
echo "Error installing $package - view $logFile for details"
exit 1
fi
done
That seems to work thanks.

Do you know why this does not work?
Code:
if [ $(apt-get -y install "$package") ]; then
 
Old 03-29-2009, 01:11 PM   #4
TimothyEBaldwin
Member
 
Registered: Mar 2009
Posts: 249

Rep: Reputation: 27
Because $(some command) substitutes the standard output of "some command" for itself. The output of apt-get is not sensible input for the [ command.

Perhaps you meant
Code:
if [ (apt-get -y install "$package") ]; then
that is without the dollar sign before the bracket.
 
Old 03-29-2009, 10:23 PM   #5
jax8
Member
 
Registered: Feb 2004
Location: Australia
Distribution: Ubuntu, Fedora 10
Posts: 632

Original Poster
Rep: Reputation: 31
Ok, thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
problem with if statement in bash script crewblunts Programming 3 03-11-2006 12:38 PM
how to close 'else' statement block in bash script servnov Linux - General 9 11-12-2004 03:53 PM
Bash script problems with "if" statement adz Programming 2 05-22-2004 03:34 AM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 02:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration