LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-02-2012, 01:57 AM   #1
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Rep: Reputation: Disabled
Unhappy Selecting output as a directory path


I have a script which take a path of a folder as an argument and a path for the output.

Ex- Myscript.sh /bin/apps/sample /bin/apps/

Input - /bin/apps/sample ($1)
Output - /bin/apps/ ($2)

Now the output should always be stored in /bin/apps/ ie one directory backwards of input directory. Please remember that the input and output directory paths vary as per user so i need a universal code which can perform the following operation.
 
Old 03-02-2012, 02:06 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Are you looking for this:
Code:
$ input="/a/b/c/d"
$ output="${input%/*}"
$ echo $output
/a/b/c
This ${input%/*} strips everything after the last / (including /).

Have a look at man bash, the Parameter Expansion section for details.

Hope this helps.
 
1 members found this post helpful.
Old 03-02-2012, 02:16 AM   #3
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Hi,

Are you looking for this:
Code:
$ input="/a/b/c/d"
$ output="${input%/*}"
$ echo $output
/a/b/c
This ${input%/*} strips everything after the last / (including /).

Have a look at man bash, the Parameter Expansion section for details.

Hope this helps.
I think its working. Thanks.
 
Old 03-02-2012, 02:17 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
Old 03-02-2012, 05:28 AM   #5
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by druuna View Post
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
Sorry to make this thread as unsolved again. Actually after the output directory is created, a third script requires its location and need to enter it.

How should i located the exact output directory created my the script as there will be so many output folders in the location?

How will a second script know the location of the output of first script?
 
Old 03-02-2012, 06:37 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by prravin1 View Post
How should i located the exact output directory created my the script as there will be so many output folders in the location?

How will a second script know the location of the output of first script?
Assuming that the second script is not started from within the first script or piped: You need to store the directory into a separate, temporary file. Normally independent scripts are not aware of each other.
Code:
#!/bin/bash
# get the desired directory (/a/b/c in this case)
input="/a/b/c/d"
output="${input%/*}"
# maybe other code goes here
.
.
# store the directory in a temp file
echo ${input} > /tmp/script1.out
The second script needs to get the info from that file:
Code:
#!/bin/bash
# get dir info
input="$(cat /tmp/script1.out)"
# rest of the code for script 2
.
.
Hope this helps.
 
1 members found this post helpful.
Old 03-02-2012, 06:42 AM   #7
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Wink

Quote:
Originally Posted by druuna View Post
Hi,
Assuming that the second script is not started from within the first script or piped: You need to store the directory into a separate, temporary file. Normally independent scripts are not aware of each other.
Code:
#!/bin/bash
# get the desired directory (/a/b/c in this case)
input="/a/b/c/d"
output="${input%/*}"
# maybe other code goes here
.
.
# store the directory in a temp file
echo ${input} > /tmp/script1.out
The second script needs to get the info from that file:
Code:
#!/bin/bash
# get dir info
input="$(cat /tmp/script1.out)"
# rest of the code for script 2
.
.
Hope this helps.
Thanks. Will try the above method. Can you have a look at my latest thread which is more brief about what i exactly want to achieve?
 
  


Reply



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
rsync error: errors selecting input/output files, dirs (code 3) at main.c(323) cucolin@ Linux - Networking 4 09-17-2020 02:03 PM
Find a file path and directory path ak.lokesh Linux - Newbie 3 02-19-2009 12:37 PM
Output directory path longer than 256 char. AmpTux Linux - Newbie 2 11-20-2008 03:56 PM
Selecting Audio Output sirius56 Linux - Hardware 2 11-26-2007 08:40 PM
How to rsynce the actual directory path instead of just the directory at the end of Niceman2005 Linux - General 5 04-08-2005 07:58 PM

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

All times are GMT -5. The time now is 03:33 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
Open Source Consulting | Domain Registration