LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 11-18-2004, 10:50 AM   #1
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Rep: Reputation: 15
Question Set working directory in shell


I am building an install script for some software

The following shell script is installed into a location chosen by the user
#!/bin/sh
java -jar lib/testapp.jar

which starts a Java application.

I also create a symbloic link to the above file and put it on the users Desktop.

If I go to where the shell script is installed and run it it works ok but if I try to run it by using the symbolic link it fails because the current working directory (pwd) is the Desktop instead of the location the file is installed.

i want to change the shell script so that it changes the users current working directory to the right location before runnning the java command. But I need to do this in a portable way which doesnt require me to hardcode the installtion dir.

So I need
#!/bin/sh
cd to location of this file
java -jar lib/testapp.jar

NOT
#!/bin/sh
cd /opt/testapp
java -jar lib/testapp.jar

Can this be done, please
 
Old 11-18-2004, 11:09 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
First (not necessarily the most beatiful) way to do it:
Code:
#!/bin/sh
pwd
echo I\'m really located in `realpath $0 | sed 's%/[^/]*$%%'`
cd `realpath $0 | sed 's%/[^/]*$%%'`
pwd
(The pwd and echo lines are for printing only)
 
Old 11-18-2004, 12:18 PM   #3
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks for response.
BUT realpath doesnt exist as a shell command on my system (Redhat Fedora 2) although there is a manual page entry for the C library API for realpath.

Any suggestions
 
Old 11-18-2004, 12:29 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Hmm.. my system has realpath.

In this case command "/bin/readlink -f" seems to do the same thing.
(eg. cd `/bin/readlink -f $0 | sed 's%/[^/]*$%%'`)
 
Old 11-18-2004, 01:01 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
That should work on any system:
Code:
dirname $(ls -l $file | sed 's/.*-> //')
 
Old 11-18-2004, 01:10 PM   #6
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks realpath works ( I couldnt get dirname to work)

BTW is there a defined core set of commands that will absolutely be on any Linux system (e.g ls,cat,more ...)
 
Old 11-18-2004, 01:59 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
No, Linux is following the Bazaar model, so there would always be someone with its own reason to create its own distro without one of these commands ...
However, there is project specifying a common set of interfaces (LSB: Linux Standard Base) including shell commands that need to be present on a conforming system, see http://refspecs.freestandards.org/LS...B/command.html

What's the problem you got with dirname ? It should have worked on LSB too.

This works for me:
Code:
#!/bin/ksh
cd $(dirname $(ls -l $0 | sed 's/.*-> //'))
pwd

Last edited by jlliagre; 11-18-2004 at 02:12 PM.
 
Old 11-18-2004, 02:17 PM   #8
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Original Poster
Rep: Reputation: 15
It complained Too many Arguments, and as I dont really understand Sed Syntax and I have a dual boot System and needed to use Windows i had to leave it.

Your using Korn Shell I was using Bourne Shell does that make a difference

Last edited by paultaylor; 11-18-2004 at 02:22 PM.
 
Old 11-18-2004, 02:45 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Okay, here's the fixed code:
Code:
#!/bin/ksh
cd "$(dirname "$(ls -l "$0" | sed 's/.*-> //')")"
pwd
You've got spaces in your link path.
Bash or Ksh doesn't make a difference for that one.
 
Old 11-19-2004, 01:35 AM   #10
paultaylor
LQ Newbie
 
Registered: Nov 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Ok, thanks that works as well
 
  


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: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
vhosts set up for a new shell service TngMstr Linux - Networking 1 03-22-2005 12:04 PM
can I set these flags using a shell script nirav.jani Linux - Security 1 01-12-2005 06:01 PM
How do i set up a shell account flamah Linux - Newbie 5 05-26-2004 06:48 PM
how do I move a directory in shell DKY Linux - Newbie 7 07-30-2003 05:33 PM

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

All times are GMT -5. The time now is 03:54 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