Checking whether the entered text is file or not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking whether the entered text is file or not
# 1  
Old 03-08-2013
Checking whether the entered text is file or not

how to check that the "file path" entered by the user has the valid file.
# 2  
Old 03-08-2013
I think we discussed a similar requirement in post #13 of this thread.
File tests are mostly the same in shell/perl.
Read the manpages of "test" --> man test
# 3  
Old 03-08-2013
I usually do this with file command. If the exit status is returned in success, it means the file or the path is okay.

verifying a single file and then checking the exit status.

Code:
# file /usr/share/readline/inputrc
/usr/share/readline/inputrc: ASCII English text
# echo $?
0
#


verify the existing of directory

Code:
# file /usr/share/readline
/usr/share/readline: directory
# echo $?
0
#


Similarly, verifying a non existing file with exit status.

Code:
# file /usr/share/readline/ThisWillbeAnError
/usr/share/readline/ThisWillbeAnError: ERROR: cannot open `/usr/share/readline/ThisWillbeAnError' (No such file or directory)
#echo $?
1
#


alternatively, you can do a test on the file/path using
Code:
[[ -d /path/to/directory ]]

in
Code:
if else

structure.

like:

Code:
if [[ -d /home/user ]]; then
echo "/home/user is a directory"
fi

# 4  
Old 03-08-2013
let me elaborate it a bit..

the path entered by the user is supposed to be something like "$ABCD/defef/geft.txt" and i want to check if anything out of "$ABCD/defef/geft.txt" is mistyped. The script should exit.

and if, only $ABCD or defef or gef.txt is entered by the user, still script should exit.

i tried this



Code:
$Input_filename=$ARGV[0];
	
if (!-e $Input_filename $$ !-d $Input_filename) 
{
	print "the file does not exist, Please enter a valid file name";
	exit;
}


please suggest where i am mistaken...

Last edited by Rashid Khan; 03-08-2013 at 03:45 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 03-08-2013
why are you using "$$"...are you by any chance refering to pid of the current shell script or this is a typo for "&&" which is AND in condition matching..

shouldn't be that
Code:
if (!-e $Input_filename && !-d $Input_filename)

---------- Post updated at 02:02 PM ---------- Previous update was at 01:54 PM ----------

2ndly

if you making test using if-else try using [[ ]] braces or the test built-in. This is more approperiate...never used ( ) for testing

Code:
if  [[ ! -e $Input_filename && !-d $Input_filename ]]

or
Code:
if test ! -e $Input_filename  -a -d $Input_filename

# 6  
Old 03-08-2013
hi busyboy...

That '$$' was a typing mistake.

thanks for the code its working fine....

Smilie Smilie
# 7  
Old 03-08-2013
welcome
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Checking and replacing first line in text file, one-liner?

Hello, I'm looking to check only the first line of a file to see if it is a format string, like # -*- coding: utf-8; mode: tcl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -\*- vim:fenc=utf-8:ft=tcl:et:sw=2:ts=2:sts=2if the first line is anything else, insert the above string. I'd... (3 Replies)
Discussion started by: f77hack
3 Replies

3. Shell Programming and Scripting

How to check user entered correct file format or not?

Hi Experts, path=/db/files/ format=$1 User can enter any file format.compare the user file format with actual file format existed in the directory /db/files. User enter all characters as "A" apart from date format. example1: user will be entering the file format AAA_AA_YYYYMMDD.AAA Actual... (6 Replies)
Discussion started by: nalu
6 Replies

4. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

5. Shell Programming and Scripting

only a number can be entered no letters?

ok the user can only enter a number if a letter is entered it shouldnt be accepted This is what i have so far read -p "How many cars to enter:" cars until do read -p "Invalid number. Please re-enter:" $tags done (5 Replies)
Discussion started by: gangsta
5 Replies

6. Solaris

Question marks appearing instead of text entered

I have a netscape 4.79 browser for our GUI which connects to a Solaris5.8 box. During peek hours, we see question marks appearing in the screen instead of the text we enter. This results in query failure. This problem does not happen always, and is quite irritating because, we have to close the... (6 Replies)
Discussion started by: vanz
6 Replies

7. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

8. UNIX for Dummies Questions & Answers

checking directory size in the text file

Hi All, I am new to unix scripting, please help me in completing this exercise, I have a scenario as follows, 1. i have a text file(snapshot.txt) consisting of directory names, and file size separated by comma as shown below: snapshot.txt data: ... (1 Reply)
Discussion started by: G.K.K
1 Replies

9. Shell Programming and Scripting

checking text file

Hello, I have the following report (report.txt) file (see attached). I would like to check the file and if the field is error, then showing error message at output. 1. In the report, 1st, 2st and 3nd line can be ignore to check 2. start to check line 4 (each field use "," to split - 1st... (1 Reply)
Discussion started by: happyv
1 Replies

10. Shell Programming and Scripting

Repeat last entered command ?

Hi, how to do that ? I mean only print it but not execute. I'm using putty to interact with ksh. (in windows cmd up arrow does the job) thanks vilius (5 Replies)
Discussion started by: vilius
5 Replies
Login or Register to Ask a Question