LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-09-2011, 05:14 AM   #1
vraptor
LQ Newbie
 
Registered: May 2011
Posts: 11

Rep: Reputation: 0
[shell script] choose a file to use


I had great support last time i asked here so i am back with another question.

I have a small script that uses the find command to look for a log file named: backup_log.txt

And then uses the first value in the log file as a variable in the script later on.
However, say there are two or more of these log files located in different directories, how would i let the user choose which log file will be the one to use, and then make that (fullpathtofile) the value of the variable that will be used.

Thank you.
 
Old 05-09-2011, 05:27 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A method to prompt the user for doing a choice is by means of the select construct. Example:
Code:
#!/bin/bash
select file in $(find . -name backup_log.txt)
do
  echo
  echo "Your selected file is: " $(readlink -f $file)
  echo
  break
done

echo "Now I can use the variable \$file whose value is $file"
where the readlink -f statement is to retrieve the full path of the file, if required. See http://www.gnu.org/software/bash/man...ndex-select-56 for details. Hope this helps.
 
1 members found this post helpful.
Old 05-09-2011, 05:29 AM   #3
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
just a suggestion ;-)

when you execute your script, you can give a logfile as an argument, like

Code:
./script.sh -log logfile.txt
 
Old 05-10-2011, 12:17 AM   #4
vraptor
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
A method to prompt the user for doing a choice is by means of the select construct. Example:
Code:
#!/bin/bash
select file in $(find . -name backup_log.txt)
do
  echo
  echo "Your selected file is: " $(readlink -f $file)
  echo
  break
done

echo "Now I can use the variable \$file whose value is $file"
where the readlink -f statement is to retrieve the full path of the file, if required. See http://www.gnu.org/software/bash/man...ndex-select-56 for details. Hope this helps.
Thank you this is great! Although, how would i go about displaying the options to be chosen? Ie which files are options.
Although i think i have an idea how.
Thanks again!
 
Old 05-10-2011, 12:30 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Quote:
Although, how would i go about displaying the options to be chosen? Ie which files are options.
Did you run the code? Assuming you change the file name to the one(s) you are looking for you will find they are all prepended a number
which when chosen will select that file name into the value $file.

If I have misunderstood, please explain further where you are unsure?
 
Old 05-10-2011, 12:36 AM   #6
vraptor
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by grail View Post
Did you run the code? Assuming you change the file name to the one(s) you are looking for you will find they are all prepended a number
which when chosen will select that file name into the value $file.

If I have misunderstood, please explain further where you are unsure?
Sorry for some reason it didn't show, but i implemented it into my code and it works just fine now, thank you so much for this
 
  


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
shell script that adds users name to a file when the file is modified netting4u Linux - General 1 03-30-2010 05:09 AM
Need a shell script to turn a lined file into a comma seperated file Thaidog Linux - General 8 03-08-2008 08:19 PM
cannot execute script file from shell shahrahulb Linux - General 4 03-04-2008 08:02 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM
Can double-click the shell script and choose "Run in Terminal" in Redhat? edwardsiow Programming 2 05-22-2006 09:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:47 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration