LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-15-2008, 01:15 PM   #1
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 51

Rep: Reputation: 18
Bash Y/N user input


All,

Got a quick question about a bash scripti I'm putting together. I'm writing a script that I want to have a user select Y/n. Y being the default if they just hit enter to pass.

One issue I was having was it only accepts capitol case input.

What the script will be doing is line by line installing some rpms (example)

Would you like to install John The Ripper Y/n?

If they go with yes it runs yum install -y john

if they go with No it moves to the next question asking about an install.

I know its a little vague.

R/

Vince
 
Old 04-15-2008, 01:26 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
Try this:

while read a; do
echo $a
done

The $a will be whatever the user enters. Check out this website

http://bashscripts.org/
 
Old 04-15-2008, 01:31 PM   #3
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 51

Original Poster
Rep: Reputation: 18
Thank

I got the read in part down. Does the a take care of the caps no caps part?

I think think next part has to be a if statement to install if y or dont if n.

Just cant get it to function properly.

Thanks!
 
Old 04-15-2008, 02:38 PM   #4
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
You could write:
Code:
read a
if [[ $a == "N" || $a == "n" ]]; then
        echo "not gonna do it"
else
        echo "gonna do it"
fi
That will "not do it" for a response of "N" or "n" and "do it" for everything else (including "no", "for the love of god, no", "don't you dare", etc).

Alternatively, you could do:

Code:
read a
if [[ $a == "Y" || $a == "y" || $a = "" ]]; then
        echo "gonna do it"
fi
will "do it" for a response of "Y", "y" or simply pressing enter, and "not do it" otherwise.
 
Old 04-15-2008, 04:32 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...or alternatively you could write:
Code:
read item
case "$item" in
 y|Y) echo "do it";;
 n|N) echo "drop it";;
 *) echo "not an answer";;
esac
or you could even force options:
Code:
select item in y n quit; do 
 case "$item" in
  y) echo do it;;
  n) echo "skip it" ;;
  *) break;;
 esac
done
 
Old 04-15-2008, 05:04 PM   #6
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 51

Original Poster
Rep: Reputation: 18
little more help.

I got the y/n input working, but I'm scratching my head on this one part.

Im asking the user to specify if they want to be prompted to install a tool on an individual basis, or as a group. The group install is the first option and the tool by tool is the second, but I want it to completely skip the second part if the do the group install.

Is there a way to jump to a certain line in the script to bypass ?


Sample script:

echo "Would you like to install the default tool package n/Y?"
echo ""
read a
if [[ $a == "N" || $a == "n" ]]; then
echo "The default tool package will not be installed."
else
echo "Installing the default tool package"

yum install -y p0f
yum install -y nbtscan
yum install -y wine

fi


R/

Vince
 
Old 04-15-2008, 10:59 PM   #7
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 51

Original Poster
Rep: Reputation: 18
if statements within if statements

Hey,

Ok, I thought I had it. There is something im obviously missing since im telling it else, go into another if statement. Below is the sample cold that I "thought" was going to work.

_____________________________________________________

#!/bin/bash

echo "We will now start installing the tools"
echo ""
echo ""
echo ""
echo ""
echo ""
echo "Would you like to install the default tool package n/Y?"
echo "If you decided not to install the default package you will be prompted for each indvidual tool"
read a
if [[ $a == "Y" || $a == "y" ]]; then

echo "Installing the default pentest tool package"
yum install p0f
yum install nbtscan


else
echo "You will now be prompted to install the "normal" tool list on an individual basis"
echo ""
echo "To speed the process, the default is YES (Just hit enter or y), otherwise hit n to NOT install the specified tool"
echo ""
echo ""
echo "Starting the individual install process"
echo ""
echo ""

echo "P0f v2 is a versatile passive OS fingerprinting tool. P0f can identify the system on machines that talk thru or near your box. p0f will also check masquerading and firewall presence, the distance to the remote system and its uptime, other guy's network hookup (DSL, OC3, avian carriers) and his ISP."
echo ""
echo ""
echo "Would you like to install p0f? n/Y?"
read a
if [[ $b == "N" || $b == "n" ]]; then

echo "p0f will not be installed."
else
echo "Installing p0f"

echo "NBTscan is a program for scanning IP networks for NetBIOS name information. It sends a NetBIOS status query to each address in supplied range and lists received information in human readable form. "
echo ""
echo ""
echo "Would you like to install Nbtscan n/Y"
read a
if [[ $a == "N" || $a == "n" ]]; then
echo "Nbtscan will not be installed."
else
echo "Installing Nbtscan"

yum install -y nbtscan
fi
 
Old 04-16-2008, 12:02 AM   #8
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
1. Can you please use [ code][ /code] when posting code on here, this preservers the indentation.
2. I'm assuming you don't indent your code, cause then you would've probably noticed the missing fi's at the end. (unless you just pasted it wrong)

Code:
#!/bin/bash

echo "We will now start installing the tools"
echo ""
echo ""
echo ""
echo ""
echo ""
echo "Would you like to install the default tool package n/Y?"
echo "If you decided not to install the default package you will be prompted for each indvidual tool"
read a
if [[ $a == "Y" || $a == "y" ]]; then

  echo "Installing the default pentest tool package"
  yum install p0f
  yum install nbtscan


else
  echo "You will now be prompted to install the "normal" tool list on an individual basis"
  echo ""
  echo "To speed the process, the default is YES (Just hit enter or y), otherwise hit n to NOT install the specified tool"
  echo ""
  echo ""
  echo "Starting the individual install process"
  echo ""
  echo ""

  echo "P0f v2 is a versatile passive OS fingerprinting tool. P0f can identify the system on machines that talk thru or near your box. p0f will also check masquerading and firewall presence, the distance to the remote system and its uptime, other guy's network hookup (DSL, OC3, avian carriers) and his ISP."
  echo ""
  echo ""
  echo "Would you like to install p0f? n/Y?"
  read a
  if [[ $a == "N" || $a == "n" ]]; then

    echo "p0f will not be installed."
  else
    echo "Installing p0f"

    echo "NBTscan is a program for scanning IP networks for NetBIOS name information. It sends a NetBIOS status query to each address in supplied range and lists received information in human readable form. "
    echo ""
    echo ""
    echo "Would you like to install Nbtscan n/Y"
    read a
    if [[ $a == "N" || $a == "n" ]]; then
      echo "Nbtscan will not be installed."
    else
      echo "Installing Nbtscan"

      yum install -y nbtscan
    fi
  fi
fi
Here's the diff (might be easier to see what's changed)
Code:
--- yesno-old.sh        2008-04-16 00:59:27.000000000 -0400
+++ yesno.sh    2008-04-16 01:01:49.000000000 -0400
@@ -31,7 +31,7 @@
   echo ""
   echo "Would you like to install p0f? n/Y?"
   read a
-  if [[ $b == "N" || $b == "n" ]]; then
+  if [[ $a == "N" || $a == "n" ]]; then
 
     echo "p0f will not be installed."
   else
@@ -49,3 +49,5 @@
 
       yum install -y nbtscan
     fi
+  fi
+fi
EDIT:
Regarding your first question, you can also do it this way:
Code:
if [[ $a == [Nn] ]]

Last edited by angrybanana; 04-16-2008 at 12:11 AM.
 
Old 04-16-2008, 08:29 AM   #9
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 51

Original Poster
Rep: Reputation: 18
Great!

Awesome, thanks for the help. I got everything working. My major issue was the number of fi's.

(Sorry, new to this)

Is there a more efficient way to deal with the number of fi's I will have. As it stands right now due to the number of tools im going to have 96 if statements. I can always do 96 fi's at the end, but just wanted to check.

R/

Vince
 
Old 04-16-2008, 09:16 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by zcrxsir88 View Post
Is there a more efficient way to deal with the number of fi's I will have.
Make a function. Put list of tools in a string. Do a "for" loop over the list using a y|Y|nN| case to get the answer. Dump the output of the function in a string (validate if necessary) and submit that to Yum at the end. Easier to structure, less waiting time and IMHO less error-prone.
 
Old 04-16-2008, 11:13 AM   #11
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 51

Original Poster
Rep: Reputation: 18
Thanks.

Yeah, I originally was going to do that when i was just going to have the yum install -y *whatever*

But then (of course) someone wanted comments about each tool to be installed. So now I having it echo info about the tool before asking if they would like to install it. Im sure I could make the "uber" for loop to grep for comments, etc.

Well see, for now I think there will be 96 fi's since that's how many tools are needed to be installed.

O'well. Thanks everyone for the help!

R/

Vince
 
Old 04-16-2008, 11:35 AM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
How about this: make variables like ${appname}_desc. Fill them with say RPM like appname_desc=`-q --qf="%{DESCRIPTION}\n" p0f`. Now for item in list-of-apps; do select option in install description skip; do case $option in ... and where it reads description) you can eval echo \$"${item}_desc" to display the description?
 
  


Reply

Tags
bash, install, script, yum


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 10:06 AM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM

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

All times are GMT -5. The time now is 12:28 AM.

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