Line 1 - good morning.
Line 2 - Enter option 1 to add.
Line 3 - option q to quit.
Line 4 - "while (loop) - Whatever is entered is not equal to q or quit.
Line 5 - do (part of while loop) - read "Enter" ? (not sure)
Line 6 - "case" - Whatever is entered, is input?? (also not sure)
The rest I understand. The punch line is that sum=$((num1 + num2)) will perform the general addition.
And the sum, sum= echo "$sum" will be sent to standard output.
Line 1 - good morning.
Line 2 - Enter option 1 to add.
Line 3 - option q to quit.
Line 4 - "while (loop) - Whatever is entered is not equal to q or quit.
Line 5 - do (part of while loop) - read "Enter" ? (not sure)
Line 6 - "case" - Whatever is entered, is input?? (also not sure)
The rest I understand. The punch line is that sum=$((num1 + num2)) will perform the general addition.
And the sum, sum= echo "$sum" will be sent to standard output.
Yes and no. I think you are missing the point, though. Programs are read like onions, peeling layers of loops off. Anyway, the script is poorly written and you should understand that, as we go along. A big part of programming is to imagine what could happen and proactively deal with it. Not logic, but foresight:
Just Output, not interesting.
Now this is more interesting: This loop (and everything inside) is executed over and over again, as long as the condition is true: "$Enter" is not equal "q". Lets see what this "everything inside" is:
First some input is read from the keyboard. Then the "case .. esac" starts. This basically has a branch, which is executed when variable "Enter" has a cerain value:
Now, why is this script poorly written: first, you ask the user to do something, which is fine. But you use some code the first time and other code for all subsequent executions of the while-loop! You should move this output into the loop so that it is executed every time, not only the first time, then you can skip the last "echo":
Second, you deal with "Enter" being "q" and "1", but what do you do if neither is entered? You should at least tell the user that you can't understand what he wants. Let us enter another branch for this, which deals with all other values, which are neither "1" nor "q":
Third: if you ask the user to enter "q" to quit you may want to allow "Q" as well. Some users have the CapsLock-key turned on without noticing, some are simply conviced that Caps work better. ;-)
Now the "exit" is executed if "$Enter" either holds "q" or "Q".
Fourth: you take the variable "Enter" (and the others) for granted, but it isn't (and they aren't). It is good style to declare what you use before you use it. This also allows to comment on the usage of a variable so that the maintainer of the program can more easily change it should the need arise. In addition a declaration will make sure that the variable has the expected scope, once you start using functions and procedures. It is never too early to start developing good habits.
Speaking of good habits: always use a "shebang" in the first line, so your script will be executed by the shell of your choice, not by the one the user happens to use:
Lastly, now that we have covered all the possible inputs in the case..esac we do not need the exit-condition for the while-loop any more. We can change it to an endless loop:
I have a file which has data in 8 lines:
10
34
6
1
4
46
67
31
I am trying to read each value into each different variable so that I use these variables in preparing my report.
Another option is to dynamically print each line values in the report like below:
users with privA:... (2 Replies)
I am reading a pipe delimted file having the 4 columns. The file content looks like
APT|string|Application ID For App|value
for env in `cat $ConfigFile`
do
name=`echo $env | cut -d '|' -f1`
type=`echo $env | cut -d '|' -f2
prompt=`echo $env | cut -d '|' -f3`
... (5 Replies)
Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK.
However, sometimes an... (3 Replies)
Hi!
I have a file which has a list of host, I want to ssh to each of the first box for an app and grep through the logs for version of the application and look for a certain string "Connection is open" in the log file. The file I am referring to looks like this -
... (1 Reply)
Hi,
I have a script, running on some outside firwall server and it's log of success or failure is maintained in a file.
I want to write a script which ftp that server and reads that file and checks the logs and if failure , I will send mail notification.
Please let meknow if I am not... (1 Reply)
I have a code given below...
ERROR=`grep "Job Status" ${LOG_FILE}`
ERROR=${ERROR##*\(}
ERROR=${ERROR%%\)*}
if
then
echo "The job completed successfully"
EXIT_STATUS=0
else
echo "The job failed"
EXIT_STATUS=1
fi
can anybody tell me what is
ERROR=${ERROR##*\(}... (1 Reply)
All Expert,
I am using Sun OS 5.8 and Perl version 5 in One server and Perl 5.8 in another unix server.
I am able to read a file using fopen function of perl --file size having more then 3 GB of data.(In the machine where Perl 5.8 install)
But when i am running the same perl script --It... (1 Reply)
hi,
I have a file containing names, say n number of names,
sample file
robin
smith
dallas
frey
cook
all these names are in a file name called names.txt and it is placed in a directory called /data/names
all i want is to write a script, which will read from the file and gives the... (3 Replies)
Hi,
I'm trying to read a file in with and assigne the stream to a char * type. I've manged it using the cin.get returning type char, but am having run-time problems returning a char *. For example,
char *pStream = "file.txt";
ifstream from(pStream);
from.open(pStream);
... (1 Reply)
#!/usr/bin/sh
echo "Enter reason:"
echo "> \c"
read $reason
$reason >> access.log
This doesnt work for me. Can someone tell me how I would read the input from what the person types, and then append that to the log file?
Regards (2 Replies)