Sponsored Content
Top Forums Shell Programming and Scripting Having trouble getting my interactive perl script to work properly Post 302997034 by Eric1 on Friday 5th of May 2017 10:06:38 PM
Old 05-05-2017
Having trouble getting my interactive perl script to work properly

So I'm making an interactive perl script, but I can't get it to work properly. I'm trying to make a script that tell the user to input either 'q' or 'Q' to terminate the program, or 'c' to continue it. If they input anything other than those three keys, it should prompt the user again and again until they input an appropriate key. If they input a 'q' or 'Q' the program is terminated. If they input 'c' then it continues to do two functions. The second function I'm saving for later, but the first function involves the program asking the user for two three dimensional coordinates, and then computing and outputting a distance. If any of the users inputs weren't a number it would display an error message. This is the code I have set up for all this, but everytime I input anything in the beginning when it asks for the 'q', 'Q', or 'c' inputs it just repeatedly asks the user to keep inputting something, and never exiting or continuing on to the function, even if I input a 'q', 'Q', or 'c'. Any idea what I need to do to fix this, and if there's anything else wrong with my code?

Code:
use warnings;
use strict;
use Scalar::Util 'looks_like_number';
my $i;
$i=0;
while ($i==0)
{
        print "Welcome to an interactive Perl program. Enter either q or Q to terminate the program or c to continue. Enter your key now: ";
        my $input;
        $input = <STDIN>;
        if (($input ne "q") || ($input ne "Q") || ($input ne "c"))
        {
                print "Welcome to an interactive Perl program. Enter either q or Q to terminate the program or c to continue. Enter your key now: ";
        }
        elsif (($input eq "q") || ($input eq "Q"))
        {
                exit;
        }
        else
        {
                print "Please enter one x coordinate: ";
                my $x1;
                $x1 = <STDIN>;
                print "Please enter one y coordinate: ";
                my $y1;
                $y1 = <STDIN>;
                print "Please enter one z coordinate: ";
                my $z1;
                $z1 = <STDIN>;
                print "Please enter a second x coordinate: ";
                my $x2;
                $x2 = <STDIN>;
                print "Please enter a second y coordinate: ";
                my $y2;
                $y2 = <STDIN>;
                print "Please enter a second z coordinate: ";
                my $z2;
                $z2 = <STDIN>;
                my $answer;
                if (($x1 !~ /^-?0/ && looks_like_number($x1)) && ($y1 !~ /^-?0/ && looks_like_number($y1)) && ($z1 !~ /^-?0/ && looks_like_number($z1)) && ($x2 !~ /^-?0/ && looks_like_number($x2)) && ($y2 !~ /^-?0/ && looks_like_number($y2)) && ($z2 !~ /^-?0/ && looks_like_number($z2)))
                {
                        $answer = sqrt(((($x2)-($x1))**2)+((($y2)-($y1))**2)+((($z2)-($z1))**2));
                        print "The distance between the two points is $answer";
                }
                else
                {
                        print "Error, you must only input numbers.";
                }
        }
}


Last edited by RudiC; 05-06-2017 at 02:55 AM..
 

9 More Discussions You Might Find Interesting

1. HP-UX

pstat_getdisk() call doesn’t work properly in HPUX 11.31 (11i V3)

As per the man page, pstat_getdisk() call returns the number of instances, which could be 0 upon successful completion, otherwise a value of -1 is returned. Please have a look at this sample program -> #include <stdio.h> #include <sys/pstat.h> int main() { int j = 0, ret; struct... (2 Replies)
Discussion started by: sandiworld
2 Replies

2. Solaris

Solaris Network doesn't work properly

Hi to all! I want to learn step by step easily how to configure my Solaris for network. I know alot about Solaris Network configuration. But I have some problems. When I install Solaris, and I plug-in my network cable to Solaris. Then I run: ifconfig -a plumb then I do ifconfig bge0 dhcp... (7 Replies)
Discussion started by: SecureXCode
7 Replies

3. UNIX for Dummies Questions & Answers

Big Trouble At Work!

People are misbehaving and doing things on the network they shouldn't be while at work, and it's killing the server. So the boss wants a plan on how to deal with these issues ASAP. What i need to know is how to use iptables to filter traffic coming to a specific ip... to say "this is allowed to... (1 Reply)
Discussion started by: ShawnaB
1 Replies

4. Shell Programming and Scripting

A script doesnt work properly when crontab

Hi, I have a script which does a tar and sends it to another server as backup. Script is as below # Locations to be backed up. Seperate by space BACKUP_LOCATIONS=/repos/subversion BACKUP_BASE_FOLDER=/bakpool BACKUP_FILE_NAME_ROOT=svn-backup START_TIME_DISP=`date` START_TIME=`date... (11 Replies)
Discussion started by: digitalrg
11 Replies

5. UNIX and Linux Applications

Gvim doesn't work properly

Hello All, I am using gvim ( redhat linux machine). backspace doesnot work properly. can some boby suggest a solution ? i have checked with older version. backspace works in it. Thanks Shiv (1 Reply)
Discussion started by: shiv.emf
1 Replies

6. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

7. Red Hat

TAB button doesn't work properly

Hi, I have list of files under a directory as shown below. /home/root -rw-r--r-- 1 user 0 Aug 27 06:08 rough.txt -rw-r--r-- 1 user 0 Aug 27 06:08 test.sh Now when i use to read the rough.txt, i did cat r and then pressed TAB button. Below is the resulting string after pressing TAB... (2 Replies)
Discussion started by: vel4ever
2 Replies

8. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

9. UNIX for Beginners Questions & Answers

Can't get mv to work properly

Trying to use mv in a shell script but for some reason this does not work: for f in *.wav;do mv $f $f.bwf;done I get this: usage: mv source target mv source ... directory So it's like I'm using 'mv' wrong but I can't see how. This works so the contens of the folder is read... (4 Replies)
Discussion started by: Oortone
4 Replies
SCRIPT(1)						    BSD General Commands Manual 						 SCRIPT(1)

NAME
script -- make typescript of terminal session SYNOPSIS
script [-adkpqr] [-F pipe] [-t time] [file [command ...]] DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript. If the argument command is given, script will run the specified command with an optional argument vector instead of an interactive shell. The following options are available: -a Append the output to file or typescript, retaining the prior contents. -d When playing back a session with the -p flag, do not sleep between records when playing back a timestamped session. -F pipe Immediately flush output after each write. This will allow a user to create a named pipe using mkfifo(1) and another user may watch the live session using a utility like cat(1). -k Log keys sent to the program as well as output. -p Play back a session recorded with the -r flag in real time. -q Run in quiet mode, omit the start, stop and command status messages. -r Record a session with input, output, and timestamping. -t time Specify the interval at which the script output file will be flushed to disk, in seconds. A value of 0 causes script to flush after every character I/O event. The default interval is 30 seconds. The script ends when the forked shell (or command) exits (a control-D to exit the Bourne shell (sh(1)), and exit, logout or control-D (if ignoreeof is not set) for the C-shell, csh(1)). Certain interactive commands, such as vi(1), create garbage in the typescript file. The script utility works best with commands that do not manipulate the screen. The results are meant to emulate a hardcopy terminal, not an addressable one. ENVIRONMENT
The following environment variables are utilized by script: SCRIPT The SCRIPT environment variable is added to the sub-shell. If SCRIPT already existed in the users environment, its value is overwrit- ten within the sub-shell. The value of SCRIPT is the name of the typescript file. SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). SEE ALSO
csh(1) HISTORY
The script command appeared in 3.0BSD. The -d, -p and -r options first appeared in NetBSD 2.0 and were ported to FreeBSD 9.2. BUGS
The script utility places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects. It is not possible to specify a command without also naming the script file because of argument parsing compatibility issues. When running in -k mode, echo cancelling is far from ideal. The slave terminal mode is checked for ECHO mode to check when to avoid manual echo logging. This does not work when the terminal is in a raw mode where the program being run is doing manual echo. If script reads zero bytes from the terminal, it switches to a mode when it only attempts to read once a second until there is data to read. This prevents script from spinning on zero-byte reads, but might cause a 1-second delay in processing of user input. BSD
December 4, 2013 BSD
All times are GMT -4. The time now is 05:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy