Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 6th January 2012, 10:07 PM
cdgary Offline
Registered User
 
Join Date: Dec 2011
Posts: 15
windows_7ie
Bash Scripting Question

I'm a newbie to bash shell scripting and I wanted to know if you create a bash script to perform a yum operation such as yum groupinstall, the system will eventually prompt you for a y or n response when running this from the command line. How do you deal with that in your script or any other situation where you have to respond to something the system will ask before moving forward, like a password or yes/no after performing a command?

Thanks
Reply With Quote
  #2  
Old 6th January 2012, 10:14 PM
coffee412's Avatar
coffee412 Offline
Registered User
 
Join Date: Aug 2005
Location: Ask the Government.
Posts: 364
linuxfirefox
Re: Bash Scripting Question

Your actual yum command line would be:

yum -y install blah.blah.

Then you dont have to mess with answering YES to yum.

Have a great one!

---Edit---
Didnt see the last part of your question. I think alot of commands have a switch to bypass the question. So, WHen running a command in a script you should investigate the command and first see if it offers something along the lines of the -y like in yum. Thats the easiest.
__________________
Fedora17 AMD X6 1090T - 8GIG RAM - 9500GT - LXDE. Linux since 1995 - No Gnomes allowed.

Last edited by coffee412; 6th January 2012 at 10:16 PM.
Reply With Quote
  #3  
Old 7th January 2012, 01:51 AM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Helvetic Federation (Swissh)
Age: 33
Posts: 2,648
linuxfirefox
Re: Bash Scripting Question

To check if an application is installed with yum, you might modify this script:
Code:
    askyum="Ask yum if awesome is installed..."
    printf "Searching..." "awesome repo"
    printf "$askyum" " "
    tpp=$(yum list installed awesome)
    echo $tpp | grep "Installed Packages awesome" && test ! "" = "$tpp" && scecho "$askyum" "$SUCCESS" || scecho "$askyum" "$FAILURE"
Or to install a missing repo:
Code:
# |
# | Check if repo is installed 
    repoInstalled=$(ls /etc/yum.repos.d/*awesome*)
    if [[  "" = "$repoInstalled" ]]
        then    if ask "The awesome repo was not found, get it now?" ; then
	                cd /etc/yum.repos.d
                    su -c "wget -nc http://repos.fedorapeople.org/repos/thm/awesome/fedora-awesome.repo"
                else
                    scecho "Sorry, but without the repo you cant install awesome." " "
                    scecho "Exiting now..." " "
                fi
      fi
G'night
__________________
Notebook: Samsung NC210/NC110 CPU: Intel 2*1600 Mhz RAM: 1024 MB DDR2 OS: Fedora 19 DE: Awesome
Laptop: Toshiba Satellite L670 CPU: Intel(r) 2*1872 Mhz RAM: 3072 MB DDR3 OS: Fedora 17 DE: Awesome
Fedora Manual (RTFM) | The Linux Documentation Project | Script-Tools
Reply With Quote
  #4  
Old 7th January 2012, 04:56 AM
weitjong's Avatar
weitjong Online
Registered User
 
Join Date: Oct 2006
Location: Singapore, 新加坡
Posts: 790
linuxfirefox
Re: Bash Scripting Question

If I understand OP correctly, "yum" is mentioned only as an example. The general way to provide user input to another program/script within your bash script is by redirecting the stdin (<), or by using a HEREDOC (<<), or by piping (|). For the latter, if the respond is a fixed answer like always 'y' or 'n' then you can use piping in tandem with "yes" command, e.g. "yes |other-program" or "yes n |other-program".
__________________
YaoWT - Leave no window unbroken ^_^

Last edited by weitjong; 7th January 2012 at 05:11 AM.
Reply With Quote
  #5  
Old 7th January 2012, 09:01 AM
jamielinux Offline
Registered User
 
Join Date: Jun 2011
Posts: 64
linuxfirefox
Re: Bash Scripting Question

Quote:
Originally Posted by sea View Post
Code:
    tpp=$(yum list installed awesome)
    echo $tpp | grep "Installed Packages awesome" && test ! "" = "$tpp" && scecho "$askyum" "$SUCCESS" || scecho "$askyum" "$FAILURE"
Really no need for echo/grep/test. Yum returns non-zero value if the package is not found.
Reply With Quote
  #6  
Old 7th January 2012, 03:12 PM
RupertPupkin's Avatar
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 4,728
linuxfedorafirefox
Re: Bash Scripting Question

Quote:
Originally Posted by cdgary View Post
How do you deal with that in your script or any other situation where you have to respond to something the system will ask before moving forward, like a password or yes/no after performing a command?
As weitjong mentioned there are several ways to do this, but my favorite is to use Expect, which is in the Fedora repos. Basically, the way Expect works is you write a script that automates answers for what you "expect" a program to ask you. In fact, you don't even have to write the script yourself; Expect includes a utility called autoexpect which can create it for you from an example session that you go through. It's very simple to use.

One thing you should be careful about, though, is storing plain-text passwords in scripts. That's usually a big security no-no. There are ways to avoid that by using encryption or other methods.
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
Reply With Quote
  #7  
Old 7th January 2012, 03:39 PM
marriedto51 Offline
Registered User
 
Join Date: Jul 2009
Location: England, UK
Posts: 823
linuxfirefox
Re: Bash Scripting Question

+1 for expect.

In fact you will find in some cases that trying to pipe or redirect stdin for an interactive process will not work. (One example is using telnet to retrieve mail from a POP3 server -- which you can achieve using expect instead.)
Reply With Quote
Reply

Tags
bash, question, scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting question Reekwind Programming & Packaging 10 16th December 2008 03:31 PM
cron scripting/bash scripting coolbeansdude51 Using Fedora 7 16th January 2007 03:13 AM
Bash scripting question MaldiGola Using Fedora 5 7th September 2006 05:13 PM
Bash scripting question leaded Programming & Packaging 4 17th March 2006 04:15 PM
A question with bash scripting pinenut Using Fedora 3 1st March 2006 08:06 PM


Current GMT-time: 07:41 (Thursday, 20-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat