• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

simple Bash script question

cHeeZeFacTory

Golden Member
I have a bash script that reads in path entered by a user,

read new_path

echo ${new_path}

right now if the user enters in a path like, "~/projects", I get of course, "~/projects".

How do i make it so the echo displays the full path (e.g. "/home/usrA/projects") ?
 
Does bash support the "if" statement ? If so, then perhaps you could just check if the first character of user input is "~". If so, then you could replace it with the full path.

It's been about 3 months since my last Linux project, so I think I've lost my touch a bit, but it SHOULD be possible, IMO.

I just finished a google search. You might be better off asking your question here,

http://forum.bash-hackers.org/

(don't blame me if they don't like noobs, I've never tried them)

Or a very friendly site, try out the Ubuntu forums. Your question seems general enough that you SHOULD get a lot of help there. And I know it's a nice place.

http://ubuntuforums.org/

 
If all you care about is resolving the ~ then some quick hack like described above may work.

In general, I've not found a handy unix utility to find the truename of a file. This is nice functionality found in Common Lisp, and Emacs has it too. It resolves all symlinks, tildes, relative paths, whatnot, to get you the absolute path to a filename.

When I needed the truename of a file, I resorted to invoking Emacs from the shell script. I shit you not.

real_path=`emacs -q --no-site-file -batch --eval "(progn (princ (file-truename \"${new_path}\")) (terpri))"`

princ writes a string to the screen, terpri writes a newline to the screen. Enjoy.

P.S. "dirname" is just simple string manipulation that doesn't do anything smart with the filesystem.
 
Easiest way I can come up with is to change your script to this:
-----------
echo $1
-----------

and then pass in the path at the command line like so: "./script.sh ~/projects"
 
The following works in CSH:
set l_path = $1
set full_path = ` cd $l_path ; pwd `

So in bash it'll look something like:
full_path = ` cd $relpath; pwd `

Originally posted by: dinkumthinkum
real_path=`emacs -q --no-site-file -batch --eval "(progn (princ (file-truename \"${new_path}\")) (terpri))"`

I have to say this is a very creative way indeed embedding lisp into a shell script!
 
Originally posted by: dinkumthinkum
If all you care about is resolving the ~ then some quick hack like described above may work.

In general, I've not found a handy unix utility to find the truename of a file. This is nice functionality found in Common Lisp, and Emacs has it too. It resolves all symlinks, tildes, relative paths, whatnot, to get you the absolute path to a filename.

When I needed the truename of a file, I resorted to invoking Emacs from the shell script. I shit you not.

real_path=`emacs -q --no-site-file -batch --eval "(progn (princ (file-truename \"${new_path}\")) (terpri))"`

princ writes a string to the screen, terpri writes a newline to the screen. Enjoy.

P.S. "dirname" is just simple string manipulation that doesn't do anything smart with the filesystem.

That is elegant. I was just trying to solve the problem at hand (which was turn ~ into /home/username). But your solution is nice.
 
Back
Top