ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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"
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.
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
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"
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.
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.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.