Results 1 to 5 of 5
I am writing a script in bash.
In this the user will input a month name in Jan, Feb format. All the 12 months are stored in an array.
I ...
- 05-21-2011 #1Just Joined!
- Join Date
- Jan 2009
- Posts
- 63
looping doubt
I am writing a script in bash.
In this the user will input a month name in Jan, Feb format. All the 12 months are stored in an array.
I want to check if the user enter correct month name and then it will match a value in the array and go out of the loop to execute something. If donot match then it exit with a message.
- 05-22-2011 #2Linux Newbie
- Join Date
- Oct 2008
- Posts
- 140
Maybe you can post what you've got so far?
- 05-22-2011 #3Just Joined!
- Join Date
- May 2011
- Posts
- 44
Perl Example
>>then it will match a value in the array
hmmm... sounds to me like you are using a hash?
Because:
However,Code:#!/usr/bin/perl my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); print $months[2] . "\n"; ------------------- Mar
Since you are need to evaluate a month as a value, a hash would be more useful than an array as you can parse the hash for existing keys, vs using an array which you will have to iterate through the array for every existing value.Code:#!/usr/bin/perl my %months = ( Jan => 5, Feb => 7, Mar => 3, Apr => 4, May => 1, Jun => 88, Jul => 23, Aug => 15, Sep => 533, Oct => 123, Nov => 1233, Dec => 2, ); print $months{'Jul'} . "\n"; ------------------------------ 23
you may assign a value to each key manually or you can build the hash pragmatically. for my example I will assume the months are keyd to their value. (since this appears to be input validation I am recommending this method, there are many other, better ways to deal with dates)
first for the array example (this turned out to be rather c like...):
now for the hash example:Code:#!/usr/bin/perl # # program takes one argument from the command line, # iterates through the array of months, and verifies the users input # # can easily be modified to take input into a variable via: # my $input = <STDIN>; use strict; use warnings; my $input = $ARGV[0]; my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $exists = -1; for (my $i = 0; $i < $#months + 1; $i++) { if ($months[$i] eq $input){ $exists = $i; last; } } if ($exists >= 0){ print "Input exists at $exists in \@months \n"; } else { print "Input does not exist"; }
Don't really know bash, hence the Perl.Code:# program takes one argument from the command line, # iterates through the array of months, in the real world I assume # the programer is going to assign specific values to the months and will use # some sort of case statement? # # can easily be modified to take input into a variable via: # my $input = <STDIN>; use strict; use warnings; # I'm building my hash here because I'm lazzy... I'm assuming the end product will # have specific key/value pairs... # written less perly for clarity my %months; my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); foreach (@months){ $months{$_} = int rand 1000; } my $input = $ARGV[0]; if ($months{$input}){ print "Entered correct Name. $input => $months{$input}"; } else { print "$input is not a valid month"; }
- 05-22-2011 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Using bash:
producing for a month not found:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate search for item in array, shell. # Utility functions: print-as-echo, print-line-with-visual-space, debug. pe() { for i;do printf "%s" "$i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; } db() { : ; } C=$HOME/bin/context && [ -f $C ] && $C m=( jan feb mar apr may jun jul aug sep oct nov dec end ) last=$(( ${#m[*]}-1 )) db " Index of last slot is $last." month=${1?" Must supply month to be searched for."} db " Searching for :$month:" pl " Results:" m[$last]=$month i=0 while [ ${m[$i]} != $month ] do (( i++ )) done if [ $i == $last ] then pe " No such month as \"$month\"" else pe " Found \"$month\" at $i";fi fi exit 0
and for the last month in the array:Code:% ./s1 xx Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") Oker|relrel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) GNU bash 3.2.39 ----- Results: No such month as "xx"
Best wishes ... cheers, drlCode:% ./s1 dec Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") Oker|relrel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) GNU bash 3.2.39 ----- Results: Found "dec" at 11
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 05-23-2011 #5Just Joined!
- Join Date
- Jan 2009
- Posts
- 63
This is what I have..Code:#/bin/bash #set -x echo "Enter the month:" read month #echo "Enter the date:" #read date declare -a monarr=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ) len=${#monarr[@]} #for i in "${monarr[@]}" for (( i = 0 ; i < $len ; i++ )) do if [ $month != ${monarr[$i]} ] then echo "Please enter the correct month" else break fi done echo "Month:$month Date:$date"


Reply With Quote