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 |
First (not necessarily the most beatiful) way to do it:
Code:
#!/bin/sh |
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 |
|
That should work on any system:
Code:
dirname $(ls -l $file | sed 's/.*-> //') |
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 ...) |
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 |
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 |
Okay, here's the fixed code:
Code:
#!/bin/ksh Bash or Ksh doesn't make a difference for that one. |
Ok, thanks that works as well
|
All times are GMT -5. The time now is 08:57 PM. |