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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-17-2011, 12:39 PM
|
#1
|
|
LQ Newbie
Registered: May 2011
Posts: 6
Rep:
|
bash scripting
im writing a bash script to dip my toe into programming but i need syntax help
pseudocode
x point
echo "do you accept (Y/N)"
if y continue
if n exit
else go to x point [or] (else ignore)
what bash commands would i use to make this happen
|
|
|
|
05-17-2011, 12:48 PM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
You might want to check out one of the numerous on line tutorials such as this one first:
http://linuxconfig.org/Bash_scripting_Tutorial
|
|
|
|
05-17-2011, 01:19 PM
|
#3
|
|
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,958
|
First, your question should have been posted in the "Programming" forum, not here.
Second, bash, as a scripting language, executes a sequence of commands which, in principal, you could run from a command prompt.
So, the answer to your question is another question: What command(s) would you issue from a terminal (command-prompt window) to "move to a point?"
Note that, in your question, you have not told us what you want "moved" to the "point," nor have you told us what you want to do when it "gets" to the point.
Now I know that the above is not helpful, for which I apologize, so here's a guess: You're looking for a window manipulation method, and the "points" of which you speak are coordinates inside the window. If that's what you're look to accomplish from a command line, you need to be looking at a windowing system. For example, you could install the tool control language (tcl) and wish, and then use those commands to create and manipulate windows.
|
|
|
|
05-17-2011, 01:27 PM
|
#4
|
|
LQ Newbie
Registered: May 2011
Posts: 6
Original Poster
Rep:
|
ok ill repost in programming
this thread can be deleted
|
|
|
|
05-17-2011, 01:32 PM
|
#5
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
Quote:
Originally Posted by ninjafairy
ok ill repost in programming
this thread can be deleted
|
This thread will not be deleted, it will only be moved to the appropriate forum. I will report it to be moved for you.
Cheers,
Josh
|
|
|
|
05-17-2011, 01:33 PM
|
#6
|
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,466
Rep: 
|
Quote:
Originally Posted by ninjafairy
ok ill repost in programming
this thread can be deleted
|
Please don't do that; I've reported your thread to be moved to Programming. In future, if you've posted in the wrong place, just use the "Report" button to ask the moderators to move the thread for you.
|
|
|
|
05-17-2011, 01:36 PM
|
#7
|
|
LQ Newbie
Registered: May 2011
Posts: 6
Original Poster
Rep:
|
oh i did not know that will do from now on
thanks
|
|
|
|
05-17-2011, 01:41 PM
|
#8
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
As far as your original post though, here is an excerpt from one of my scripts:
Code:
read -p " place wlan0 into monitor mode? (yn) " answer
while true
do
case $answer in
[yY]* ) echo -e "\033[34m ---placing wlan0 into monitor mode..."
airmon-ng start wlan0
break;;
[nN]* ) break;;
* )
esac
done
I used that to ask a general yes or no question, and if Y or y is inputted, it executes airmon-ng. The tutorial that MensaWater posted is a good one to read and to understand and learn how to do bash scripting.
Cheers,
Josh
|
|
|
|
05-17-2011, 07:42 PM
|
#9
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Quote:
Originally Posted by ninjafairy
im writing a bash script to dip my toe into programming but i need syntax help
pseudocode
x point
echo "do you accept (Y/N)"
if y continue
if n exit
else go to x point [or] (else ignore)
what bash commands would i use to make this happen
|
You don't use goto's in bash scripting.
Code:
if command
then
this is run if "command" was successful
elif command
this is run if the first command failed but this one succeeded
else
this is run if none of the commands succeeded
fi
while command
do
this is run over and over while "command" is successful. "command" is run before every iteration.
done
for myvar in list
do
this code is run for every item of the list, where $myvar contains the current item
done
The "test" command (or it's alias, "[") is often used for if and while conditions. See "man test" for more info.
Last edited by MTK358; 05-17-2011 at 07:44 PM.
|
|
|
1 members found this post helpful.
|
05-18-2011, 08:59 AM
|
#10
|
|
Member
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackware64 13.37-multilib
Posts: 518
Rep:
|
another approach is functions; Putting the stuff together would be
Code:
ask_question()
{
valid="no"
while [[ "$valid" != "no" ]]
do
echo -n "Can you give a valid answer? [y/n]: "
read answer
case $answer in
[yY]*) valid="yes"; break;
[nN]*) valid="yes"; break;
*) valid="no"; break;
esac
done
}
while true
do
ask_question
done
There are a few things to keep in mind:
- most likely the answer given in $answer is not carried over to the main part of your script; This is because in some cases bash spawns a sub-shell. I generally use Korn shell (ksh) to overcome that problem.
- a function must exist prior to calling it; so your functions are at the top of your script, the "body" at the bottom.
|
|
|
|
05-18-2011, 09:51 AM
|
#11
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
@Ramurd
Your script has two issues.
First, it should be "while [[ "$valid" = "no" ]]" instead of "while [[ "$valid" != "no" ]]".
Second, it appears that you take "Y" followed by any characters as a "yes". That means that if the users says "yffngfdgsnr", it will be taken as a "yes".
|
|
|
|
05-18-2011, 10:00 AM
|
#12
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
@Ramurd: the case statement in the while loop in the function breaks in every case so the loop never repeats ... but the while loop in the body repeats infinitely ...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:12 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|