LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-22-2011, 12:12 PM   #1
Orange Sunshine
Member
 
Registered: Jul 2004
Location: Kansas
Posts: 37

Rep: Reputation: 15
Hopefully simple scripting question


I have a bash script that requires users to point to two different files like this:

Code:
echo "Path to file 1: "
read -e pathFile1

echo "Path to file 2: "
read -e pathFile2
The thing is, those two files will USUALLY be in the same directory and the way it is now, the user has to put the full path to each file which can be a bit of a pain if the files are deep at all.

How can I have the second prompt automatically start in the directory the user pointed to in the first prompt? Keep in mind, the user will be pointing to a FILE so it would look like "/home/user/files/important/file1.zip" and I need the second prompt to start at "/home/user/files/important/" (sans the "file1.zip" part).

I suppose it's also important that the user be allowed to go back directories just in case the files happen to not be in the same directory (should be rare).

Last edited by Orange Sunshine; 08-22-2011 at 12:16 PM.
 
Old 08-22-2011, 12:47 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
parameter substitution can break a filepath into path and filename easily.

Code:
pathFile1="$( readlink -nqf "$pathFile1" )"

path1="${pathFile1%/*}"
file1="${pathFile1##*/}"
The first command ensures that you have the full canonical path to the file, as opposed to just a symlink or partial path. You should probably set up a test to ensure that the path leads to an existing file first.

Then you can cd into the resulting directory, or do whatever else you want.
 
1 members found this post helpful.
Old 08-22-2011, 02:44 PM   #3
Orange Sunshine
Member
 
Registered: Jul 2004
Location: Kansas
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
parameter substitution can break a filepath into path and filename easily.

Code:
pathFile1="$( readlink -nqf "$pathFile1" )"

path1="${pathFile1%/*}"
file1="${pathFile1##*/}"
The first command ensures that you have the full canonical path to the file, as opposed to just a symlink or partial path. You should probably set up a test to ensure that the path leads to an existing file first.

Then you can cd into the resulting directory, or do whatever else you want.
Thanks, this all works nicely but ideally I would like the prompt to show the user that they are in that particularly directory. So I now have:

Code:
echo "Path to file 1: "
read -e pathFile1

pathFile1="$( readlink -nqf "$pathFile1" )"
path1="${pathFile1%/*}"
cd $path1

echo "Path to file 2: "
read -e pathFile2
...and when the user is prompted for file 2 they can simply type the name of the file because they are already in the correct directory. BUT, at the prompt it simply says:

Path to file 2:
[insertion point here]

so there's a blank line where the user will enter the path (or just the file now). Is there any way that blank line could have the actual path of path1 showing for the user? So after then are prompted for file 1 and enter the path the next prompt would automatically have:

Path to file 2:
/home/user/files/important/[insertion point here]

Hope what I'm asking makes sense.
 
Old 08-22-2011, 02:52 PM   #4
Orange Sunshine
Member
 
Registered: Jul 2004
Location: Kansas
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Orange Sunshine View Post
Thanks, this all works nicely but ideally I would like the prompt to show the user that they are in that particularly directory. So I now have:

Code:
echo "Path to file 1: "
read -e pathFile1

pathFile1="$( readlink -nqf "$pathFile1" )"
path1="${pathFile1%/*}"
cd $path1

echo "Path to file 2: "
read -e pathFile2
...and when the user is prompted for file 2 they can simply type the name of the file because they are already in the correct directory. BUT, at the prompt it simply says:

Path to file 2:
[insertion point here]

so there's a blank line where the user will enter the path (or just the file now). Is there any way that blank line could have the actual path of path1 showing for the user? So after then are prompted for file 1 and enter the path the next prompt would automatically have:

Path to file 2:
/home/user/files/important/[insertion point here]

Hope what I'm asking makes sense.
Nevermind, looks like -i at the read line will do the trick. Thanks David the H for the help!
 
  


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
bash scripting simple question wildtiger23 Programming 1 03-04-2009 11:35 AM
simple scripting question davidstvz Linux - Newbie 7 08-25-2008 09:58 AM
Another simple scripting question Echo Kilo Linux - Newbie 10 05-14-2008 10:52 AM
simple scripting question jerf Linux - Newbie 4 09-19-2007 04:12 AM
simple scripting question wedgeworth Programming 6 02-04-2004 11:30 AM


All times are GMT -5. The time now is 05:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration