LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2012, 01:38 PM   #1
9gagarmy
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
Find Shell script problem


I'm a newbie, I want to write a simple shell code to find a file, and if it exist vi it. But I have some problem with find command
this is the part i have problem.
Quote:
read -p "Input a name of file:" name
`find / -name $name>searchresult.txt`
$c=`head -n 1 searchresult.txt`
and the result of this code is
Quote:
=/My file location: No such of file or directory
please help me.
 
Old 10-23-2012, 02:02 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

1) In the bash version of read, you can use the -e option to give you readline editing. This will allow you to use tab completion and other advanced editing techniques.

2) In the second line, $(..) is highly recommended over `..`. And don't bother to use a text file; just store the result in another variable.

Although you probably have to decide what to do if the find command turns up more than one filename. Or no name.

You also need to enclose all variables in double quotes, to protect any spaces in their contents from shell word-splitting.

It's vital in scripting to understand how the shell handles arguments and whitespace:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


3) In the last line, you do not put $ in front of a variable when setting it, only when expanding it.


Ignoring the multiple match problem for the moment:
Code:
read -e -p "Input a name of file: " fname

fname=$( find / -type f -name "$fname" -print )	#there's probably no reason not to re-use the same variable

#you should do some other error checking here to determine if the file's legit before using it.

vi "$fname"
 
Old 10-23-2012, 02:36 PM   #3
9gagarmy
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
thanks for helping me, I will check it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find and Replace problem at Shell Script TzaB Programming 3 12-27-2008 06:16 AM
Shell script: not able to find jags.singh Programming 8 06-15-2007 10:12 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
find shell script help liren Linux - Newbie 3 05-02-2005 03:05 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration