I thought that if user input is not to be used, then
the code doesn't need continue or break. If possible, may I
expect anyone to give a small algorithm or example to show
how without user input, 'continue/break' makes sense.
Here are a few continue and break examples from source code:
The isolated examples probably mean little or nothing to you. The point is programmers use break and continue all the time in situations not processing user input.
Here is a simple example using bash, showing how break and continue make sense:
You are right that break and continue are often useful for processing user input. But there are obviously many kinds of input. Input from data files. Input from arrays. Input from strings. Input from numbers. Perhaps other inputs. Those other inputs also have to be read in some way, and break / continue are helpful for processing those other inputs too. There is no "special connection" between reading user input and continue/break.
Assume you have a loop, and you have a condition to stop executing that loop before it reaches its boundaries - break out in the middle of the loop. Use continue to skip the execution of the remaining body of the loop for that single iteration:
Don Cragun, that may be a good way to test our programs by giving standard input (without specifying any file name). I had run awk programs by giving stnd input.
Now, what I want to say is:
As many told that why I see a link between 'read' and 'continue/break', I also wondered why they wrote so. Because my 1st post reflects the connection. Based on the user input read by the read statement, either 'continue' is executed or 'break' is executed. See my codes, if user inputs 'y' , 'continue' is executed orelse 'break'. I hope this should show the connection.
Now , coming to the other point :--" what is the use of 'continue/break' if awk doesn't understand 'read' statement". You all are right to say that 'continue/break' is used in awk without user interference or without user input. The reason I told becz I have used programs which execute 'continue or break' based on the input as the example I gave you. so, I thought that if user input is not to be used, then the code doesn't need continue or break. If possible, may I expect anyone to give a small algorithm or example to show how without user input, 'continue/break' makes sense.
Alister thanks to say a way of reading stnd input. I will check how it works.
The "program" in your 1st posting is not a valid shell script (although most shells will accept it without complaining. The only valid uses of break and continue are when they are used within a for loop, an until loop, or a while loop. If the script you posted:
is run using bash on OS X, it will either say:
or:
By the logic you're using above and the shell program below:
there is a link between read, exit, and printf (or any other utilities you happen to execute if the value of a condition based on the value of a variable is true or false.
What I was saying about awk is that it can be used interactively or non-interactively. I can also say that bash, ksh, and perl can be used interactively or non-interactively. I have seen a sudoku game that reads the initial values from a file and then interactively reads commands from the user to set open positions to values supplied by the user (and optionally notes all values that could be entered into any unset position based on values that have already been set in that position's quadrant, row, and column). I have seen a similar sudoku game written in ksh93. I have a lot of shell scripts that perform non-interactive updates to homegrown databases (sometimes invoking awk to perform some of the work). Classifying awk, bash, ksh, or perl as interactive or non-interactive seems to me to just show a lack of imagination.
There are many non-interactive scripts where I would find all of the above tools to be bad choices depending on what the script is supposed to do. There are many interactive scripts where I would find all of the above tools to be bad choices depending on what the script is supposed to do. There are many interactive and non-interactive scripts where any or all of the above tools would be good choices depending on what the script is supposed to do.
Look at the requirements for a project and choose appropriate tools to fulfill those requirements. Don't restrict the tools you have in your toolbox based on someone's predefined notion of how those tools are most frequently used.
And hanson44 has just supplied you with several real life examples of using break and continue in loops. Whether these uses are interactive or non-interactive depends on where the input is coming from and what else is going on in the code before and after the given fragments.
The "program" in your 1st posting is not a valid shell script (although most shells will accept it without complaining. The only valid uses of break and continue are when they are used within a for loop, an until loop, or a while loop.
Don Cragun, I have intentionally written only the read,continue and break statements . I knew the code I wrote should be within a loop(while, for, until) then only 'continue' and 'break' makes sense. In awk also , if continue and break are written without loop, it will show error. I knew all these. I thought a reader would understand that I am writing a sub-part of a loop. So, I intentionally skipped writing the loop (while or for). It should have been tacitly understood that the code I wrote is a part of a loop because then only what I am saying becomes meaningful. If 'loop' was not in my mind and simply I am writing 'continue' and 'break', it makes no sense of what I am saying.
By the way, thanks for the examples. I will go through later.
I tacitly understood the initial code was part of a loop. Personally, if I said anything too strong, I apologize. We're all just trying to help. Thank you for posting, and the discussion.
Don Cragun, I have intentionally written only the read,continue and break statements . I knew the code I wrote should be within a loop(while, for, until) then only 'continue' and 'break' makes sense. In awk also , if continue and break are written without loop, it will show error. I knew all these. I thought a reader would understand that I am writing a sub-part of a loop. So, I intentionally skipped writing the loop (while or for). It should have been tacitly understood that the code I wrote is a part of a loop because then only what I am saying becomes meaningful. If 'loop' was not in my mind and simply I am writing 'continue' and 'break', it makes no sense of what I am saying.
By the way, thanks for the examples. I will go through later.
In these kinds of fora, others can judge your level of understanding only through what you mention in your posts; rarely anyone knows anybody else personally to make any appropriate presumptions.
So, it's always better to mention such things explicitly rather than let others play a guessing game about your competence level.
These 4 Users Gave Thanks to elixir_sinari For This Post:
I Bourne shell I have a lines
#!/bin/sh
something
for file in *_${Today}*.csv *_${Today}*.txt
do
if
then
echo "This file will be processed in separate script"
continue
fi
something
done
The script doesn't understand if... (6 Replies)
greetings,
the following code isn't working as i expect it to. the first dbl brackets do but the second set gets ignored. ie: if i'm on t70c6n229 it echoes "Something" and i expect it not to. what am i missing?
if " ]] || " ]]; then
echo "Something"
fi
thanx! (9 Replies)
I ran the following grep and sed command.
grep "\t" emp.txt
sed -n '/\t/p' emp.txt
grep treated the '\' as to escape t and took the pattern as literal t whereas sed took the pattern as tab.
That means , grep doesn't understand escape sequence!!!!!!
what to do to make grep... (8 Replies)
Can some one please help me know how exactly to understand logs in sendmail 8.14.5. I am pretty new to sendmail.
I am running rhel6 on my system and i installed sendmail on it. Its just a test enivornment and i am sending and receiving emails on my localshot only.
Jun 13 18:32:38 serv1... (0 Replies)
I have the following and for some reason I can't have two options together.
I mean if I choose -u and -p it won't work... why?
#!/bin/bash
resetTime=1
mytotalTime=0
totalHour=0
totalMin=0
averagemem=0
finalaverage=0
times=0
function usage()
{
cat << EOF
USAGE: $0 file
EOF
} (10 Replies)
Hello,
after upgrading the memory to 96GB & 6 Dual Processor for P 550 ( and still not applied the parameters which some experienced posters said in post https://www.unix.com/aix/141835-oracle-database-running-slow-aix-nmon-topas-6.html )
I am getting system dumps. How to understand and... (1 Reply)
Always, I have a confuse/not understand file descriptors.
I know about the
0 for standard input
1 for standard output
2 for standard error
and 3 to 9 is a additional file descriptor but it's a theoritical knowledge.
Could you please give information about below two lines of code in AIX... (1 Reply)
Hi -
Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert.
The list file (ldaplist) would look like -
***********
o=company a
o=company b
***********
**... (7 Replies)
CO UNixware 7.1.1
Hi friends,
I have chopped my case statementt out of my .profile and put it in another script called setsid. The case statement works when run from my .profile but not from my setsid file.
All that the script does is set an environmental variable based on user input.... (7 Replies)