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:
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)
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)
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)
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)
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)
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 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)
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)
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)