LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-11-2009, 03:02 AM   #1
wonderfullyrich
LQ Newbie
 
Registered: Sep 2009
Location: Kampala
Distribution: Ubuntu
Posts: 10

Rep: Reputation: 0
bash: PWD minus last two directories


I've got a script that where I'm trying to check the existance of a file two directories above the working directory. i.e. working directory is /home/bozo/ideas/thoughts and I'm interested in files in /home/bozo

How do I manipulate PWD (or something) into a variable that I can check?

I tried this but with no success.
Code:
outputfilename=bozo.mpg
outputfile=../../${outputfilename}
if [ -f ${outputfile}  ] ;

then

blah blah

else

blah blah blah


fi
I was playing pattern replacement but can't figure it out.
i.e.
Code:
${PWD##*/}
execept reversed and going back two /


TIA for any help recieved,
Rich

Last edited by wonderfullyrich; 09-11-2009 at 03:10 AM.
 
Old 09-11-2009, 04:08 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
try
Code:
IFS=/
set -- $PWD
echo "/${*:1:$(( $# - 2 ))}"
 
Old 09-11-2009, 04:42 AM   #3
wonderfullyrich
LQ Newbie
 
Registered: Sep 2009
Location: Kampala
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
thx.
I thought a parameter subsitution would do the trick. I don't yet fully understand how this is working, but I'll keep trying to parse it.

When I tried this I'm now getting
"script.sh: 60: Bad substitution"
which is indicating the line
Code:
echo "/${*:1:$(( $# - 2 ))}"
Any idea why my script doesn't like this?
 
Old 09-11-2009, 05:19 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I'd rather return to the original issue. Why extracting this information from $PWD when the notation ../../ should work instead? Can you post the output of the following command?
Code:
bash -x script.sh
Option -x of bash let you check all the executed commands after expansions have been performed. It prints out the commands being executed, together with their output. Very useful for debugging!

The content of script.sh should be a simple test script like:
Code:
#!/bin/bash
outputfilename=bozo.mpg
outputfile=../../${outputfilename}
if [ -f ${outputfile} ]
then
  echo file found
else
  echo file not found
fi
Note that you can get the same behaviour if you put option -x in the sha-bang and run the script in the usual way.
 
Old 09-11-2009, 05:32 AM   #5
wonderfullyrich
LQ Newbie
 
Registered: Sep 2009
Location: Kampala
Distribution: Ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
colucix, that helps a bunch. Turns out I was dumb and was missing the extension, but couldn't tell. Debug like that will be an invaluable tool!

Thanks!
 
  


Reply

Tags
bash, parameter


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
pwd: error retrieving current directory: getcwd: cannot access parent directories: Pe bootkernel Linux - General 1 05-05-2008 05:45 AM
Bash Prompt - display correct $(pwd) tschima Linux - General 2 07-05-2007 07:03 PM
redirecting the output of pwd into konqueror as a url. using bash fiery_ice Linux - Newbie 5 04-05-2006 03:13 PM
reset pwd to root's current pwd? ataraktos2 Linux - General 10 04-26-2005 04:13 AM
getting custom PWD in bash prompt techrolla Linux - Software 2 08-09-2004 12:39 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration