Need your guidance for case statement. I tried many way but no success yet.Now my existing code is doing something like below. Each Line of the input file contains one test case.
Now I want to modify the code so that it will ask for User Input to continue or not with the next line/next test case. If only YES it process the next Line from Input file else Exit .
I tried with below code but unfortunately it iterates infinitely. Please guide.
BR,
PD
Your second read is reading from the same descriptor as the first, meaning both reads are reading your input file, meaning that your case will never get the answer you are looking for and the inner while-loop will never break (you can test that by adding a line to your input file with only an n in it, at which point the script will exit).
You need to change one of the reads to read from a different descriptor. There's a number of ways to do this. One way, for example, is to change your second read to:
Thank you for pointing me to the right direction. It worked for me.
Though I searched a lot from morning I could not able to find that you have mentioned now I got it 2 O clock midnight .
Though I would like to know how an expertise like you will prefer for that sort of logic .
Though I would like to know how an expertise like you will prefer for that sort of logic .
Just the way Scott has pointed out to you - it is one of the "correct" ways to solve that problem.
Something you might think about is to "structure" your program differently, but this has nothing to do with actual code. It is a way of looking at how programs (scripts) do what they should do and is basically a "philosophical" topic: the general UNIX program (again - any program, be it written in shell or any other language) is designed to work inside of a pipeline:
For this to work a program needs to have some defined input (a file, a stream, ...) and a defined output. Picture it like a garden hose: you pour something in on top and something (else) runs out at the bottom. This "something else" then serves as the input stream for the next program until you finally get what you want out of the last one.
In order to work like this you need to avoid interactive input in any way. When you rely on interactive input you are limiting the possible usage of your program to a situation where a human sits at a screen and types something in (or provides input in another form, like clicking with a mouse). The program working in a pipeline will not be limited to this situation because it can be started and run automatically without any human intervention.
This is not to say that you don't need any interactive programs: it just means you should carefully think about which category your program - really - needs to be in. You should write programs that need manual intervention only if there is no other way at all. If it is possible to do it automatically you should do so, even if it means more work: you do this work only once whereas you do manual intervention every time the program runs.
This (and more) is discussed in a book i can heartily recommend: Advanced Programming in the UNIX Environment by W. Richard Stevens. One of the best books ever written about programming.
I hope this helps.
bakunin
Last edited by Scott; 11-28-2017 at 02:24 PM..
Reason: Fixed link
These 4 Users Gave Thanks to bakunin For This Post:
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Apologies to jump in here with such a lengthy post.
Quote:
Originally Posted by bakunin
This (and more) is discussed in a book i can heartily recommend: Advanced Programming in the UNIX Environment by W. Richard Stevens. One of the best books ever written about programming.
And I heartily agree with that.
However, that 700-page tome might not be the best place for shell scripting practices. If one is interested in shell programming, the following books are some that we discussed in classes that I lead, and that I have in my library (and there are other, newer books, as well):
Best wishes ... cheers, drl
Note that The Art of UNIX Programming can be read on-line. For example, this chapter: Taxonomy of Unix IPC Methods
Stevens' book is available in Kindle format.
The bash Cookbook was co-written by a Cray employee with whom I worked and coincidently lives nearby.
The Unix Programming Environment is still in print, even after 30 years !
Apologies to jump in here with such a lengthy post.
You always have something interesting to contribute, so: you're welome!
Quote:
Originally Posted by drl
However, that 700-page tome might not be the best place for a shell scripting practices.
You noted some very good books and, yes, if we are talking shell programming they might be better places to look at compared to the Stevens. On the other hand - and this was my implicit point - I think there is something about how "good" programs are written which is absolutely independent of the programming language employed. Regardless of writing in assembler or the most high-level programming language there are some practices, some dos and don'ts which should be known by everybody before s/he even attempts "hello world". This is what Stevens talks about.
It might be easier to first read Eric Raymonds TAOUP, but - without wanting to take away from Erics achievement - at its very essence IMHO it rehashes what W. R. Stevens already laid down in his book. This is because the principles, the "rules" of our art are eternal and cast in stone.
bakunin
These 3 Users Gave Thanks to bakunin For This Post:
Hello,
I have a problem. I want to launch a different sql queries for different shell parameter values, something like this.
#/bin/bash
case $1 in
"A")
sqlplus -s user/pass << SQL
query A;
SQL
"B") sqlplus -s user/pass << SQL2
... (3 Replies)
Hi,
I am reading file records inside a while loop,
and want to update the record when certain condition is met.
How can I update a file while being read?
I want to avoid using temporary files, copy, rename, ...
while IFS=',' read -r f1 f2
do
function(f1,f2)
if
then
<add... (1 Reply)
I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks:
Copy directory tree
Delete files or directories
Output Information (this part is done )
*Copy directory tree
The “Copy... (2 Replies)
please let me know if the below code could be written efficiently inside single awk
case "$INP" in
ksh)
cat catalog | awk 'BEGIN {FS=",";} { print $2 } END {}'
;;
pset)
cat catalog | awk 'BEGIN {FS=",";} { print $3 } END {}'
;;
dml)
cat catalog | awk 'BEGIN {FS=",";} {... (2 Replies)
i have a case statement which branches to different sections based on an input. Each branch needs to call a function. below is the code. FOr some reason, the code inside the function is not getting executed. the code is below for reference.
in the below code echo "Function 1" which is there... (2 Replies)
echo "please enter ur choice..
1. Make a file.
2. Display contents
3. Copy the file
4. Rename the file
5. Delete the file
6. Exit"
read choice
case $choice in
1 ) echo enter the file name
read fname
if
then
echo... (2 Replies)
Hi all,
I would like to ask whether in Unix shell/perl have any functions or command to allow grep/cat/read a file inside compressed .tgz without extract it?
I know we can tar tvf a compressed tgz but this only allow we read the path/filename contained inside the tarball. If we want to read... (3 Replies)
Im new to unix and shell scripting. I am required to write a program and im in the process of creating a menu system. I have my main menu but i want to be able to select an option that takes me onto another menu. I have tried doing this with the case statement with no luck so far. Wondering if it... (3 Replies)
well I found lot of topics about awk..about if command in awk..
but I had to implement this:
nawk -F"|" '
$47 ~ /0R0011/ { print > ("/home/user/M/MC.tmp" )}
$47 ~ /0R0012/ { print > ("/home/user/M/DuSI.tmp" )}
$47 ~ /0R0014/ { print > ("/home/user/M/FF.tmp" )}
$47 ~ /0R0018/ { print >... (9 Replies)