Re: [SLUG] bash shell script question

From: Daniel MacLaren (maclaren@creag.org)
Date: Sun Sep 15 2002 - 11:13:56 EDT


You can perform Tilde Expansion with "eval cd $directory", but as
Ronan mentioned you script won't change the working directory of the shell
that launched the script.

-Daniel

On Sun, 15 Sep 2002, Ronan Heffernan wrote:

> Jan Mason wrote:
> > Using ~/ works the same as using just ~ in both of the test cases.
> >
> > Norbert Cartagena wrote:
> >
> >> Jan Mason wrote:
> >>
> >>> In test case 1, when a "~" is passed in as a parameter it is
> >>> substituted for $HOME and the script works.
> >>>
> >>> In test case 2, when no parameter is passed and the "~" is assigned
> >>> to the var $directory using the read command, $HOME is not
> >>> substituted for "~" and the script does not work.
> >>>
> >>> How do I get test case 2 to work?
> >>>
> >>> bash shell script named green:
> >>>
> >>> directory=$1
> >>> if [[ $directory = "" ]]
> >>> then
> >>> echo -n "prompt for dir: "
> >>> read directory
> >>> fi
> >>> echo "dir = "$directory
> >>> pwd
> >>> cd $directory
> >>> pwd
> >>>
> >>> Test case 1 output:
> >>>
> >>> $ green ~
> >>> dir = /home/jan
> >>> /home/jan/bin
> >>> /home/jan
> >>>
> >>> Test case 2 output:
> >>>
> >>> $ green
> >>> prompt for dir: ~
> >>> dir = ~
> >>> /home/jan/bin
> >>> /home/jan/bin/green: cd: ~: No such file or directory
> >>> /home/jan/bin
> >>>
> >>
> >> Perhaps I'm wrong, but shouldn't it be ~/ ?
> >>
> >> Gnorb
> >>
> >>
> >
>
> BASH 'expands' the ~ to the value of $HOME whenever it is passed-in to
> any program as a command-line arg. This is not going to happen for
> interactive user input. You might be able to use 'expr' to perform the
> expansion:
> read directory
> fi
> echo "dir = " `expr $directory`
>
> Something like that should work.
>
> You aren't trying to get this script to change your pwd are you? Unless
> you 'source' that script every time, you will be changing the pwd of the
> shell that was launched just for the purpose of executing the script.
> Once the script exits, and the short-lived shell terminates, the pwd of
> your original shell should be unchanged.
>
> --ronan
>
>
>
>
>



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:36:24 EDT