Getting path name using bash script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting path name using bash script
# 1  
Old 03-19-2012
Getting path name using bash script

I have this code to extract the directory name ($0:h) in tcsh. I am converting the code to bash and need a way to get the equivalent setting for DefRaytracPath

Code:
set DefRaytracPath = `echo $0:h | awk 'BEGIN {FS="/tcsh"} {print $1"/prog"}'`

# 2  
Old 03-19-2012
Are you triying to do so from some script .. if so try below

Code:
loc1=`basename $0`
if [[ "`echo $0 | cut -c1`" = "/" ]]; then
  loc2=`dirname $0`
else
  loc2=`pwd`/`echo $0 | sed -e s/$loc1// | sed -e "s/.$//g"`
fi
fles=$loc2
echo $fles

As here am not sure about use of your
Code:
set DefRaytracPath

Can do so by just
Code:
set DefRaytracPath = `echo $fles`

--Shirish Shukla
# 3  
Old 03-19-2012
I have done like this
Code:
dflt_bashPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "dflt_bashPath = $dflt_bashPath"
dflt_raytracPath=`echo "$dflt_bashPath" | awk 'BEGIN {FS="/bash"} {print $1"/prog"}'`
echo "dflt_raytracPath = $dflt_raytracPath"

Basically I will be running a script , example

Code:
/home/chrisd/tatsh/trunk/hstmy/bin/bash/raytrac.bash

From the script I then want to run

Code:
/home/chrisd/tatsh/trunk/hstmy/bin/prog/raytrac

Thus I want to change
Code:
/home/chrisd/tatsh/trunk/hstmy/bin/bash

to
Code:
/home/chrisd/tatsh/trunk/hstmy/bin/prog

# 4  
Old 03-19-2012
Or (not having read post #3 which is a different problem):
Code:
DefRaytracPath=`dirname "${0}"`
if [ "${DefRaytracPath}" = "." ]
then
        DefRaytracPath=$(pwd)
fi
echo "${DefRaytracPath}"

This will work id the script is run with a full path or as ./scriptname or . ./scriptname or just as scriptname .
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

4. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

5. Shell Programming and Scripting

Importing a path/file into a bash script

Hey, I'm new here. Basically, I'm trying to make a bash script that affects a file of my choice. What I want to do is $./script.sh /path/to/file.jpg and then the bash script will know that variable=/path/to/file.jpg Thanks! (4 Replies)
Discussion started by: TFB
4 Replies

6. Shell Programming and Scripting

BASH - read use a path as input

I am trying to script simply data transfer. I would like to have the user input the source "SRC" (/Volumes/DriveName/Users/johnq123) and then name the directory that the copied information will go to, "DST" . put I can't get it to work - #!/bin/bash ... (8 Replies)
Discussion started by: dropkick888
8 Replies

7. Shell Programming and Scripting

absolute path for a script ran with relative path

I have a script in which i want to print absolute path of the same script irrespective of path from where i run script. I am using test.sh: echo "pwd : `pwd`" echo "script name: $0" echo "dirname: `dirname $0`" when i run script from /my/test/dir/struct as ../test.sh the output i... (10 Replies)
Discussion started by: rss67
10 Replies

8. Shell Programming and Scripting

setting a path in bash shell

Hello all, Sorry if the question if stupid but I have no big experience with programming. I am trying to set a path to be used in a makefile.in, for installation of a Fortran code. The makefile.in contains the string $(CODE_NAME) Now, when I type in the bash shell export... (6 Replies)
Discussion started by: laura74
6 Replies

9. Shell Programming and Scripting

PATH on solaris 8 (bash)

Im trying to setup my profile to export the PATH at login i had this in my .bashrc export PATH=/usr/local/bin:/usr/bin/ however when i login and type echo $PATH i only get bash-2.05$ echo $PATH /usr/bin: any ideas on what im doing wrong? ive used linux for years but this is my... (1 Reply)
Discussion started by: recklessop
1 Replies

10. Shell Programming and Scripting

Bash Script to display directories in Path?

Hello there, As a newbie: The directories in PATH can be hard to distinguish when printed out as one line with colon .Please, can i have a sample script to display them,one to a line. Thank you. (1 Reply)
Discussion started by: debut
1 Replies
Login or Register to Ask a Question