case structure in .sh file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting case structure in .sh file
# 1  
Old 09-28-2010
case structure in .sh file

Hi,

Code:
while true
    do
        printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
        read MIN

        case "$MIN" in
                [0-9]|[0-5][0-9]|s) break 2;;
            *) 
        echo ""
     echo "Invalid minutes, please try again.";;
        esac
    
    done

In the above case structure, if the user doesnt give any value (simply press enter) then, it should also accept.

How it can be done ?

With Regards

Last edited by Scott; 10-04-2010 at 11:20 AM.. Reason: Code tags
# 2  
Old 09-28-2010
Quote:
Originally Posted by milink
...
while true
do
printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
read MIN

case "$MIN" in
[0-9]|[0-5][0-9]|s) break 2;;
*)
echo ""
echo "Invalid minutes, please try again.";;
esac

done

In the above case structure, if the user doesnt give any value (simply press enter) then, it should also accept.
...
Code:
$
$ cat f25.sh
#!/usr/bin/bash
while true
do
 printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
 read MIN
 case "$MIN" in
   [0-9]|[0-5][0-9]|s|"") break 2;;
   *)
     echo ""
     echo "Invalid minutes, please try again.";;
 esac
done
$

Add null string in your first case condition.

Code:
$
$ ./f25.sh
Please enter the minutes (0-59):
$
$

tyler_durden
# 3  
Old 09-28-2010
Hi,

I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each line having same number of "~"s. so i need to check each line is having same "~" or not . my script gives me output like

**output**
Code:
total =        2
linelength=       62
14
linelength=       62
28

my script

Code:
#!/bin/bash
DATA_DIR=/export/opt/rtrupld/autosys/scripts_old
data=`ls $DATA_DIR/count.txt`

#     count1=1
var=`wc -l < $data`
#var2=`expr $var + 1`
echo "total = $var"
#var3=14

tildacount=0
while read line
do
   count=0
   linelen=`echo "$line"|wc -c`
 
echo "linelength= $linelen"
   if [ $linelen -gt 0 ]
     then 

# echo $linelen
       until [ $count -ge $linelen ]
     do
         count=`expr  $count + 1 `
         char=`echo "$line"|cut -b"$count"`
         if [ "$char" = "~" ]
         then
            tildacount=`expr  $tildacount + 1`
 
     fi
 
     done
     echo $tildacount

  fi
done < $data
#echo $tildacount


Last edited by Scott; 10-04-2010 at 11:21 AM.. Reason: Code tags
# 4  
Old 09-29-2010
Hi,

As you have suggested, I added the space but showing error as below :

Code:
check_minutes()
{
    
    while true
    do
        printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
        read MIN

        case "$MIN" in
		[0-9]|[0-5][0-9]|"") break 2;;
	      *) 
		echo ""
	 echo "Invalid minutes, please try again.";;
        esac
	
    done

Error :

Please enter the minutes (0-59):
Code:
./install_cron_export_gps.sh: line 92: [: =: unary operator expected

minute =


Last edited by Scott; 10-04-2010 at 11:22 AM.. Reason: Code tags
# 5  
Old 10-01-2010
Hi,


Code:
check_minutes()
{
    
    while true
    do
        printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
        read MIN

        case "$MIN" in
		[0-9]|[0-5][0-9]) break 2;;
	      *) 
		echo ""
	 echo "Invalid minutes, please try again.";;
        esac
	
    done

In the abov, how to match the blank space in the case structure ?
Means, if user simply enters <enter> without entering any value then it should also accept in the above case structure.

For number range [0-9]|[0-5][0-9] is used , but how to use blank space in the criteria ?

How it can be done ?

With Regards

Last edited by Scott; 10-04-2010 at 11:22 AM.. Reason: Code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case structure combined with standard input

Hi folks, I am new to bash scripting so please excuse my question. Is there any chance to combine a case structure with the read command? Like case (read -p "" variable) in x) Thx! (7 Replies)
Discussion started by: haukee
7 Replies

2. Red Hat

Copy certain file types recursively while maintaining file structure on destination?

Hi guys, I have just been bothered by a fairly small issue for some time now. I am trying to search (using find -name) for some .jpg files recursively. This is a Redhat environment with bash. I get this job done though I need to copy ALL of them and put them in a separate folder BUT I also... (1 Reply)
Discussion started by: rockf1bull
1 Replies

3. UNIX for Dummies Questions & Answers

Fall through case structure

Hello Experts, I was wondering how can we get a fall trough case structure. Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower. I tried this: #!/bin/sh v1=1... (6 Replies)
Discussion started by: hkansal
6 Replies

4. UNIX for Dummies Questions & Answers

ksh case structure

Hello Experts, I ve been trying to build another shell where I am using the following code. transact="tv5cpc1" case "$transact" in "...cp..") xActType="" ;; "...de..") xActType="sp_dep" ;; "...ep..") xActType="sp_epa" ;; "....v.") ... (4 Replies)
Discussion started by: hkansal
4 Replies

5. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

6. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

7. Shell Programming and Scripting

file structure

Hi, Is there a command that can help me to determain a unix file structure ? Thanks (7 Replies)
Discussion started by: yoavbe
7 Replies

8. Shell Programming and Scripting

Case structure

Hi, Can anyone help me with the following case structure? echo "Please enter the date for which you want the Report:\c" read dat d1=`echo $dat|cut -c7-8` m1=`echo $dat|cut -c5-6` y1=`echo $dat|cut -c1-4` yr=`expr "$d1" - 1` case "$yr" in 0) MONTH=`expr "$m1" - 1`... (4 Replies)
Discussion started by: kamitsin
4 Replies

9. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

10. UNIX for Dummies Questions & Answers

Problem w. case structure

Hello, I am having a problem setting a range of numbers for the "case" structure. I can use with no problems, but when I use it doesn't work??? Does the case struture allow numeric ranges? eg: echo -e "enter number between 0 and 60: \c" read $answer case $answer in ) echo... (2 Replies)
Discussion started by: Joe54321
2 Replies
Login or Register to Ask a Question