LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-20-2011, 07:17 PM   #1
Astroe
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
Bash'ing myself over the head, yes,no prompt scripting issues :(


Hey there, so I'm writing a script that will automate the download and most of the install of Java JDK, Android SDK, Eclipse, and script adb to start at boot.

If you own an Android phone, this would mean that you could view and manipulate your phone from your Ubuntu computer.

So I've got most of the script done, but I'm having issues with the prompts.

So when the script gets to the point where it edits the .bashrc I'd like it to stop and ask the user if they want to do that. I found the script for a prompt that works here and it looks like this
Code:
#1/bin/bash
yn() {
printf " Reply [y/n]: "
read yn
[[ "$yn" != y ]] && [[ "$yn" != n ]] && yn
}
# OTHER CODING
# Now you pose a question
# and then execute the yn function

printf "do you want to continue, "
yn
[[ "$yn = n ]] && exit
This is the unaltered code from the forum post, not yet customized to my needs.
What I want it to do it, if they say no, then the script continues, just skips the command that's tied to yes.
I also need to script it so that if they say no, it doen't give them the
Code:
echo "Congratulations, you're almost there!"
message.

Any help anyone can offer is appreciated
 
Old 11-20-2011, 07:50 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I am guessing the typo in the first line is a known (ie should be ! not 1) Also you seem to be missing quotes in the final test.

So you have said what you would like ... what issues are you having? Are you not being prompted at the correct point? Are you still getting the congrats after pressing n?
 
Old 11-20-2011, 08:07 PM   #3
Astroe
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Oh, ok so I started fiddling with it, ended up with this,

Code:
# Yes/No Prompt BASH
yn() {
printf " Reply [y/n]: "
read yn
[[ "$yn" != y ]] && [[ "$yn" != n ]] && yn
}
# OTHER CODING
# Now you pose a question
# and then execute the yn function

printf "If you'd like to install the android screencaster press y, otherwise press n."
yn
[[ $yn = y ]] && sudo wget -P ~/android-sdks/platform-tools http://androidscreencast.googlecode.com/svn/trunk/AndroidScreencast/dist/androidscreencast.jnlp && sudo chmod +x ~/android-sdks/platform-tools/androidscreencast.jnlp && echo "That's awesome! Now you have the screen caster! You can run it by using the following commands from a Terminal, in order" && echo "cd ~/android-sdks/platform-tools" && echo "javaws androidscreencast.jnlp"
And I think this bit will download the android screencast and make it executable. The folder it's being downloaded to has already been added to the .bashrc in a previous step so they'd be able to use it by just typing
Code:
javaws androidscreencast.jnlp
into the terminal.

But yeah, I was having trouble because I didn't think to add && and make yes give a string of commands. Think it's under control now though, thanks
 
Old 11-20-2011, 08:33 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Personally, when I require more than one command after the test I would use an if instead of && or || constructs (there are dangers with them that may not seem obvious).
So as an alternative:
Code:
if [[ $yn = y ]]
then
    sudo wget -P ~/android-sdks/platform-tools http://androidscreencast.googlecode.com/svn/trunk/AndroidScreencast/dist/androidscreencast.jnlp
    sudo chmod +x ~/android-sdks/platform-tools/androidscreencast.jnlp
    echo "That's awesome! Now you have the screen caster! You can run it by using the following commands from a Terminal, in order"
    echo "cd ~/android-sdks/platform-tools"
    echo "javaws androidscreencast.jnlp"
fi
Also you may wish to consider what happens when the user enters an incorrect value or even a case altered one (ie Y instead of y)

Here are a couple of helpful links to get you going:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex
 
Old 11-20-2011, 08:47 PM   #5
Astroe
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks Those links will definitely come in handy in the future.

This scripts is just for a few classmates though, and doing the entire thing manually isn't that hard, I'm just trying to make it simpler, not baby-proof it
 
  


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
Bash scripting with sudo and password prompt, and accepting user input. Jetso Programming 14 07-31-2011 12:31 PM
grep'ing and sed'ing chunks in bash... need help on speeding up a log parser. elinenbe Programming 4 04-22-2009 10:17 AM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 08:46 AM
LXer: The year head: The shift to scripting and agility LXer Syndicated Linux News 0 01-11-2007 11:54 AM
Multi-monitor Issues on RH9, Geforce 4 Ti Dual Head + TNT2 Single Head the letter b Linux - Newbie 3 12-04-2004 11:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:52 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