Sponsored Content
Full Discussion: Case inside While read File
Top Forums UNIX for Beginners Questions & Answers Case inside While read File Post 303008192 by bakunin on Tuesday 28th of November 2017 10:04:36 AM
Old 11-28-2017
Quote:
Originally Posted by pradyumnajpn10
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:

Code:
program1 | program2 | program3 | ....

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:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

case command inside awk/nawk

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)
Discussion started by: abdulaziz
9 Replies

2. Shell Programming and Scripting

Case inside case?

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)
Discussion started by: RAFC_99
3 Replies

3. UNIX for Advanced & Expert Users

how to grep/read a file inside compressed tgz without extract?

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)
Discussion started by: mayshy
3 Replies

4. Shell Programming and Scripting

multiple lines inside a case statement

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)
Discussion started by: gotam
2 Replies

5. UNIX for Advanced & Expert Users

how to read a file inside jar

how to read the text file inside the jar without extracting jar. (3 Replies)
Discussion started by: karthikn
3 Replies

6. Shell Programming and Scripting

Call function inside CASE

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)
Discussion started by: cvsanthosh
2 Replies

7. Shell Programming and Scripting

Nested case inside awk

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)
Discussion started by: cvsanthosh
2 Replies

8. Homework & Coursework Questions

How to read user keyboard input inside the case?

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)
Discussion started by: femchi
2 Replies

9. Shell Programming and Scripting

Update file record inside read loop

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)
Discussion started by: ysrini
1 Replies

10. Shell Programming and Scripting

Use sqlplus statement inside case sentence

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)
Discussion started by: Vares
3 Replies
All times are GMT -4. The time now is 07:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy