LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Set working directory in shell (https://www.linuxquestions.org/questions/programming-9/set-working-directory-in-shell-256449/)

paultaylor 11-18-2004 10:50 AM

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

ToniT 11-18-2004 11:09 AM

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)

paultaylor 11-18-2004 12:18 PM

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

ToniT 11-18-2004 12:29 PM

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%/[^/]*$%%'`)

jlliagre 11-18-2004 01:01 PM

That should work on any system:
Code:

dirname $(ls -l $file | sed 's/.*-> //')

paultaylor 11-18-2004 01:10 PM

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 ...)

jlliagre 11-18-2004 01:59 PM

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


paultaylor 11-18-2004 02:17 PM

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

jlliagre 11-18-2004 02:45 PM

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.

paultaylor 11-19-2004 01:35 AM

Ok, thanks that works as well


All times are GMT -5. The time now is 08:57 PM.