Searching files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching files
# 1  
Old 11-30-2012
Searching files

i want to search a file bt it not happening i m using
Code:
#!bin/bash
read file
 if (-e "$file")
then
echo "asfsafafa"
else
 echo "NO SUCH FILE"
fi

....error
Code:
./VMC.sh: line 5: [fegsgws]: command not found
NO SUCH FILE
;;;;;;;;;;

its giving correctly no such file found but whats is command not found.

Last edited by Franklin52; 11-30-2012 at 04:40 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-30-2012
The following need to be modified.
Code:
if (-e "$file") ==> if [ -e $file ]

Cheers,
This User Gave Thanks to Joseph_TKLee For This Post:
# 3  
Old 11-30-2012
noo thats not working
# 4  
Old 11-30-2012
it should work..
Please check..

Code:
$ cat file2
read file
if [[ -e "$file" ]]
then
echo "asfsafafa"
else
echo "NO SUCH FILE"
fi

Code:
$ sh file2
file
asfsafafa

$ sh file2
no_file
NO SUCH FILE

And your script also can be written as..

Code:
read file
[[ -e "$file" ]] && echo "Pass" || echo "Fail"

This User Gave Thanks to pamu For This Post:
# 5  
Old 11-30-2012
I have tested it below
Code:
# ./test.sh
aaaa
./test.sh: line 2: -e: command not found
NO SUCH FILE
# vi test.sh
 # ./test.sh
asdfa
NO SUCH FILE
# cat test.sh
read file
if [ -e "$file" ]
then
echo "asfsafafa"
else
echo "NO SUCH FILE"
fi
#

After changing to [ -e "$file" ], it is working. Make sure there is a space between [ and -e.

Cheers,
This User Gave Thanks to Joseph_TKLee For This Post:
# 6  
Old 11-30-2012
yea got it friends Smilie

---------- Post updated at 01:46 AM ---------- Previous update was at 01:33 AM ----------

friends one more question
Code:
read file >> $file.csv

is it correct we can make a new file with same file name ?
and my read file is also .csv
# 7  
Old 11-30-2012
Quote:
Originally Posted by console
friends one more question
read file >> $file.csv
is it correct we can make a new file with same file name ?
and my read file is also .csv
change your code to
Code:
read file
touch $file

If file is already present how you can make new file with same file name.
You should decide either you want create new file or append data into old file.

You can append data to that file
using
Code:
echo "data" >> $file

OR create new file
Code:
echo "data" > $file

and if your file name is having csv in it then you don't need to use $file.csv
and if you want to add extension use $file".csv"

I hope this helps Smilie

pamu

Last edited by pamu; 11-30-2012 at 03:23 AM..
This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

2. UNIX for Dummies Questions & Answers

Searching all Files on Computer

Hi I need to search all files on my Solaris box and have to know which files are 2GB or more...any combination of commands to do this? (4 Replies)
Discussion started by: mojoman
4 Replies

3. UNIX for Advanced & Expert Users

Searching for files

Hi, I have the following command to list files beginning with a specific name and containing some text... find . -type f -name "dm_merge_domain_adm*" -exec grep -il "Error Message:" '{}' \; -print|xargs ls -ltr It works fine, but seems to list two of each file, when they only exist once...any... (1 Reply)
Discussion started by: chrislluff1976
1 Replies

4. Shell Programming and Scripting

Files searching

I have a list of files in directory and i should write a script if any of these files contains words given in a text file test.txt. the words can be case ignored and word should match. The output should be the name of the directory in which the file is present followed by list of file names Eg:... (1 Reply)
Discussion started by: kinny
1 Replies

5. Shell Programming and Scripting

Searching all files that contain pattern

Hello All, i have to search a pattern in all the files in all subfolders that are present in current directory. suppose i am in d1 directory and in that sd1,sd2,sd3 are subdirectories. in sd1 i have files f1,f2 sd2 i have files f3,f4 sd3 i have file f5 i have to list out all those... (4 Replies)
Discussion started by: ravi.sadani19
4 Replies

6. Shell Programming and Scripting

awk searching between two files

hi there, im writing some script in awk; in few words i have a list from router (mac address- ip address) and the second list with only the mac addresses. the thing is that i want search list from router for the first mac address; if found - print the ip address, if not print error; then search... (1 Reply)
Discussion started by: mac7
1 Replies

7. UNIX for Dummies Questions & Answers

searching for content of files

Hi, This question may be quite newbish. I've stored a few files on my Unix system and am wondering how to search for their contents (i.e. I input the keyword and get a list of files with this keyword) I'd then like to put it on my website (php). I thought of find and grep, but am not... (19 Replies)
Discussion started by: Aretai
19 Replies

8. UNIX for Advanced & Expert Users

Searching Files

Hi, I have a file /db01/dat/march 2006/7001DW06.03B Please note, between "march 2006" there is a space/tab. While running the following script, it identifies /db01/dat/march ----> as first file 2006/7001DW06.03B ---> as second file. SRC_PATH = /db01/dat SEARCH_FILENAME =... (12 Replies)
Discussion started by: ronald_brayan
12 Replies

9. UNIX for Dummies Questions & Answers

Searching files within subdirectories

I need to concatenate files that are inside a directory and subdirectories. Those files end with .c Can anyone help me I am using comand 9find) and (cat) but they don't work together.:confused: (1 Reply)
Discussion started by: jalvarez
1 Replies

10. UNIX for Dummies Questions & Answers

Searching files..?

hi there can anyone tell me how to search and copy files under unix? im writing shell scripts with 'vi' and 'pico' something like read directoryName if then echo Copying the files copy those *.src files to sub1(another directory) using cp else ... (4 Replies)
Discussion started by: nickaren
4 Replies
Login or Register to Ask a Question