LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-28-2006, 01:31 AM   #1
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Bash Scripting newb.. Advice needed.


Im trying to learn a little a little more about scripting so im just writing simple scripts to try and get me going. I know there is a ton of documentation available on the web but sometimes it's a pain to dig around and find what I need. At this moment I am needing simple help with case statements.

Here is my simple script:

Code:
#!/bin/bash
clear 
kv="uname -r"
ut="uptime"
echo "Press 1 to check kernel version."
echo "Press 2 to do nothing."
read userchoice 
case "$userchoice" in
1) 
	clear; echo -n "Your kernel version is: "  
	$kv;
	;;
2)
	echo "Then Shut Up!";
	;;
*)
	echo "Press 1 or 2 jacka$$!";
	;;
esac
My simple question is: How do I go back to the beginning of the script after the end the case statement? Is there a case statement command/function that does this? Or do I have recall the script within the script?

edit: in dos there is the goto command... is there something similar in bash?

Thanks!
 
Old 09-28-2006, 01:37 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
there's no goto in bash but if you want to make a loop you can
Code:
#!/bin/bash
clear 
kv="uname -r"
ut="uptime"
while :; do
	echo "Press 1 to check kernel version."
	echo "Press 2 to do nothing."
	read userchoice 
	case "$userchoice" in
	1) 
		clear; echo -n "Your kernel version is: "  
		$kv;
		;;
	2)
		echo "Then Shut Up!";
		;;
	*)
		echo "Press 1 or 2 jacka$$!";
		;;
	esac
done
OR

Code:
for ((;;)); do
	:
done

Last edited by konsolebox; 09-28-2006 at 01:41 AM.
 
Old 09-28-2006, 01:40 AM   #3
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by konsolebox
there's no goto in bash but if you want to make a loop you can
Code:
#!/bin/bash
clear 
kv="uname -r"
ut="uptime"
while :; do
	echo "Press 1 to check kernel version."
	echo "Press 2 to do nothing."
	read userchoice 
	case "$userchoice" in
	1) 
		clear; echo -n "Your kernel version is: "  
		$kv;
		;;
	2)
		echo "Then Shut Up!";
		;;
	*)
		echo "Press 1 or 2 jacka$$!";
		;;
	esac
done
while :; do ... done: Does this allow anything to loop? or just case to loop?
 
Old 09-28-2006, 01:46 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
: is also a command and it always return true so while : will always repeat. yes everything inside do and done of the while statement repeats. use 'break' to exit from the loop. ex.
Code:
while :; do
    if statementreturningtrue; then
         break
    fi
    
    # or unconditional
    break
done
 
Old 09-28-2006, 01:52 AM   #5
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by konsolebox
: is also a command and it always return true so while : will always repeat. yes everything inside do and done of the while statement repeats. use 'break' to exit from the loop. ex.
Code:
while :; do
    if statementreturningtrue; then
         break
    fi
    
    # or unconditional
    break
done

Awesome, Thanks for the info

More qeustions to come over the next couple of weeks.

Thanks, Trey
 
Old 09-28-2006, 12:05 PM   #6
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
One more small tip, it's not a bad idea to use an opening parenthesis in case statements, e.g.

Code:
(1) whatever... ;;
rather than

Code:
1) whatever... ;;
The shell ignores the ( but most text editors have a command to jump to matching brackets/parentheses etc, which is invaluable in longer scripts. A single ) will bodge up the count, so using (...) ensures that there are always matched pairs.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gmail notifier help needed (newb) budlightnmyhand Linux - Software 8 11-15-2005 09:37 AM
bash scripting --- some help needed rajsharma Linux - Software 1 09-09-2005 02:49 AM
64bit distro needed for newb Shinigami-Sama Linux - Laptop and Netbook 5 08-10-2005 12:00 AM
complete apache newb: advice ? qwijibow Linux - Security 9 09-16-2004 02:07 AM
total newb needs advice kjasant Linux - Newbie 4 04-21-2004 06:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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