LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-17-2011, 01:39 PM   #1
ninjafairy
LQ Newbie
 
Registered: May 2011
Posts: 6

Rep: Reputation: 0
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
 
Old 05-17-2011, 01:48 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
You might want to check out one of the numerous on line tutorials such as this one first:
http://linuxconfig.org/Bash_scripting_Tutorial
 
Old 05-17-2011, 02:19 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
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.
 
Old 05-17-2011, 02:27 PM   #4
ninjafairy
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: 0
ok ill repost in programming

this thread can be deleted
 
Old 05-17-2011, 02:32 PM   #5
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
Quote:
Originally Posted by ninjafairy View Post
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
 
Old 05-17-2011, 02:33 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by ninjafairy View Post
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.
 
Old 05-17-2011, 02:36 PM   #7
ninjafairy
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: 0
oh i did not know that will do from now on
thanks
 
Old 05-17-2011, 02:41 PM   #8
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
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
 
Old 05-17-2011, 08:42 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by ninjafairy View Post
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 08:44 PM.
 
1 members found this post helpful.
Old 05-18-2011, 09:59 AM   #10
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 555

Rep: Reputation: 75
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.
 
Old 05-18-2011, 10:51 AM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
@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".
 
Old 05-18-2011, 11:00 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
@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 ...
 
  


Reply


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
bash scripting and tr zunder1990 Linux - Newbie 11 03-15-2011 03:39 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Bash scripting JonCooperUK Programming 3 03-04-2004 09:55 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 02:10 AM
bash scripting -=MaGo=- Programming 16 08-30-2003 08:07 PM


All times are GMT -5. The time now is 11:56 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration