Input validation of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input validation of file
# 8  
Old 10-19-2011
Code:
ant:/home/vbe $ 003

enter a file name
004
 file does not exist
enter a file name
003
2: file name  003
file read
-rwxrwxr-x   1 vbe        bin            337 Oct 19 12:19 003
ant:/home/vbe $ more 003
#!/usr/bin/ksh

CONT=1
while [ CONT -gt 0 ]
do
   echo "enter a file name"
   read USERINPUT
   #echo "1 file name " $USERINPUT
   #if [ -e $USERINPUT ]
   if test -s "$USERINPUT"
   then
        echo "2: file name " $USERINPUT
        #read $USERINPUT
        echo "file read"
        ll $USERINPUT
        CONT=0
   else
        echo " file does not exist"
   fi
done
003: END

# 9  
Old 10-19-2011
I'm using both linux(RH) and HP-UX system for implementing this code and using ksh in both cases
And, when i'm using the above mentioned code, the o/p for a null entry (filename) is displaying all the files [ls -lrt / ll $USERINPUT] present in that directory.
Ofcourse the code is effectively validating invalid filenames. But the problem is with null input.

Last edited by sam_bd; 10-19-2011 at 08:50 AM..
# 10  
Old 10-19-2011
Sorry mate I was stressed by other issues (hard to be multitasking...)
Code:
#!/usr/bin/ksh

CONT=1
while [ CONT -gt 0 ]
do
   USERINPUT=""
   echo "enter a file name"
   echo "or CTRL-c to quit "
   read USERINPUT
   if test -s "$USERINPUT"
   then
        echo "2: file name " $USERINPUT
        #read $USERINPUT
        echo "file read"
        ll $USERINPUT
        CONT=0
   else
        echo " file does not exist or nothing entered"
   fi
done

Forgot to initialze USERINPUT...

Last edited by vbe; 10-19-2011 at 09:52 AM.. Reason: put modif in bold...
# 11  
Old 10-19-2011
Hm..the o/p is not rejecting the null input after using the updated code
Here is the o/p:
Code:
 
$./test1
enter a file name
2: file name
file read
total 116
-rw-rw-r--  1 linux1 linux1  244 Jul 21 16:42 intro_bkp
-rw-rw-r--  1 linux1 linux1  244 Jul 21 16:42 intro
.....................................................................
.....................................................................
......................................................................

It is showing all the files if there's 'null input'. i've tried with -e and
-s options in 'if test' statement.
# 12  
Old 10-19-2011
I put in bold in my previous post the initialisation that seem to not occur in your script
in which case try to echo its content straight after to see it is null...
# 13  
Old 10-19-2011
Or set the set in debug mode (set -x)
# 14  
Old 10-19-2011
My last attempt:
Code:
ant:/home/vbe $ uname -a
HP-UX ant B.11.11 U 9000/800 168921511 unlimited-user license
ant:/home/vbe $
ant:/home/vbe $ vi 003
#!/usr/bin/ksh

CONT=1
while [ CONT -gt 0 ]
do
   echo "value of USERINPUT to begin: " $USERINPUT
   USERINPUT=""
   echo "USERINPUT initialized to : " $USERINPUT
   echo "enter a file name or CTRL-c to quit "
   read USERINPUT
   echo "1- file name entered:" $USERINPUT
        #
   #    This test is for /bin/sh: if [ -e $USERINPUT ]
        #
   if test -s "$USERINPUT"
   then
        echo "2- file name looked for:" $USERINPUT
        echo "file read"
        ll $USERINPUT
        CONT=0
   else
        echo " file does not exist or nothing entered"
   fi
done
# exit
~
~
"003" 25 lines, 532 characters
ant:/home/vbe $ 003

value of USERINPUT to begin:
USERINPUT initialized to :
enter a file name or CTRL-c to quit
gagaz
1- file name entered: gagaz
 file does not exist or nothing entered
value of USERINPUT to begin:  gagaz
USERINPUT initialized to :
enter a file name or CTRL-c to quit
gaga
1- file name entered: gaga
2- file name looked for: gaga
file read
-rwxr-xr--   1 vbe        bin            339 Oct 12  2006 gaga
ant:/home/vbe $

I want you to copy exactly the shellscript as it is and execute it and post back the output
This User Gave Thanks to vbe For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Input Email ID Validation

Hi Experts, I've a script where users will input the email id. I want to loop the script until the user enter proper email id. Its validating for all condition and error out if any wrong data, but for one condition alone its not erroring out. Capture_EmailID() { echo -e "Please Enter... (1 Reply)
Discussion started by: senthil.ak
1 Replies

2. Windows & DOS: Issues & Discussions

Put the numeric validation in user input when value is 5.1.2.3

Hi I need to put the validation in batch script when user will enter the build number it should be numeric.I can put the validation for numeric values but there is .(dot) in number so it would not take it as numeric. Is it possible we can store it in variable and remove the .(dot) from the... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. Windows & DOS: Issues & Discussions

Validation in user input in batch script

I need to insert the validation in my batch script.When user enter the value it should be numeric+minimum length should be 2 for e.g. 02,03 if he puts 1a,1A,2a3 t hen should print the message that it is wrong and print te message enter valid value. Echo RC is in format of 02 set /p... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

4. Shell Programming and Scripting

perl: How to improve with minimal validation of its input??

The Code: #!/usr/bin/perl use strict; use warnings; print "Please enter numbers, separated by commas: "; my $data=<STDIN>; chomp $data; my @dataset=split(/,/, $data); my $sum = 0; foreach my $num (@dataset) { $sum += $num; } my $total_nums = scalar(@dataset); my $mean =... (1 Reply)
Discussion started by: 300zxmuro
1 Replies

5. Shell Programming and Scripting

Input Validation of comma separated values

Hello all, I am working on a script and have the first part solved of numerical input validation. Below the code validates that the input is a numerical value between 100 and 1000. If not, it errors out. Now I need to be able to read values separated by a comma. For example, instead of my... (5 Replies)
Discussion started by: LinuxRacr
5 Replies

6. Shell Programming and Scripting

how to do validation the input requirement with if/else

Hello Everyone, I have the following script; ------------------------------------------------------------------ #!/bin/ksh set HSC=$1 set SEX=$2 set EXP='export ORACLE_SID=AFISDB' if ($#argv !=2); then echo Usage $0 HSC SEX else date > modify_gender_trace.txt $EXP echo... (5 Replies)
Discussion started by: wingcross
5 Replies

7. Shell Programming and Scripting

Command line user input validation

Hi guys, I have a piece of snippet below which asks the user to input some numbers if isDatasubEnabled && isReconEnabled; then echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit" elif isDatasubEnabled &&... (4 Replies)
Discussion started by: pyscho
4 Replies

8. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

9. Shell Programming and Scripting

Input validation ?

I am trying to validate user input, at the moment what i have is as below :- echo "\n\tPlease, enter package name to import: \c" read PACKAGE So far this works perfect , But if the user does not input any thing it stalls :( What I need is, If the user does not enter the name of the... (9 Replies)
Discussion started by: systemali
9 Replies
Login or Register to Ask a Question