For loop in bourne shell is not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop in bourne shell is not working
# 1  
Old 03-08-2017
For loop in bourne shell is not working

I have a loop with cases
I am working on Bourne shell

Code:
 for file in *.${Today}*.csv *.${Today}*.txt\
 do
  
 case ${file} in
    sun_detail)
        do something
     ;;
    sum)
       do something
    ;;
    mod)
      do something
    ;;
    *)
     do something
    ;;

I have a 50 files, three from them fall in three cases. I thought all others should get to common case *). But the first file got to that case and program exits without errors
Could you please help me with this?


Thanks for contribution

Last edited by digioleg54; 03-08-2017 at 02:58 PM..
# 2  
Old 03-08-2017
what OS and BASH are you using? the syntax varies a little from version to version.
# 3  
Old 03-08-2017
sun_detail looks too specific, sun_detail.csv wouldn't match it for instance. Try *sun_detail*
# 4  
Old 03-08-2017
I'm afraid that code snippet can't possibly run flawlessly:
- the continuation ( \ ) between for ... and do needs to be dropped
- done missing
- esac missing

And, looking at the for construct, file will assume string values like ...csv or ...txt, which will never match sun_detail nor sum nor mod.
# 5  
Old 03-08-2017
It is just an example
Code:
  
 case $file in
      sum_detasils*$Today*.csv

Could you please tell me if continue works in case statement?

---------- Post updated at 02:09 PM ---------- Previous update was at 02:07 PM ----------

Code:
 for file in *.${Today}*.csv *.${Today}*.txt
 do
case $file in
      sum_detasils*$Today*.csv)
             do something
             continue
        ;;
 esac
 done

Continue will work?
# 6  
Old 03-08-2017
Please DON'T double post, seriously!


Quote:
Originally Posted by digioleg54
It is just an example
Please apply some care when posting - people can't tell if what you post is sloppy typing or copying or erroneous coding causing misbehaved programs.

Quote:
Continue will work?
I don't think continue is necessary -
Quote:
If the ;; operator is used, no subsequent matches are attempted after the first pattern match.
Although from man bash, this should be valid for sh, too.


EDIT: Looking at your other thread, I see that your reasoning is a bit different. Another reason for a) no double posting, b) precise and detailed posting.

Last edited by RudiC; 03-08-2017 at 03:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Alias is not working under Bourne Shell

Hi, Please assist me why HC alias is not working under this shell? root@singapore # grep HC /.profile alias HC='cd /var/tmp/HC/2015/`date +%B`' root@singapore # . /.profile Sourcing //.profile-EIS..... root@singapore # echo $HC root@singapore # HC HC: not found root@singapore # echo... (18 Replies)
Discussion started by: tprabhu1983
18 Replies

2. Shell Programming and Scripting

Shell script loop not working

Hi I'm using this script to transcode videos in an Ubuntu 12.04 machine. #! /bin/bash MOVDIR="/media/topaz_1/media/transcodes/transcode_mov/" MOVDESTDIR="/media/topaz_1/media/transcodes/final_mov/" DONEFILESDIR="/media/topaz_1/media/transcodes/dv_cache/"... (1 Reply)
Discussion started by: oscarfelson
1 Replies

3. Shell Programming and Scripting

Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

for (( i=1; i<=3; i++ )); do for (( j=1; j<=3; j++ )); do for (( k=1; k<=3; k++ )); do echo $i$j$k done done done Will the above code work on a BOURNE shell? As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using... (7 Replies)
Discussion started by: navienavnav
7 Replies

4. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

5. Shell Programming and Scripting

Shell Script not working using for loop and a funtion

Hi- Here is the shell script that for some reason is not returning results: #! /bin/ksh - avg() { AVG=0 typeset SUM=0 if then echo "You must have at least two numbers" else for NUM in "$*" do ... (2 Replies)
Discussion started by: koomba
2 Replies

6. Shell Programming and Scripting

Help on "for" loop in bourne shell

Hello Everyone.... I am trying to print a number sequence in following format using for loop. I am using a bourne shell. I tried following for loop condition but it is bash syntax. for (( i=0; i<=5; i++ )) It is giving syntax error. Kindly help with the syntax of "for"... (7 Replies)
Discussion started by: EmbedUX
7 Replies

7. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

8. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies

9. Shell Programming and Scripting

if loop problem in bourne shell

how to use if-loop in bourne shell with multiple conditions like follows if then commands fi it gives me an error test: ] missing then i put if ] it gives me an error [[ not found kindly i need the syntex for the bourne shell (5 Replies)
Discussion started by: ahmad.diab
5 Replies

10. Shell Programming and Scripting

bourne shell not working

This code has worked for years and still does in my production environment. But it's failing in my development environment now. The cd works but the creation of node1, jnum, and node2 fails. Oddly the output shows a line from from the awk script at the end of the code during the setting of each... (6 Replies)
Discussion started by: gillbates
6 Replies
Login or Register to Ask a Question