Vaildation of command line inputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Vaildation of command line inputs
# 1  
Old 07-17-2006
Vaildation of command line inputs

Hi,

I want to do the following validations in my script when my script gets 2 parameters as command line inputs.

My script expects 2 inputs : a -f option and a filename

If a filename is given as input without the -f option then I have to exit. If only -f option is given and no filename is given then also I have to exit.

Once I get both the inputs I have to check whether the file has read permissions or not.

Please provide me a snippet of code as of how to do this.

Thanks in advance
# 2  
Old 07-17-2006
Let us see what you've tried!
# 3  
Old 07-17-2006
Check this if you can find something help regarding user input validation.

If one parameters is fixed i.e. -f, why you are expecting it from the user input? why dont you use it in your shell script itself. And as manthasirisha said, post your code here.

Regards,
Tayyab
# 4  
Old 07-17-2006
Hi,
It worked. But the only thing is that when I put the same code in a function it doesnt work. It says test argument expected in the line where i check for the existence of the file

v=$1
if [ $v = -f ]
then
echo "ok"

if [ -f $2 ]
then
echo "exists"

if [ -r $2 ]
then
echo "Read permissions exists for the file"


sed '2,2d' $2 > sdh_reconfig_modify_dl.tmp
more sdh_reconfig_modify_dl.tmp | grep -q "^|"
if (($?!=0))
then
echo "ok"

while read line
do
tmp1=`echo ${#line}`
line=`echo $line | sed 's/|//g'`
cnt=$(($tmp1 - `echo ${#line}`))

if [ \( $cnt -ne 1 \) ]
then
echo "The fields in the file must be seperated by a |"
exit 1
echo "File format is correct. Proceeding for further execution..."
fi
done < sdh_reconfig_new_dl.tmp

tr '|' ' ' < sdh_reconfig_new_dl.tmp > sdh_reconfig_new_dl1.tmp
exec < sdh_reconfig_new_dl1.tmp
read dl1 dl2
echo $dl1
echo $dl2


else
echo "File cannot start with a |"
exit
fi


else
echo "The file doesnot have read permissions"
exit
fi

else
echo "The file that you have entered does not exist"
exit
fi
else
echo "The filename must be preceeded by a -f option"
exit
fi
# 5  
Old 07-17-2006
Function that expects arguments need to have them , isn't it?

Try calling your function by passing the relevant parameters. Say, if you're passing the filename alone, then u may do:

func1 <<filename>>
Do post the error and the function call, if u still have trouble!
# 6  
Old 07-17-2006
Quote:
Originally Posted by sendhilmani123
Hi,

I want to do the following validations in my script when my script gets 2 parameters as command line inputs.

My script expects 2 inputs : a -f option and a filename

If a filename is given as input without the -f option then I have to exit. If only -f option is given and no filename is given then also I have to exit.

Once I get both the inputs I have to check whether the file has read permissions or not.

Please provide me a snippet of code as of how to do this.

Thanks in advance
If the filename is all that you need, why have a -f flag ? It is not needed unless you have other flags, or plan to introduce other flags.

The script could be as simple as

Code:
#! /bin/ksh

if [[ $# -eq 1 ]] ; then
  # we have a filename
  FILE="$1"
else
  # we have more than 1 argument. Exit
  exit 1;
fi ;

if [[ -r "$FILE" ]] ; then 
  echo "$FILE exists and is readable"
else
  echo "Either not a file, or not readable"
  exit 1;
fi;

Not tested tho'.
# 7  
Old 07-17-2006
Hi, Sorry for the late reply. It worked when I passed the parameters to the function. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. UNIX for Beginners Questions & Answers

Script to accept Multi line inputs

Hi there, I'm trying to create a script that will accept multiple inputs by copying and pasting the strings from a notepad, hit Enter key and output the string to a text file.I'm thinking of using the read command however it will just simply get the first line. Apologies but got no idea how... (7 Replies)
Discussion started by: norbie.lopez
7 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

5. Shell Programming and Scripting

[Solved] How to refer more than 9 command line inputs for a scripts in korn shell?

Hi all, I have a script which should take more than 9 command line inputs while running. Likescript.sh a s d f g h j j k l o p i u y t r e w Now in the script if I have to access one of the input which is at position after 9, in this case say 'p' then how can I do that? echo $12 will not work... (15 Replies)
Discussion started by: pat_pramod
15 Replies

6. UNIX for Dummies Questions & Answers

How to send keyboard inputs toa UNIX command executed from a shell script?

I have a unix command that prompts for 'y'. How do I run this from my shell script? (4 Replies)
Discussion started by: Sree10
4 Replies

7. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

8. Shell Programming and Scripting

Problem in sending inputs to format command using expect

Hi , I am not able figure out how to use expect tool to send input to this prompt of format Searching for disks...done selecting c0t3d0 FORMAT MENU: disk - select a disk type - select (define) a disk type partition - select (define) a partition... (3 Replies)
Discussion started by: chidori
3 Replies

9. Shell Programming and Scripting

edit a line for getting inputs

Hello Everybody, I have a file with data like this 8z:1 15y:0 7x:0 12w:1 ... ... I would like read each line through a loop and will needing to get the input from each line like this For 8z:1; a=8,b=1 15y:0; a=15,b=0 7x:0; a=7,b=0 Please let me know of a way to... (5 Replies)
Discussion started by: leo.maveriick
5 Replies

10. Shell Programming and Scripting

Command line inputs validation

Hi, I have a script called read.sh that takes a file as input. Now I want to make that script take the file as input with a -f option preceding the filename. How can I do this validation. How can I find whether the given option is -f or not inside the script. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies
Login or Register to Ask a Question