Sponsored Content
Full Discussion: Case statement - continue
Top Forums Shell Programming and Scripting Case statement - continue Post 302993317 by drysdalk on Wednesday 8th of March 2017 02:54:10 PM
Old 03-08-2017
Hi,

Here's a code example combining case, for and if:

Code:
$ cat script.sh
#!/bin/bash

for animal in `/bin/ls animals`
do
        case "$animal" in
                "cat")
                        echo "This is an acceptable animal: $animal"
                        ;;
                "dog")
                        echo "This is another cceptable animal: $animal"
                        ;;
                *)
                        if [ "$animal" == "furby" ]
                        then
                                #Let's skip this - it's not a real animal
                                continue
                        else
                                echo "And this is also an acceptable animal: $animal"
                        fi
                        ;;
        esac
done
$ ls animals
cat  dog  furby  monkey  zebra
$ ./script.sh
This is an acceptable animal: cat
This is another cceptable animal: dog
And this is also an acceptable animal: monkey
And this is also an acceptable animal: zebra
$

When we run the script, the effect of the continue statement when animal is equal to "furby" is to skip at that point entirely to the next anmal in the list, which is "monkey". So we only see "cat", "dog", "monkey" and "zebra" mentioned in the output, with the continue causing "furby" to be skipped in the output entirely.

Hope this helps clear up how these things can be used in combination.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies

2. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies

3. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

4. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

5. UNIX for Dummies Questions & Answers

CASE statement

Hi, I am writing a bash shell script. My script has a few user defined parameters. When the script runs the first thing it does is make sure that these parameters are valid. One of the parameters is called YEAR. A valid input for YEAR can be 1997-2000. One way I have come up with to ensure... (3 Replies)
Discussion started by: msb65
3 Replies

6. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

7. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

8. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

9. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies
Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild(User Contributed Perl DocumentatMoose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild(3)

NAME
Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild - Builder methods and lazy_build VERSION
version 2.1202 SYNOPSIS
package BinaryTree; use Moose; has 'node' => (is => 'rw', isa => 'Any'); has 'parent' => ( is => 'rw', isa => 'BinaryTree', predicate => 'has_parent', weak_ref => 1, ); has 'left' => ( is => 'rw', isa => 'BinaryTree', predicate => 'has_left', lazy => 1, builder => '_build_child_tree', ); has 'right' => ( is => 'rw', isa => 'BinaryTree', predicate => 'has_right', lazy => 1, builder => '_build_child_tree', ); before 'right', 'left' => sub { my ($self, $tree) = @_; $tree->parent($self) if defined $tree; }; sub _build_child_tree { my $self = shift; return BinaryTree->new( parent => $self ); } DESCRIPTION
If you've already read Moose::Cookbook::Basics::BinaryTree_AttributeFeatures, then this example should look very familiar. In fact, all we've done here is replace the attribute's "default" parameter with a "builder". In this particular case, the "default" and "builder" options act in exactly the same way. When the "left" or "right" attribute is read, Moose calls the builder method to initialize the attribute. Note that Moose calls the builder method on the object which has the attribute. Here's an example: my $tree = BinaryTree->new(); my $left = $tree->left(); When "$tree->left()" is called, Moose calls "$tree->_build_child_tree()" in order to populate the "left" attribute. If we had passed "left" to the original constructor, the builder would not be called. There are some differences between "default" and "builder". Notably, a builder is subclassable, and can be composed from a role. See Moose::Manual::Attributes for more details. The lazy_build shortcut The "lazy_build" attribute option can be used as sugar to specify a whole set of attribute options at once: has 'animal' => ( is => 'ro', isa => 'Animal', lazy_build => 1, ); This is a shorthand for: has 'animal' => ( is => 'ro', isa => 'Animal', required => 1, lazy => 1, builder => '_build_animal', predicate => 'has_animal', clearer => 'clear_animal', ); If your attribute starts with an underscore, Moose is smart and will do the right thing with the "predicate" and "clearer", making them both start with an underscore. The "builder" method always starts with an underscore. You can read more about "lazy_build" in Moose::Meta::Attribute CONCLUSION
The "builder" option is a more OO-friendly version of the "default" functionality. It also separates the default-generating code into a well-defined method. Sprinkling your attribute definitions with anonymous subroutines can be quite ugly and hard to follow. AUTHORS
o Stevan Little <stevan.little@iinteractive.com> o Dave Rolsky <autarch@urth.org> o Jesse Luehrs <doy@tozt.net> o Shawn M Moore <code@sartak.org> o XXXX XXX'XX (Yuval Kogman) <nothingmuch@woobling.org> o Karen Etheridge <ether@cpan.org> o Florian Ragwitz <rafl@debian.org> o Hans Dieter Pearcey <hdp@weftsoar.net> o Chris Prather <chris@prather.org> o Matt S Trout <mst@shadowcat.co.uk> COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.18.2 2014-01-19 Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild(3)
All times are GMT -4. The time now is 09:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy